实验一 MATLAB软件的使用
§ 初等代数
1.1 表达式的运算
1.一元多项式的运算
表1.1一元多项式常用的命令
![roots(p)ploy(p)polyval(p,x)polyder(p)[r,p,k]=residue(x,y)conv(x,y)deconv(x,y)](https://i-blog.csdnimg.cn/blog_migrate/37c9824986159daba2a081d6e9b74339.png)
>> p=[1,2,1];roots(p)
ans =
-1
-1
>> poly([1,2])
ans =
1 -3 2
>> [r,p,k]=residue([1,2,3,4],[1,-3,2])
r =
26
-10
p =
2
1
k =
1 5
2.一般符号表达式的运算
表1.2
![collect(f)expand(f)factor(f)horner(f)simple(f)simplify(f)subs(f)[n,d]=numden(f)](https://i-blog.csdnimg.cn/blog_migrate/cea279cbe7efafffad44d5ce9b33614b.png)
>> syms x y z a b
>> f=(x+y)*(x+2*y);collect(f)
ans =
x^2 + 3*x*y + 2*y^2
>> g=(a+b)*(a+2*b);collect(g)
ans =
a^2 + 3*a*b + 2*b^2
>> u=sym('(a-b)^2');a=x+y;b=z;v=subs(u)
v =
(x + y - z)^2
>> [n,d]=numden(x/y+y/x)
n =
x^2 + y^2
d =
x*y
1.2 方程求解
表1.3方程组求解的常用命令

>> syms a b c x
>> s=a*x^2+b*x+c;solve(s)
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
>> fzero(@sin,3)
ans =
3.141592653589793
>> q=solve('sin(x)-cos(x)')
q =
pi/4
>> s=solve('a*u^2+v^2','u-v=1','a,u')
s =
a: [1x1 sym]
u: [1x1 sym]
>> s.a
ans =
-v^2/(v + 1)^2
§2 微积分
表1.4

>> syms x n
>> limit(sin(x)/x)
ans =
1
>> diff(sin(n*x),x,3)
ans =
-n^3*cos(n*x)
>> int(log(x))
ans =
x*(log(x) - 1)
>> quad(@(x)4./(1+x.^2),0,1)
ans =
3.141592682924567
>> dsolve('Dy-y=1')
ans =
C4*exp(t) - 1
>> dsolve('Dy-y=1','y(0)=1')
ans =
2*exp(t) - 1
>> taylor(exp(x),x,0,'Order',5)
ans =
x^4/24 + x^3/6 + x^2/2 + x + 1
§3 线性代数
>> A=[1,2;8,7];inv(A)
ans =
-0.777777777777778 0.222222222222222
0.888888888888889 -0.111111111111111
>> [P,D]=eig(A)
P =
-0.707106781186547 -0.242535625036333
0.707106781186547 -0.970142500145332
D =
-1 0
0 9
>> log(A)
ans =
0 0.693147180559945
2.079441541679836 1.945910149055313
>> B=sym([1,2;8,7]);inv(B) %采用符号运算得到精确的结果
ans =
[ -7/9, 2/9]
[ 8/9, -1/9]
>> [P,D]=eig(B)
P =
[ -1, 1/4]
[ 1, 1]
D =
[ -1, 0]
[ 0, 9]
表1.5关于向量与矩阵的一些常用命令
![dot(a,b)cross(a,b)sum(a)prod(a)norm(a)length(a)A'trace(a)size(A)rank(A)det(A)inv(A)poly(A)eig(A)[P,D]=eig(A)f(A)](https://i-blog.csdnimg.cn/blog_migrate/aee81ae21d6799145cea6eb2090c4778.png)
§4 计算方法
4.1 插值
>> x=0:0.1:2;y=sin(x);
xi=0:0.1:2;yi=interp1(x,y,'spline')
yi =
1 至 11 列
-0.1000 -0.0900 -0.0801 -0.0704 -0.0611 -0.0521 -0.0435 -0.0356 -0.0283 -0.0217 -0.0159
12 至 21 列
-0.0109 -0.0068 -0.0036 -0.0015 -0.0003 -0.0000 -0.0008 -0.0026 -0.0054 -0.0091
>> zi=sin(xi);plot(xi,zi-yi)

4.2 拟合
下面的命令用来对数据进行多项式最小二乘拟合
>> x=1:10;y=log(x);f=polyfit(x,y,2)
f =
-0.03 0.53 -0.36
>> z=polyval(f,x);plot(x,y,'*',x,z,'-')

4.3 最优化
>> [x,y]=fminsearch(@(x)sin(x),5) %求sinx在5附近的最小值
x =
4.71
y =
-1.00
>> [x,y]=fminbnd(@(x)cos(x),0,5) %求cosx在【0,5】的最小值
x =
3.14
y =
-1.00
§5 MATLAB软件中的作图
5.1 二维作图
1.plot(y)
>> plot([2,3,4,7,11,13])

2.plot(x,y)
>> x=0:pi/50:2*pi;y=sin(x);plot(x,y)

表1.6 plot语句的各种常用选项

3.plot(x1,y1,‘s1’,x2,y2,‘s2’,…)
>> t=0:pi/50:2*pi;
>> x1=cos(t);y1=sin(t);
>> x2=2*cos(t);y2=2*sin(t);
>> x3=3*cos(t);y3=3*sin(t);
>> plot(x1,y1,x2,y2,x3,y3)

4.plot(X,Y,‘s’)
>> t=[0:pi/50:2*pi]';u=1:3;
>> X=cos(t)*u;Y=sin(t)*u;plot(X,Y)

5.图形控制
![常用的图形控制命令axis autoaxis equalaxis imageaxis onaxis offaxis([x1,x2,y1,y2])grid ongrid offhold onhold offtitle('name')xlabel('xtext')ylabel('ytext')subplot(m,n,k)](https://i-blog.csdnimg.cn/blog_migrate/57ec7534821d86307df4704ea3bd98c0.png)
6.ezplot(‘fun’,[xmin,xmax,ymin,ymax])
ezplot('x^2-2*y^2-1',[-6,6,-4,4])

5.2 三维曲线作图
1.plot3(x,y,z)
若x,y,z为相同长度的向量.该命令在三维空间中生成一条曲线,坐标对应于x,y,z的值.若x,y,z为相同维数的矩阵,则画出若干条空间曲线,数目等于矩阵的列数.
>> t=-8:0.1:8;x=6*cos(t);y=6*sin(t);z=3*t;
>> plot3(x,y,z);grid on

2.ezplot3(‘f’,‘f’,‘h’,[t1,t2])
ezplot3('6*cos(t)','6*sin(t)','3*t',[-8,8])

5.3 三维曲面作图
1.mesh(X,Y,Z) 与 surf(X,Y,Z)
>> x=-5:0.2:5;y=x;
[X,Y]=meshgrid(x,y);
>> Z=sin(sqrt(X.^2+Y.^2));
>> mesh(X,Y,Z);

2.ezmesh(‘f’,‘g’,‘h’,[u1,u2,v1,v2]) 与ezsurf(‘f’,‘g’,‘h’,[u1,u2,v1,v2])
>> ezmesh('cos(u)*cos(v)','sin(u)*cos(v)','sin(v)',[0,2*pi,-pi,pi]);axis equal;
>> ezsurf('cos(u)*cos(v)','sin(u)*cos(v)','sin(v)',[0,2*pi,-pi,pi]);axis equal;

本文详细介绍MATLAB软件在数学领域的应用,涵盖初等代数、微积分、线性代数、计算方法及图形绘制等内容。通过具体实例,展示如何在MATLAB中进行表达式运算、方程求解、极限、导数、积分、矩阵运算、插值、拟合、最优化、二维及三维图形绘制等操作。
763

被折叠的 条评论
为什么被折叠?



