1、matlab操作题答案P.28 ex 2.1 数值变量的运算 format short e clear muw0=1.785e-3; a=0.03368; b=0.000221; t=0:20:80; muw=muw0./(1+a*t+b*t.2)muw = 1.7850e-003 1.0131e-003 6.6092e-004 4.6772e-004 3.4940e-004p.31 ex 2.2 数值驻足和字符串转换 a=1:5; b=num2str(a); aa = 1 2 3 4 5 bb =1 2 3 4 5 b*2ans =98 64 64 100 64 64 102 64 64 1
2、04 64 64 106P.44 ex 2.9 比较用左除和右除分别求解恰定方程(线性方程组如果方程数等于未知数个数,叫做恰定方程组,如果方程多于未知数,叫做超定方程组,反之称为欠定。换个角度说,系数矩阵如果是方阵,就是恰定方程组)的解见课本P.48 ex 2.14 计算矩阵的指数 并比较不同函数的结果 b=magic(3); expm(b)ans = 1.0e+006 * 1.0898 1.0896 1.0897 1.0896 1.0897 1.0897 1.0896 1.0897 1.0897 expmdemo2(b)ans = 1.0e+006 * 1.0898 1.0896 1.089
3、7 1.0896 1.0897 1.0897 1.0896 1.0897 1.0897 expm1(b)ans = 1.0e+003 * 2.9800 0.0017 0.4024 0.0191 0.1474 1.0956 0.0536 8.1021 0.0064 expmdemo3(b)ans = 1.0e+006 * 1.0898 1.0896 1.0897 1.0896 1.0897 1.08971.0896 1.0897 1.0897P50 ex 2.18 计算矩阵的特征值条件数 a=rand(3)a = 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218
4、 0.9706 0.8003 0.9157 V,D,s=condeig(a)V = 0.4913 0.6696 0.6696 0.3158 -0.4476 + 0.2831i -0.4476 - 0.2831i 0.8117 -0.2332 - 0.4655i -0.2332 + 0.4655iD = 1.8146 0 0 0 0.2757 + 0.3061i 0 0 0 0.2757 - 0.3061is = 1.1792 1.2366 1.2366P62 ex 2.29矩阵的抽取、三角抽取 a=pascal(4)a = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20
5、diag(a,-2)ans = 1 4 v=diag(diag(a)v = 1 0 0 0 0 2 0 0 0 0 6 0 0 0 0 20%diag简单来说就是抽取矩阵各对角线上的元素,如上是抽取主对角线以下第二条对角线之元素,其另一功能是建立对角矩阵 tril(a)ans = 1 0 0 0 1 2 0 0 1 3 6 0 1 4 10 20 triu(a)ans = 1 1 1 1 0 2 3 4 0 0 6 10 0 0 0 20%triu&tril用法与diag非常类似,用途是提取下、上三角矩阵P62 ex2.30 建立多项式之伴随矩阵这道题有点凌乱了.求解释P64 ex 2.31
6、数组的幂运算 a=2 1 -3 -1;3 1 0 7;-1 2 4 -2;1 0 -1 5; a3ans = 32 -28 -101 34 99 -12 -151 239 -1 49 93 8 51 -17 -98 139 a.3ans = 8 1 -27 -1 27 1 0 343 -1 8 64 -8 1 0 -1 125P66 ex 2.32 数组之逻辑运算 a=1:3;4:6;7:9; b=0 1 0;1 0 1;0 0 1; x=5;y=ones(3)*5; x ab=a&bab = 0 1 0 1 0 1 0 0 1%此处为与运算,就是同真才为真(同为非零数) bans = 1 0
7、 1 0 1 0 1 1 0%逻辑非运算,即全都非,真变假假变真;还有逻辑或运算,看下面即懂: a|bans = 1 1 1 1 1 1 1 1 1%总结多项式运算的函数:poly: Polynomial with specified roots特征多项式的生成p=poly(a)a为n阶特征矩阵,所得一般为n阶特征多项式;poly2sym数值2符号;polyval 求多项式的值;roots 求多项式的根;conv 多项式的乘法(向量之卷积)conv(p,d);polyder 多项式微分;polyfit 多项式拟合。P71 ex 2.41 多项式拟合,用5阶多项式对正弦函数进行最小二乘拟合 x=
8、0:pi/20:pi/2; y=sin(x); a=polyfit(x,y,5); x1=0:pi/30:pi*2; y1=sin(x1); y2=a(1)*x1.5+a(2)*x1.4+a(3)*x1.2+a(4)*x1.2+a(5)*x1+a(6); plot(x1,y1,b-,x1,y1,r*) legend(原曲线,拟合曲线) axis(0,7,-1.2,4) axis(0,7,-1.2,1.5) %调整坐标轴显示范围 2.符号运算命名和基本运算法则P 79 符号矩阵的运算 A=sym(a,b;c,d) A = a, b c, d syms a b c d B=a,b;c,d B =
9、a, b c, d B-A ans = 0, 0 0, 0 A/B ans = 1, 0 0, 1 A2 ans = a2 + b*c, a*b + b*d a*c + c*d, d2 + b*c A.2 ans = a2, b2 c2, d2 det(A) ans = a*d - b*c %行列式 inv(A) ans = d/(a*d - b*c), -b/(a*d - b*c) -c/(a*d - b*c), a/(a*d - b*c) %逆 rank(A) ans = 2%秩%SUMMARY 矩阵的分解:奇异值分解 U,S,V=svd(A);特征值分解V,D=eig(A);正交分解Q,
10、R=qr(A);三角分解L,U=lu(A);P88 ex 3.7 利用函数gradient绘制一个矢量图 x,y=meshgrid(-2:.2:2,-2:.2:2); z=x.*exp(-x.2-y.2); px,py=gradient(z,.2,.2); contour(z),%等高线绘制函数 hold on quiver(px,py)%矢量图绘制函数 %绘图SUMMARY 1.二维plot 不解释;fplot:绘制y=f(x)图形 fplot(fname,lims,s);ezplot:绘制隐函数图形 help吧;极坐标polar,对数坐标semilogx,semilogy,loglog;b
11、ar 条形图,pie饼状图;hist 直方图 有些疑惑!;grid on/off:控制是否画网格线。box on/off: 控制是否加边框线。hold on/off 控制是否刷新当前轴及图形%2.三维:plot3; meshgrid函数:产生平面区域内的网格坐标矩阵;mesh 画格子;surf 面;figure(n)开窗户;subplot:割图。3.二维图形函数运用P98 基本绘图命令 y=rand(100,1); plot(y) x=rand(100,1); z=x+y.*i; plot(z) P101 ex4.1 绘制带有显示属性的二维属性 x=1:0.1*pi:2*pi; y=sin(x
12、); z=cos(x); plot(x,y,-k,x,z,-.rd) P104 ex4.5 条状图和矢量图 x=1:10; y=rand(10,1); bar(x,y) x=:0.1*pi:2*pi; x=0:0.1*pi:2*pi; y=x.*sin(x); feather(x,y) P104 ex4.6 函数图形绘制 lim=0,2*pi,-1,1; fplot(sin(x),cos(x),lim) P105 ex 4.7 绘制饼状图 x=2,4,6,8; pie(x,math,english,chinese,music) 4.三维图形函数应用P107 ex4.9 绘制三维螺旋线 x=0:
13、pi/50:10*pi; y=sin(x);z=cos(x); plot3(x,y,z) P107 ex 4.10 绘制参数为矩阵的三维图 x,y=meshgrid(-2:0.1:2,-2:0.1:2); z=x.*exp(-x.2-y.2); plot3(x,y,z) P109 ex 4.11 使用mesh函数绘制三维面图 x=-8:0.5:8;y=x; a=ones(size(y)*x; b=y*ones(size(x); c=sqrt(a.2+b.2)+eps; z=sin(c)./c; mesh(z) P110 ex4.13 meshc函数绘制的三维面图 X,Y=meshgrid(-4
14、:0.5:4); Z=sqrt(X.2+Y.2); meshc(Z) P111 ex 4.16 绘制三维饼状图 clear x=2,4,6,8; pie(x) pie(x,0,0,1,0) pie3(x,0,0,1,0) pie3(x,0,0,1,1) P113 ex 4.19 绘制如图柱面图 x=0:pi/20:pi*3; r=5+cos(x); a,b,c=cylinder(r,30); mesh(a,b,c) P113 ex 4.20 绘制地球表面的气温分布示意图 a,b,c=sphere(40); t=abs(c); surf(a,b,c,t); axis(equal) axis(sq
15、uare) colormap(hot) 5.图形控制命令P118 ex 4.24 坐标标注函数应用 x=1:0.1*pi:2*pi; y=sin(x);plot(x,y) xlabel(x(0-2pi) ylabel(y=sin(x) title(正弦函数,FontSize)Error using title (line 29)Incorrect number of input argumentsError in title (line 23) h = title(gca,varargin:); title(正弦函数,FontSize,12) title(正弦函数,FontSize,12,Fo
16、ntWeight,bold) %课本上不知由于版本问题还是什么,与2011b不同,最后尝试用Bold成功解决 ADD 已解决,用单引号引起bold即可Undefined function or variable bold. title(正弦函数,FontSize,12,FontWeight,Bold)Undefined function or variable Bold. title(正弦函数,FontSize,12,FontWeight,Bold) title(正弦函数,FontSize,12,FontWeight,Bold,FontName,隶书) P123 ex 4.30 在同一张途中绘
17、制几个三角函数图 x=0:0.1*pi:2*pi; y=sin(x); z=cos(x); plot(x,y,-*) hold on plot (x,z,-o) plot(x,y+z,-+) legend(sin(x),cos(x),sin(x)+cos(x),0) P124 ex 4.31 在4个子图中绘制不同的三角函数 x=0:0.1*pi:2*pi; subplot(2,2,1); plot(x,sin(x),-*) title(sin(x) subplot(2,2,2) plot(x,cos(x),-o) title(cos(x) subplot(2,2,3) plot(x,sin(x
18、).*cos(x),-x) title(sin(x)*cos(x) subplot(2,2,4) plot(x,sin(x)+cos(x),-h) title(sin(x)+cos() title(sin(x)+cos(x) % SUMMARY interp1:1-D data interpolation (table lookup) % yi = interp1(x,Y,xi,method,extrap) method:nearest/linear/echip(hermite)/spline,etcP222 ex 7.3 正弦曲线的差值实例 x=0:0.05:10; y=sin(x); xi
19、=0:.125:10; yi=interp1(x,y,xi);%一维插值 plot(x,y,*,xi,yi)P227 例7.7二次拟合 x=0.5:0.5:3.0; y=1.75 2.45 3.81 4.80 8.00 8.60; a=polyfit(x,y,2)%用最小二乘法拟合a = 0.4900 1.2501 0.8560 x1=0.5:0.05:3.0; y1=a(1)*x1.2+a(2)*x1+a(3);%拟合出的函数 plot(x1,y1,-or,x,y,-+b)P228 例7.8拟合,用求解矩阵的方法解,求解超定方程 xi=19:6:44xi = 19 25 31 37 43 y
20、i=19.0 32.3 49.0 73.3 98.8; format short a=polyfit(xi,yi,2)a = 0.0635 -0.5932 7.2811 x1=19:.1:44; y1=a(1)*x1.2+a(2)*x1+a(2);%此处发生了一点错误,将a(3)误输为a(2),后面会有改正 plot(x1,y1,-x) plot(x1,y1,-) x2=xi.2;%采用求解矩阵的方法来求解此拟合问题 x2=ones(5,1),x2x2 = 1 361 1 625 1 961 1 1369 1 1849 ab=x2yiab = -1.3522 0.0540 y3=ab(1)+a
21、b(2)*x1.2; hold on;plot(x1,y3,-g) y1=a(1)*x1.2+a(2)*x1+a(3); plot(x1,y1,-)%从图中可以看出,两种拟合方法较为吻合P233 例7.10 分别用矩形和梯形公式求积分 y=inline(exp(-0.5*t).*sin(t+pi/6);%内联函数 d=pi/1000; t=0:d:3*pi; nt=length(t)nt = 3001 y1=y(t); sc=cumsum(y1)*d; scf=sc(nt)scf = 0.9016 format long scfscf = 0.901618619310013%矩形方法 z=tr
22、apz(y1)*dz = 0.900840276606885%梯形方法P237 ex7.11 采用自适应Simpson公式求积分 f=inline(x./(x.2+4); quad(f,0,1)ans = 0.111571765994935P237 ex 7.12 用quadl求积分 f=inline(exp(-x/2); f1=quadl(f,1,3)f1 = 0.7668 f2=quad(f,1,3,1e-10)f2 =0.7668 format long e f1f1 = 7.668009991284686e-001 f2f2 =7.668009991284349e-001P237 7.
23、12 quadl gauss-labatto 求积分 quadl(exp(-x/2),1,3)ans = 0.7668%求线性方程组的方法一般有两种:左除求法和linsolve求法P246 ex 7.17 求线性方程组 a=rand(4,4);%生成随机矩阵 b=rand(4,1); x=abx = 1.728190838792039e+001 8.395388076704506e-001 -1.590669694986821e+0011.088276299038511e+000%此处与书上不同,为生成的随机矩阵%SUMMARY矩阵左除 AB =A-1* B 等效于A*X=B求X inv(A)
24、 注:.数组左除 A.B Bij/Aij ; /:矩阵右除 A/B =A*B-1 等效于X*B=A求X format short x1=linsolve(a,b)%SUMMARY用linsolve求解线性方程;用solve、fzero求解非线性方程;Dsolve Ordinary differential equation and system solver,用dsolve求解微分方程;fsolve Solve system of nonlinear equations, x,fval = fsolve(myfun,x0,options) % Call solverx1 = 17.2819 0
25、.8395 -15.9067 1.0883P246 ex 7.18 对矩阵进行LU分解 A=ones(4,4)A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 l,u=lu(A)l = 1 0 0 0 1 1 0 0 1 0 1 0 1 0 0 1u = 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 l*uans = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1P265 ex 7.39 求方程组之符号解 x1,x2=solve(x1-0.7*sin(x1)-0.2*cos(x2)=0,x2-0.7*cos(x1)+0.2*sin(x2)
26、=0) x1 = 0.52652262191818418730769280519209 x2 = 0.50791971903684924497183722688768%方法一:用solve求符号解,注意使用x1,x2方式调用才可求得具体数值 另有不动点迭代法和newton法,见课本p263editfc.mfunction f=fc(x)f(1)=x(1)-0.7*sin(x(1)-0.2*cos(x(2);f(2)=x(2)-0.7*cos(x(1)+0.2*sin(x(2);f=f(1) f(2);%编写m文件 x0=0.5 0.5; fsolve(fc,x0)Equation solved
27、.fsolve completed because the vector of function values is near zeroas measured by the default value of the function tolerance, andthe problem appears regular as measured by the gradient.ans = 0.5265 0.5079%用fsolve求解,显然麻烦很多P273 ex 7.42 微分方程数值解f=inline(-y+x+1); x,y=ode23(f,0,1,1)x = 0 0.1000 0.2000 0.3000
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1