特殊符号
表达式
式子 | 功能 |
---|
syms x; y = x^2 | 表达式:定义x为符号变量,可用于数学运算 |
function f = x^2 | 函数表达式 |
f = @(x)x^2 | 匿名函数表达式 |
绘图
标签
函数 | 功能 |
---|
title(s,str) | 图形标题,s为subplot分割的当前面板,默认没有,str是要显示的标题 |
legend(p1,p2,…) | p1、p2等等,是要显示的曲线的label名称,legend中p的顺序,就代表plot显示顺序 |
hold on | 开启,保持在当前面板的所有图形 |
hold off | 关闭,刷新面板,显示当前最新的图形, 默认值是这个 |
没有使用hold on 时
x = 0:0.1:10;
y1 = x*2;
plot(x,y1,"c");
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d26d5eb37b2a2f258f02e38a2ef63392.png)
x = 0:0.1:10;
y2 = exp(x);
plot(x,y2,"g");
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a9bef9664770aaad82b7354fd6be46c4.png)
x = 0:0.1:10;
y1 = x*2;
plot(x,y1,"c");
y2 = 10*log(x);
plot(x,y2,"g");
你想象中(理想很丰满)
![](https://i-blog.csdnimg.cn/blog_migrate/21500de883fd70a5179f25695a402fa5.png)
其实(显示很骨感)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/126b3bba1a48671d70546e107f2a0406.png)
这时候就要使用我们的hold on 了
x = 0:0.1:10;
y1 = x*2;
plot(x,y1,"c");
hold on
y2 = 10*log(x);
plot(x,y2,"g");
总体用法概括实现
s1 = subplot(2,1,1); %将图形显示面板分割成2*1,当前显示在第1个
hold on; %开启当前面板的图形重叠显示功能
x = 0:0.1:10;
y1 = x*2;
plot(x,y1,"c");
hold on
y2 = 10*log(x);
plot(x,y2,"g");
legend("x*2","10*log(x)")
title(s1,"tile_1")
% 面板二
s2 = subplot(2,1,2); %当前显示在第2个
hold on; %开启当前面板的图形重叠显示功能
x = 0:0.1:20;
y = exp(x);
plot(x,y,"c");
legend("exp(x)")
title(s2,"tile_2")
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/84835fc5863a3f373d0cd043275718b2.png)
二维绘图函数
函数 | 功能 |
---|
subplot(m,n,p) | 切分视图窗口为m*n个,该函数以下(区域内,下一个subplot之前)的图都显示在第p个上 |
plot(x,f,c) | 绘制数据点图;x为矩阵数据,c表示曲线颜色或者显示的符号 |
fplot(f, x) | 绘制光滑的曲线;f是函数表达式,不是矩阵;x是区间不是矩阵,用数学符号表示如:[1,3] |
ezplot(y, x) | 同fplot,不过这是符号函数的绘图函数,图上会显示y的表达式 |
高级运算函数
函数 | 功能 |
---|
diff(y,n) | 求符号表达式的n阶导数,默认一阶 |
solve(y==value) | 求解方程式,如y=x*2,求y=4时,x的解,使用eval()可以将符号解转成数值解 |
dsolve(str1,str2,…) | 求解微分方程,str是符号表达式 |
lsqcurvefit(y, b, xdata, ydata) | 残差拟合函数,b为要拟合的系数矩阵,返回 [b, resnorm] |
nlinfit(y, b, xdata, ydata) | 拟合函数,返回 b |
lsqcurvefit(y, b, xdata, ydata)用法举例
代码
t = [0.25 0.5 0.75 1 1.5 2 2.5 3 3.5 4 4.5 5 6 7 8 9 10 11 12 13 14 15 16];
C = [30 68 75 82 82 77 68 68 58 51 50 41 38 35 28 25 18 15 12 10 7 7 4];
plot(t, C, "x");
hold on;
Q0 = 21700*2; %初始化参数
C0 = 0;
b = [1 2];
f = @(b, t)(exp(-b(2)*t)*(420*C0*b(1) - 420*C0*b(2) + Q0*b(1)))/(420*(b(1) - b(2))) - (Q0*b(1)*exp(-b(1)*t))/(420*(b(1) - b(2)));
[b, resnorm] = lsqcurvefit(f, b, t, C);
disp(b); %拟合得到的参数: b=[ 2.0261 0.1842]
%参数代入f
b = [2.0261 0.1842];
fy = @(t)(exp(-b(2)*t)*(420*C0*b(1) - 420*C0*b(2) + Q0*b(1)))/(420*(b(1) - b(2))) - (Q0*b(1)*exp(-b(1)*t))/(420*(b(1) - b(2)));
fplot(fy, [0,17], "b");
拟合结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2abb7140fd163c2047456fe1a0dcaa3c.png)
其中的“x”就是数据的分布,曲线是拟合得到的结果
我第一次建模使用matlab,就总结到这啦,以后发现新的功能会再写的,有写的不对的地方,希望大伙可以告诉我,非常乐意接受大家的指正。