2020年3月29日20:37:15
今天利用plot函数来写了一个教程中的四次的函数,()= 4−3.53−2.52+14−6 from t = -2.5 to 4.0
function createfigure(X1, Y1)
%CREATEFIGURE(X1, Y1)
% X1: vector of x data
% Y1: vector of y data
% Auto-generated by MATLAB on 29-Mar-2020 20:33:55
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create plot
plot(X1,Y1,'Color',[0 0 0]);
% Create ylabel
ylabel('f(t)');
% Create xlabel
xlabel('t(/s)');
% Create title
title('test');
box(axes1,'on');
% Set the remaining axes properties
set(axes1,'XGrid','on','YGrid','on');
这一大段代码是用
t = -2.5:0.001:4;
y=t.^4-3.5*t.^3-2.5*t.^2+14*t-6;
plot(t,y)
做出图之后然后编辑,添加横纵两轴的信息和添加标题、更改线条颜色然后按"Generate Code"生成代码自动生成的。
而且,第一开始用t^4,他会报错,说道:
Error using ^
Incorrect dimensions for raising a matrix to a power.
Check that the matrix is square and the power is a scalar.
To perform elementwise matrix powers, use '.^'.
经过查询后,而且结合
>> t = 1:1:100
t =
Columns 1 through 10
1 2 3 4 5 6 7 8 9 10
Columns 11 through 20
11 12 13 14 15 16 17 18 19 20
Columns 21 through 30
21 22 23 24 25 26 27 28 29 30
Columns 31 through 40
31 32 33 34 35 36 37 38 39 40
Columns 41 through 50
41 42 43 44 45 46 47 48 49 50
Columns 51 through 60
51 52 53 54 55 56 57 58 59 60
Columns 61 through 70
61 62 63 64 65 66 67 68 69 70
Columns 71 through 80
71 72 73 74 75 76 77 78 79 80
Columns 81 through 90
81 82 83 84 85 86 87 88 89 90
Columns 91 through 100
91 92 93 94 95 96 97 98 99 100
以不加“;”分号的情况,他会展示内容,然后写了columns,是第几列第几列,意味着这是向量,然后是横着的,即一维。
.^2是矩阵中的每个元素都求平方,^2是求矩阵(此矩阵为方阵)的平方。
不带“.”的是方阵求幂,带“.”是常数求幂。
点乘(.*)、乘(*)。
>> a=[1 2;3 4];
b1=a.^2;
b2=a^2;
>> b1
b1 =
1 4
9 16
>> b2
b2 =
7 10
15 22
用set
>> grid
>> grid
>> set(gca,"ygrid","on")
>> set(gca,"ygrid","off")
这里grid意思是开启网格,再写一次grid就是关闭网格。当然,在这里,网格有x,y两个方向
MATLAB 中gcf、gca 以及gco 的区别:
1.三者的功能定义:
2.matlab下的图形对象:
图形对象是用以显示图形和用户界面元素的基本元素。下表列出了各种图形对象。
对象 | 描述 |
---|---|
Root | 对计算机屏幕最高级的对象 |
Figure | 用来显示图形和用户界面的窗口 |
Axes | 在窗口中显示图形的轴 |
Uicontrol | 用户界面控制。执行一个对用户交互作用的函数。 |
Uimenu | 用户定义窗口菜单 |
Uicontextmenu | 右键单击对象时弹出的菜单 |
Image | 二维像素基础图 |
Light | 影响斑点和表面对象着色的光源 |
Line | 用于诸如plot, plot3, semilogx 等函数的线 |
Patch | 带边缘的填充多边形 |
Rectangle | 二维形状,从矩形到椭圆 |
Surface | 由x-y 平面以上的数据值所作图创建的矩阵数据的三维表示 |
Text | 字符串 |
3. 图形对象级别
不同对象分布在一个树形结构级别里,反映它们的独立性。例如,线对象是在轴对象的框架下的。依次,轴对象只存在于figure 对象中。要想对gcf、gca 以及gco 之间的区别进一步的了解,MATLAB 中的句柄图形的了解是必要的,以下部分是MATLAB 句柄图形中关于gcf、gca 以及gco的部分,供大家参考。
以上引用自https://www.cnblogs.com/haore147/p/3633010.html
想画多个图
>> plot(t,f1)
>> hold on
>> plot(t,f2)
>> plot(t,f3)
画面太扭曲,要更改坐标轴的范围
>> x = -3*pi/2:0.001:3*pi/2;
>> y = tan(x);
>> plot(x,y)
>> axis([-3*pi/2 3*pi/2 -10 10])
仅重置一下坐标轴,x轴不变,y轴范围改为[-10, 10],即可看到熟悉的正切曲线图
Using the axis command after the plot statement will re-set the axis.
Note: The axis can also be re-set using plot tools.
官方教学文档是这样讲的 。
如果想在一个窗口下看到多个图像?
The third entry points to a particular sub-window. So, subplot(3,2,4) would divide the plot window into 3 rows and 2 columns allowing for 6 smaller plot windows and would point to the 4th sub-window (2nd row – 2nd column).
你可以先用
>> subplot(2,2,1)
来创建一个2*2的,一共会有4个区域的其中的第一行,第一列的一个位置。然后再去用plot函数画图像,再用axis等函数来修改这个图像。
如果想要将四个区域填满的话,更改subplot的第三个参数,重复劳动即可。