总结:
- 1、颜色:
MATLAB绘图的常见颜色有八种,分别是:蓝(b)、绿(g)、红(r)、青(c)、品红(m)、黄(y)、黑(k)、白(w)。
fill(x1,y1,'b') %用于创建彩色多变型。
c = [1,0.4,0.5]; fill(x1,y1,c) ; %三元组设置颜色 - 2、二维图像:
plot(x,y,'r') %画二维图像
hold on %在现有图上继续画
axis([x1,x2,y1,y2]) %显示的xy的范围
legend({'y1','y2'},'Location','northwest','NumColumns',1); %图例 - 3、三维图像:
mesh(X,Y,Z) 曲面图
surf(X,Y,Z) 曲面图,若取值很细,图形为黑色。 - 4、计算:
factorial(5) %阶乘
exp(x-2) %e的x-2次方
画图
1、画多边形
clear
clc
t = (1/16:1/8:1)'*2*pi;
x1 = cos(t)+1;
y1 = sin(t)+1;
x2=[3,4.5,4.5,3];
y2=[0,0,2,2];
x3=[5,6.5,6.5];
y3=[0,0,2];
x4=[7,8.5,8.5,8];
y4=[0,0,2,3];
x5=x1;
y5=y1-3;
x6=x2;
y6=y2-3;
x7=x3;
y7=y3-3;
x8=x4;
y8=y4-3;
fill(x1,y1,'b',x2,y2,'g',x3,y3,'r',x4,y4,'c',x5,y5,'m',x6,y6,'y',x7,y7,'k',x8,y8,'w')
axis([0,10,-4,4])
可用ColorSpec的RGB三元组设定其他颜色。
c = [1,0.4,0.5];
file(x,y,c);
2、画函数图
在一张图里同时画出: y=x2、y=x3、y=10sin(x)、y=3e^(x-2)
% y=x^2、 y=x^3、 y=10sin(x)、 y=3e^(x-2 )
clear
clc
x=-3*pi:pi/100:3*pi;
y1=x.^2;
y2=x.^3;
y3=10*sin(x);
y4=3*exp(x-2);
plot(x,y1,'r')
hold on
plot(x,y2,'g')
plot(x,y3,'m')
plot(x,y4,'b')
%axis equal
axis([-3*pi,3*pi,-400,400])
xlabel('x')
ylabel('y')
title('函数y1=x^2,y2=x^3,y3=10sin(x),y4=3*exp(x-2)的图像')
legend({'y1','y2','y3','y4'},'Location','northwest','NumColumns',1);
hold off
3、画三维图
编写M文件,绘制下面分段函数的曲面图
function y = piecewise(x1,x2)
%绘制分段函数的曲面图
for i = 1:length(x1)
for j = 1:length(x2)
x = x1(i) +x2(j);
if x > 1
y(i,j) = 0.5457*exp(-0.75*x2(j)^2-3.75*x1(i)^2-1.5*x1(i));
elseif x > -1 && x < 1
y(i,j) = 0.7575*exp(-x2(j)^2-6.*x1(i)^2);
elseif x < -1
y(i,j) = 0.5457*exp(-0.75*x2(j)^2-3.75*x1(i)^2+1.5*x1(i));
end
end
end
y
调用
x1 = -2:0.1:2;
x2 = -2:0.1:2;
y = piecewise(x1,x2);
mesh(x1,x2,y)
1、计算阶乘
function S = calculate(m)
%计算:S=1!+2!+3!+4!+5!
S = 0;
for i=1:m
S = S + factorial(i);
end
S