1. 简单作图
x = -2 * pi : 0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x, y1, '-b');
hold on
plot(x, y2, '-r');
xlabel('x')
ylabel('y')
text(0,0,'(0,0)')
legend('sin x','cos x')

MATLAB 作图_图例

t = 0:pi/180:4*pi;
x = 16*sin(t).^3
y = 13 * cos(t) - 5*cos(2*t)...
-2*cos(3*t)- cos(4*t);
plot(x-3,y,'-r',x+3,y,'-b');
xlabel('x');
ylabel('y');
axis([-20,20,-20,15]);
title('Two Hearts')
legend('U','I')

MATLAB 作图_控制语句_02
曲线图 plot: plot(x,y); plot(x,y,s), plot(x1,y1,s1,x2,y2,s2,…)

b    blue           ·     point                    -        solid
g green o circle : dotted
r red x x-mark -. dashdot
c cyan + plus -- dashed
m magenta * star (none) no line
y yellow s square
k black d diamond
w white v triangle (down)
^ triangle (up)
< triangle (left)
> triangle (right)
p pentagram
h hexagram
  1. 简单控制语句
    title(图形名称)
    xlabel(x轴说明);ylabel(y轴说明)
    text(x,y, 图形说明)
    legend(图例1,图例2,…)
    grid on /grid off /grid minor
    axis([xmin xmax ymin ymax]),xlim([xmin,xmax])
  2. 其它坐标系
    对数坐标:loglog,semilogx
x = 10*2.^[0:6];
y = [100 150 225 340 ...
510 765 1150];
loglog(x,y,'.-r')
xlim([0.5e1,0.8e3])
ylim([0.8e2,1.4e3])
xlabel('x');
ylabel('y');

MATLAB 作图_控制语句_03
极坐标: polar

theta = 0:pi/180:4*pi;
r = 1-sin(theta);
polar(theta,r,'-r');

MATLAB 作图_图例_04
4. 三维曲线图

t = 0:pi/50:10*pi;
x = sin(t);
y = cos(t);
z = t;
plot3(x, y, z)
title('Helix')
xlabel('sin t')
ylabel('cos t')
zlabel('t')
grid on

MATLAB 作图_5e_05

[x,y] = meshgrid(-pi : 0.1 : pi);
z = sin(x).*cos(y);
mesh(x, y, z)
surf(x,y,z)
xlabel('x')
ylabel('y')
zlabel('z')
title('sin x sin y')

MATLAB 作图_5e_06

  1. 三维曲面图:补充函数 meshgrid
[x, y] = meshgrid(1:3, 1:3);
x
y

MATLAB 作图_图例_07

[x, y] = meshgrid(1:3, 1:3);
x
y
rsq = (x-2).^2 + (x-2).^2

MATLAB 作图_控制语句_08

[x, y] = meshgrid(1:3, 1:3);
x
y
rsq = (x-2).^2 + (x-2).^2
r = sqrt(rsq)

MATLAB 作图_5e_096. M函数格式
function [output 1, …] = functionname(input1, …)

MatLab command 1;
MatLab command 2;

function area = rectarea(L, W)
area = L .* W