实验一 MATLAB软件的使用

实验一 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)

>> 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)

>> 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方程组求解的常用命令
roots(p)solve(s)solve(s,v)solve(s1,s2,...,sn)solve(s1,s2,...,sn,v1,v2,...,vn)fzero(fun,x0)fsolve(funs,x0)

>> 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
limit(f)limit(f,a)diff(f)diff(f,t)diff(f,n)diff(f,t,n)int(f,t)int(f,t,a,b)quad(f,a,b)dblquad(f,xmin,xmax,ymin,ymax)talor(f,n,v,a)dsolve('eqn','con','v')

>> 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)

§4 计算方法

4.1 插值

>> x=0:0.1:2;y=sin(x);
xi=0:0.1:2;yi=interp1(x,y,'spline')

yi =

  111-0.1000   -0.0900   -0.0801   -0.0704   -0.0611   -0.0521   -0.0435   -0.0356   -0.0283   -0.0217   -0.0159

  1221-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)

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;

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值