Matlab简单实例学习.docx
Matlab程序代码v=-7li=e-,sjsin(75)'7J5的函数图象t=0:0.02:10;fl=10sqrt(7.75).*exp(-1.5*t);f2=sin(sqrt(7.75).*t);y=fl.*f2;plot(t,y,'-k',t,y,'ok');XIabeI(t);ylabel('y(t)%title('函数图像)axis(-210-0.52)Q1拉氏变换clear;clc;symsstfslfs2fs3ftlft2ft3;1.=I,C=0.1,R=1.535;hl=l(1.*C*s2+R(l)*C*s+l);h2=1/(1.*C*s2+R(2)*C*s+1);h3=1/(1.*C*s2+R(3)*C*s+1);fsl=hl*(ls);fs2=h2*(ls);fs3=h3*(ls);ft1=ilaplace(fs1,s,t);ft2=ilaplacc(fs2,s,t);ft3=ilaplace(fs3,s,t);ezplot(t,ftl);holdon;czplot(t,ft2);holdon;ezplot(t,ft3);xtioiiio100i)进行编码。clear;clc;c=l1011101001fori=l:length(c)ifi=ldl(i)=0;d2(i)=0;elseifi=2dl(i)=c(i-l);d2(i)=c(i-l);elseifi=3d1(i)=mod(c(i-l)+c(i-2),2);d2(i)=c(i-l);elsed1(i)=mod(c(i-l)+c(i-2),2);d2(i)=mod(c(i-l)+c(i-3),2);endenddld2迭代法运用一般迭代法求解方程的3/=,解。第一根:clear;clc;x=IOjerr=I;while(err>10-6)Fl=xO;F2=log(3*x02);err=abs(Fl-F2);xO=F2;endx其次根:clear;clc;x=1;err=1;While(err>10-6)Fl=x;F2=sqrt(exp(x)/3);err=abs(Fl-F2);x=F2;end第三根:clear;clc;x=10;err=l;while(err>10-6)Fl=x;F2=-sqrt(exp(x)3);err=abs(Fl-F2);x=F2;end牛顿迭代法运用牛顿迭代法求解方程"-4+3-6=0的解。SymSXfxfx1;fx=2*x3-4*x2+3*x-6;fxl=diff(fx)err=l;k=l;xO=1.5;while(err>10A-6&&k<=1000)x1=x-(subs(fx,x,x)/(subs(fx1,x,x)x=xl;err=abs(x-xl)k=k+l;endx牛顿迭代法求解运用牛顿迭代法求解方程/-=sin1的解。第一解:symsXfxfxl;fx=x.3-sin(x)-l2*x+1;fxl=diff(fx);err=l;k=l;xO=-4;while(err>10A-6&&k<=1000)x1=x-(subs(fx,x,x)/(subs(fx1,x,x);er=abs(x-xl);x=xl;k=k+l;endkxx三-3.4912»其次解:symsxfxfxl;fx=x.3-sin(x)-12*x+l;fxl=diff(fx);err=1;k=1;xO=O;while(err>10-6)&&(k<=lOOO)x1=x-(subs(fx,x,x)(subs(fx1,x,x);err=abs(x-xl);x=xl;k=k+l;endx第二解:symsxfxfxl;fx=x.3-sin(x)-l2*x+l;fxl=diff(fx);err=l;k=l;xO=4;while(err>10A-6)&&(k<=lOOO)x1=x-(subs(fx,x,x)/(subs(fx1,x,x);err=abs(x-xl);x=xl;k=k+l;endxx3.4101般迭代法求解运用一般迭代法求解方程-12=sinA-I的解oclear;clc;errl=l;err2=1;err3=1;x01=3;x02=0;x03=-3;k=3;while(errl>10-6)&&(err2>10-6)&&(err3>10-6)&&(k<1000)Fll=x01;F12=(sin(x01)+12*x01-l)(l3);errl=abs(FlI-F12);x01=F12;%第一解F21=x02;F22=(x023-sin(x02)+l)12;err2=abs(F21-F22);%其次解xO2=F22;F31=x03;F32=-sqrt(sin(x03)-l)x03+12);err3=abs(F31-F32);xO3=F32;%第三解k=k+l;endx=xlx02x03调用matlab函数求解方程。clear;clc;symsXab;p=x.2-a*x-4*b;r=solve(p,x);clear;%roots求解法clc;symsx;p=1000-12-3;r=roots(p);clear;%solve求解法clc;symsx;P=x.6-x.2+2*x-3;r=solve(p,x);f1.1238932S11762955476010811097327.60804677070454177795683814517971÷.88541124247455434577495883624571口-.46586916707607665574895492689981+1.1M13222394156806M023931300200-1.40824M5843322579201684754629251.ii-clear;clc;symsxf;f"(x)x.*sin(x)-l;x0=l;tol=le-6;zl=fzero(f,x);x0=2.7jtol=le-6;z2=fzero(f,x);z=zlz2clear;clc;%求非线性方程组的解symx;fun=x(l)-x(2)2,x(2)-cos(x(l),x=l,2f=fsolve(fun,xO)Ilfun三(x(l)-x(2>2,x(2)-cos(x<l)IOpti»izationterminated:first-orderoptnaltyislessthanoptions.TolFun.F0.64170.8011clear;clc;%求解非线性方程的解symsx;fun=sin(x)-log(x+0.1),;x=l;f=fsolve(fun,xO)2.1760矩阵基本操作Clearjclc;%加、减、乘省略,只需补零即可运算A=U234;2312;111-1;1O-2-6;B=5;6;7;81;C1=AB%左除C2=det()%求行列式C3=inv(八)%求逆C4=A3%求需C5=rank(八)%求秩C6=A,%求转置C7=rref(八)%行变换简CommandWindow4.vl+2.r,-x,=2求解方程3x,+2S=IO的解。11xi+3x2=8clear;clc;A=42-1;3-12;1130);b=10;rref(八)Rl=rank(八)R2=rank(A,b)CommandWindowans=1.000000.300001.0000-1.10000001.0000001.000000.3000-1.10000001.00002x+3y+z=4求解方程的解。3x+8y-2z=134.r-y+9=-6clear;clc;A=231O;1-240;38-2O;4-19O);b=|4;-5;13;-6;rref(八)rrcf(A,b11R2=rank(l,b)CommandWindow1O2OO1-1OOOOOOOOOans三!O2O-1O1-1O2OOOOOOOOOORl=2R2=2×3xl+2+j+.r4=5求解方程卜2;1.+4:l22x1-3x2-Xy-5x4=-23x1+x2+2xj+IIx4=Oclear;clc;A=l111;12-14;2-3-1-5;31211;b=(5;-2;-2;0);rref(八)rref(A,b)R1=rank(八)多项式拟合clear;clc;x=0:0.1:l;y=2.32.52.12.53.23.63.03.14.15.13.8;%输入数据pl,sl=polyfit(x,y,2);p2,s2=polyfit(x,y,3);p3,s3=polyfit(x,y,7);%求得多项式系数Pyl=polyval(pl,x);y2=polyval(p2,x);y3=polyval(p3,x);%由P得到X的多项式rl=corrcoef(y,yl)r2=corrcoef(y,y2)r3=corrcoef(y,y3)%计算相关系数plot(x,y,'.k1,x,yl,'.r',x,y2,.g',x,y3,1.bj;%图像中InSert->1.egend可加标注。5554.5-.4-,-.35.:.3-.251*t210102030405OG0708091最小二乘拟合functiony=f(x)%f文件定义t=0.250.511.523468;c=(19.2118.1515.3614.1012.989.327.455.243.01;y=c-x(l).*exp(-x(2).*t);cle