MATLAB TRICKS
如何在MATLAB中画一个矩形
函数直击
plot
line
这里用到的其实plot更基础的画法,关于点和线的画法
plot(xdata,ydata),plot画图时一般要传入想要的数据点,或者说是点集,即(xdata,ydata)
这里不难想到这两个向量的维度要匹配
plot(1:9)
这里只使用了一个参量,是因为默认xdata是从1开始,步长为1,相当于plot(1:1:length(1:9),1:9)

有了以上的经验,我们不难画出一个矩形

我们来关注一下它的形成过程

这样 ,很容易就能确认xdatas和ydatas
代码如下:
>> x1 = [-10 10 10 -10 -10];
>> y1 = [5 5 -5 -5 5];
>> x2 = [-5 5 5 -5 -5];
>> y2 = [2 2 -2 -2 2];
>> plot(x1,y1,x2,y2)
>> axis([-20 20 -20 20])
之前卖了一个关子,line函数也有同样的功能,只不过它只允许传入一组数据点
废话不多说,一起来看一看
x1 = [-10 10 10 -10 -10];
y1 = [5 5 -5 -5 5];
x2 = [-5 5 5 -5 -5];
y2 = [2 2 -2 -2 2];
line(x1,y1)
hold on
line(x2,y2)
axis([-20 20 -20 20])

接下来我们来画一个倒立摆
在画之前,我们来画一个圆,我们规定它的圆心为(5,5),半径为3

接下来
%% plot the arm of the pendulum
x1 = [0 4];
y1 = [0 4];
plot(x1,y1)
axis([-6 6 -4 8])
hold on
%% plot the rectangles
x2 = [-3 3 3 -3 -3];
y2 = [0 0 -3 -3 0];
plot(x2,y2)
%% plot the three circles
t = 0:0.01:2*pi;
x3 = 4 + 0.5*sin(t);
y3 = 4 + 0.5*cos(t);
x4 = 2 + 0.5*sin(t);
y4 = -3.5 + 0.5*cos(t);
x5 = -2 + 0.5*sin(t);
y5 = -3.5 + 0.5*cos(t);
plot(x3,y3,x4,y4,x5,y5)
axis equal
