MATLAB 知识点

MATLAB画图

快捷键:
1.ctrl+r 注释

常用命令:
1.clc 清屏
2.clear 清除

循环:

while %条件
%...
end

for i=1:n
%...
end

for i=5: -1: 5
%...
end

v = [1 5 9 7 3 5 ];
for i = v
	disp(i);
end
%1 5 9 7 3 5

函数:

linspace(m, n, p);起始点m,终点n,中间取p个点
%取模a%b
mod(a,b)

%=
r ~= 0 等价于 r != 0

%输出
fprintf();
disp(); //disp([a,b,c]);
%求和函数
v = [1 2 3 4 ];
ans = sum(v);  
disp(ans);%10
%自定义函数
function res = gcd( a, b )
    if b == 0 
        res = a;
    else 
        res = gcd( b, mod(a, b));
end     

画图函数

直角坐标图像
plot(x,y);
plotyy(x1,y1,x2,y2);%两个标度
plot3(x,y,z);
ezplot('x-1/x+2')%仅画轮廓
%-------
theta = 0 : pi/50 : 6*pi;
x = cos(theta);
y = sin(theta);
z = 0 : 300;
plot3(x,y,z);

%-------

在这里插入图片描述

极坐标图像
t = 0:0.01:2*pi;
r = sin(t).*cos(t);
polar(t,r,'r');
同时画两张图
x1 = -5 : 0.1 : 5;
y1 = x1 .^ 2;
plot(x1, y1);
%1
hold on;

x2 = -5 : 0.1 : 5;
y2 = x2 .^ 3;
plot(x2 , y2);

%2
y = [sin(x);cos(x);2*sin(x)];

% 显示网格
grid on;

% 图表+标题
title(' title ');

% x、y轴加标签
xlabel(' x-axis ');
ylabel(' y-axis ');



画多个图
subplot(m , n, p); %有m行n列,当前操作第p个图
x = -10 : 0.1 : 10;
y1 = sin(x);
y2 = sin(2 .* x);
y3 = sin(3 .* x);
y4 = sin(4 .* x);

subplot(2, 2, 1);
plot(x, y1);
title(' y = sin( x ) ');

subplot(2, 2, 2);
plot(x, y2);
title(' y = sin( 2x ) ');

subplot(2, 2, 3);
plot(x, y3);
title(' y = sin( 3x ) ');

subplot(2, 2, 4);
plot(x, y4);
title(' y = sin( 4x ) ');
x = -10 : 0.1 : 10;
y1 = cos(x);
y2 = cos(2 .* x);
y3 = cos(3 .* x);

subplot(2, 2, 1);
plot(x, y1);
title(' y = cos( x ) ');

subplot(2, 2, 2);
plot(x, y2);
title(' y = cos( 2x ) ');

subplot(2, 2, [3 , 4]);
plot(x, y3);
title(' y = cos( 3x ) ');
三维图
%曲面
x = -3 : 0.1 : 3;
y = -3 : 0.1 : 3;

%投出所有点
[X , Y] = meshgrid(x , y);
z =  X.^2 + Y.^2;

%绘制曲面
surf(X, Y, Z);


%1
x = 1:10;
y = (1:10)';%%注意转置
X = ones(size(y))*x;
Y = y*ones(size(x));
Z = sin(X+Y);
subplot(2,2,1)
surf(X,Y,Z)
subplot(2,2,2)
mesh(X,Y,Z)
subplot(2,2,3)
meshc(X,Y,Z)%加等高线
subplot(2,2,4)
meshz(X,Y,Z)%加底座

%2
x = 1:10;
y = 1:10;
[X, Y] = meshgrid(x, y);
Z = sin(X+Y);

%移动曲面

X = -2*pi : 0.1 : 2*pi;
Y = sin(X);

h = plot(X, Y);

for i = 1 : 100
	X = X + 0.1;
	Y = sin( X );
	%把h里x的值变成X,y的值变成Y
	set(h, ' XData ', X, ' YData ', Y);
	%重新把所有东西画一遍 
	drawnow;
end
二维统计图
bar,stairs,stem,fill
条形图,阶梯图,茎状图,填充图

用法和plot一样
自适应图
fplot('cos(tan(pi*x))',[0,1],[1e-4])
fplot(函数,区间,误差);
线型和颜色
%线型
plot(x,y,'-');%实线
plot(x,y,':');%虚线
plot(x,y,'-.');点划线
plot(x,y,'--');双划线

%颜色
b:蓝色
g:绿色
y:黄色
k:黑色
r:红色

%标记点符号
o
x
s

eg:
线型、颜色、标记点符号顺序无所谓
plot(x,y,':r',x1,y1,':mx');
标注
title('---');
xlabel('x从0到2{\pi}');
xlabel('X');ylabel('Y');
text(0.8,1.5,'曲线y1=2e^{-0.5x}')
text(2.5,1.1,'曲线y2=cos(4{\pi}x)')
legend('y1','y2') //给曲线加标注在右上角

axis用法

% x,y坐标大小调整至一致
axis equal   

%显示、取消x坐标轴
axis on; axis off;

%产生正方形坐标系
axis square

%自定义坐标轴(x,y的最大最小)
axis([xmin xmax ymin ymax]);

常用函数

半对数
similogx(x,y)
similogy(x,y)

双对数
loglog(x,y);

插值与拟合

插值

linear、nearest、cubic、spline
插值函数:interp1(x,y,x0,method)
%(x,y)已知
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%样例
 clear;clc;
x = [2 4 7 11];
y = [1.1 2.1 3.1 4.8];
%单个插值
y0 = interp1(x, y, 3, 'linear')%1.6000
y1 = interp1(x, y, 3, 'cubic')%1.6403
y2 = interp1(x, y, 3, 'spline')%1.6540
%多个插值
y3 = interp1(x, y, [3 5 8 9 10], 'spline')%1.6540    2.4690    3.4238    3.7944    4.2429

clear;clc;
x = [2 4 7 11];
y = [1.1 2.1 3.1 4.8];
x0 = [3 5 8 9 10];
y0 = interp1(x,y,x0,'spline')
plot(x, y, '*', x0, y0, 's')

在这里插入图片描述

二维插值

clear;clc;
x = 0:0.1:1;
y = 0:0.2:2;
[x, y] = meshgrid(x, y);
z = x.^2 + y.^2;
z1=interp2(x,y,z,0.5,0.5,'spline')

%z1 =0.5000

拟合

%拟合
p = poyfit(x,y,n)
y0 = polyval(p,x0)
clear;clc;
x = linspace(0,2*pi,50);
y = sin(x);
p1 = polyfit(x,y,3);
y1 = polyval(p1,x);
p2 = polyfit(x,y,5);
y2 = polyval(p2,x);
plot(x,y,'r',x,y1,'b:',x,y2,'y')

在这里插入图片描述

#### 参考学习视频

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值