MATLAB实验第八次上机.docx
《MATLAB实验第八次上机.docx》由会员分享,可在线阅读,更多相关《MATLAB实验第八次上机.docx(25页珍藏版)》请在冰豆网上搜索。
MATLAB实验第八次上机
1
(1)
>>roots([1020-15])
ans=
1.73205080756887
0.00000000000000+2.23606797749979i
0.00000000000000-2.23606797749979i
-1.73205080756888
>>vpa(ans,6)
ans=
-1.73205
-8.32667e-17+2.23607*i
-8.32667e-17-2.23607*i
1.73205
(2)
M文件
functiony=fun(x)
y=x^4+2*x^2-15;
>>x=fsolve('fun',1.7)
Optimizationterminated:
first-orderoptimalityislessthanoptions.TolFun.
x=
1.73205080756901
vpa(x,6)
x=
1.712051
>>x=fsolve('fun',-1.7)
Optimizationterminated:
first-orderoptimalityislessthanoptions.TolFun.
x=
-1.73205080756899
vpa(x,6)
x=
1.73205
>>x=fsolve('fun',2i)
Optimizationterminated:
first-orderoptimalityislessthanoptions.TolFun.
x=
0.00000000000010+2.23606797751316i
>>vpa(x,6)
ans=
1.0e-13+2.23607*i
>>x=fsolve('fun',-2i)
Optimizationterminated:
first-orderoptimalityislessthanoptions.TolFun.
x=
0.00000000000010-2.23606797751316i
>>vpa(x,6)
ans=
1.0e-13-2.23607*i
(3)
>>x=solve('x^4+2*x^2-15=0')
x=
3^(1/2)
-3^(1/2)
i*5^(1/2)
-i*5^(1/2)
ans=
1.73205
-1.73205
2.23607*i
-2.23607*i
…………………………………………………………….
2.
解析解
>>s='x-sin(x)-exp(x)*cos(x)'
s=
x-sin(x)-exp(x)*cos(x)
>>x=solve(s)
x=
1.4621214800914779232240323515507
>>vpa(x,6)
ans=
1.46212
数值解
>>x=fsolve('x-sin(x)-exp(x)*cos(x)',1.4)
Equationsolved.
fsolvecompletedbecausethevectoroffunctionvaluesisnearzero
asmeasuredbythedefaultvalueofthefunctiontolerance,and
theproblemappearsregularasmeasuredbythegradient.
x=
1.462121480439321
>>vpa(x,6)
ans=
1.46212
迭代法
M文件fun2.m
clear
x0=1.4;
x1=sin(x0)+exp(x0)*cos(x0);
whileabs(x1-x0)>=0.00005
x0=x1;
x1=sin(x0)+exp(x0)*cos(x0);
end
x1
结果
>>fun2
x1=
1.462126234771429
>>vpa(x1,6)
ans=
1.46213
二分法
clear
a=0;b=1.45;
n=0;
whileb-a>=0.00005
x=(a+b)/2;
n=n+1;
fa=a-sin(a)-exp(a)*cos(a);
fx=x-sin(x)-exp(x)*cos(x);
iffa*fx<0
b=x;
else
a=x;
end
end
x,n=n-1
结果
>>fun1
x=
1.449955749511719
n=
14
>>vpa(x,6)
ans=
1.44996
另一个解略
……………………………………………………………………
3.
>>s='exp(-x)-sin(pi*x/2)+log(x)';
>>x=solve(s)
x=
1.5493516167540306801165507666627
>>vpa(x,6)
ans=
1.54935
……………………………………………………………….
4.
>>a=[-1,0,1;-4,3,1;1,3,2]
a=
-101
-431
132
>>det(a)
ans=
-18
>>p=poly(a)
p=
1.0000-4.0000-3.000018.0000
>>p=poly2str(p,'x')
p=
x^3-4x^2-3x+18
>>eig(a)
ans=
-2.0000
3.0000+0.0000i
3.0000-0.0000i
>>[y,r]=eig(a)
y=
-0.5774-0.2357+0.0000i-0.2357-0.0000i
-0.5774-0.2357-0.0000i-0.2357+0.0000i
0.5774-0.9428-0.9428
r=
-2.000000
03.0000+0.0000i0
003.0000-0.0000i
>>R=rank(a)
R=
3
>>n=inv(a)
n=
-0.1667-0.16670.1667
-0.50000.16670.1667
0.8333-0.16670.1667
…………………………………………………..
5.
临时文件
>>fun1=inline('x^3+3*sin(-x/3)-23');
>>fun2=inline('x^3+3*x-23');
>>x1=fun1(pi),x2=fun1(5),x3=fun1(3.4)
x1=
5.408200468946500
x2=
99.013776126744702
x3=
13.586511915207851
>>x1=fun2(pi),x2=fun2(5),x3=fun2(3.4)
x1=
17.431054641069196
x2=
117
x3=
26.503999999999991
匿名函数
>>fun1=@(x)x^3+3*sin(-x/3)-23;
>>fun2=@(x)x^3+3*x-23;
>>x1=fun1(pi),x2=fun1(5),x3=fun1(3.4)
x1=
5.408200468946500
x2=
99.013776126744702
x3=
13.586511915207851
>>x1=fun2(pi),x2=fun2(5),x3=fun2(3.4)
x1=
17.431054641069196
x2=
117
x3=
26.503999999999991
永久文件
fun1.m
functiony=fun1(x)
y=x^3+3*sin(-x/3)-23;
fun2.m
functiony=fun2(x)
y=x^3+3*x-23;
结果
>>x1=fun1(pi),x2=fun1(5),x3=fun1(3.4)
x1=
5.408200468946500
x2=
99.013776126744702
x3=
13.586511915207851
>>x1=fun2(pi),x2=fun2(5),x3=fun2(3.4)
x1=
17.431054641069196
x2=
117
x3=
26.503999999999991
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
6.
>>a=[1,4,7,20;2,5,8,11;3,-6,9,12];
>>b=[1;3;3];
>>c=rank(a),d=rank([a,b])
c=
3
d=
3
>>u=null(sym(a))
u=
161/12
5/6
-21/4
1
>>x=a\b
x=
0
0.0373
0.5652
-0.1553
>>u=sym(a)\sym(b)
警告:
Systemisrankdeficient.Solutionisnotunique.
u=
25/12
1/6
-1/4
0
>>
7.
>>x=[1,1.4,1.8,2.2,2.6,3,3.4,3.8]
x=
1.00001.40001.80002.20002.60003.00003.40003.8000
>>y=[2,-1.448,-1.664,-0.056,1.968,3,20.144,0.5520]
y=
2.0000-1.4480-1.6640-0.05601.96803.000020.14400.5520
>>x1=[1.25,2.3,2.9,3.66]
x1=
1.25002.30002.90003.6600
>>y1=interp1(x,y,x1,'linear')
y1=
-0.15500.45002.74207.4092
>>y2=spline(x,y,x1)
y2=
-0.64640.71901.205716.1500
>>y1=interp1(x,y,x1,'spline')
y1=
-0.64640.71901.205716.1500
>>x=[1,1.4,1.8,2.2,2.6,3,3.4,3.8,1.25,2.3,2.9,3.66]
x=
Columns1through9
1.00001.40001.80002.20002.60003.00003.40003.80001.2500
Columns10through12
2.30002.90003.6600
>>y=[2,-1.448,-1.664,-0.056,1.968,3,20.144,0.5520,-0.6464,0.7190,1.2057,16.1500]
y=
Columns1through9
2.0000-1.4480-1.6640-0.05601.96803.000020.14400.5520-0.6464
Columns10through12
0.71901.205716.1500
>>p=polyfit(x,y,4)
p=
-6.950262.2790-192.3240242.1436-105.2824
>>poly2str(p,'x')
ans=
-6.9502x^4+62.279x^3-192.324x^2+242.1436x-105.2824
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
8.
(1)
>>symsx
>>s=int(exp(-x^2),-1,5)
s=
(pi^(1/2)*(erf
(1)+erf(5)))/2
>>vpa(s,11)
ans=
1.6330510583
(2)
>>h=0.000001;x=-1:
h:
5;
>>y=exp(-x.^2);
>>s=h*sum(y)
s=
1.633051242203520
>>vpa(s,5)
ans=
1.6331
(3)
>>h=0.000001;x=-1:
h:
5;
>>y=exp(-x.^2);
>>s=h*trapz(y)
s=
1.633051058263760
>>vpa(s,5)
ans=
1.6331
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
9.
(1)
>>symst
>>s=int(exp(-0.5*t)*sin(t+pi/6),0,pi*3)
s=
((2*3^(1/2)+1)*(exp(-(3*pi)/2)+1))/5
>>vpa(s,11)
ans=
0.90084078782
(2)
>>h=pi/1000;t=0:
h:
pi*3;
>>y=exp(-0.5.*t).*sin(t+pi/6);
>>s=h*trapz(y)
s=
0.900840276606885
(3)
>>y='exp(-0.5.*t).*sin(t+pi/6)';
>>s=quad(y,0,3*pi)
s=
0.900840811006463
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
10.
>>y=dsolve('D3y=-y','y(0)=1,Dy(0)=0,D2y(0)=0')
y=
exp(-t)/3+(2*exp(t/2)*cos((3^(1/2)*t)/2))/3
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
11.
>>fun=@(x,y)y-2*x/y;
>>[x,y]=ode23(fun,[0,1],[1])
x=
0
0.080000000000000
0.180********0000
0.280000000000000
0.380000000000000
0.480000000000000
0.580000000000000
0.680000000000000
0.780000000000000
0.880000000000000
0.980000000000000
1.000000000000000
y=
1.000000000000000
.0770********
1.166********0293
1.249017917208636
1.326676081253747
1.400034750953813
1.469738236869182
1.536284563268660
1.600068179713872
1.661407874348603
1.720565714193718
1.732154879417795
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
。
12.
数值解
>>fun=@(x,y)[y
(2);-6*cos(x)];
ode23(fun,[-10,10],[5,0])
>>[x,y]=ode23(fun,[-10,10],[5,0])
x=
-10.000000000000000
-9.999984109419911
-9.999904656519465
-9.999507392017236
-9.997521069506089
-9.987589456950357
-9.937931394171693
-9.823011698264244
-9.660813474879619
-9.460729774500120
-9.228030874486807
-8.964651209392159
-8.669191555479403
-8.335338*********
-7.945026707902023
-7.534135580254955
-7.098590248274665
-6.686765918941736
-6.351116143428428
-6.064078404739998
-5.816834208814697
-5.676579279755624
-5.536324350696551
-5.373707097371709
-5.165727528773497
-4.909807675114976
-4.583508333543399
-4.276309013996998
-3.969109694450596
-3.785877313496457
-3.644209936421614
-3.502542559346770
-3.329902814028738
-3.121191612349518
-2.880652050645896
-2.609412677193195
-2.305074556406769
-1.959395503555049
-1.548052236437003
-1.007685620871909
-0.648329535937590
-0.288973451003272
0.026695537887512
0.300568916557177
0.535176937187460
0.723543961635943
0.877312726974181
.0775********
1.324344071307815
1.634484898385757
1.955463740046763
2.276442581707769
2.469137613521180
2.602773107181954
2.736408600842728
2.896537924347151
3.094745633376593
3.325768726230144
3.587505914509060
3.881161418314442
4.212702926788669
4.599180775684407
5.108837174059754
5.487848523711013
5.866859873362272
6.200978970945136
6.489433181994937
6.738394722201685
6.882797275400684
.027*********
7.189********5368
7.396379056423532
7.651474921421556
7.976146768180901
8.304280982143922
8.550804724211686
8.746234647689784
8.881549241653310
9.016863835616837
9.176********4311
9.373476519950946
9.603796908489754
9.864848957890619
10.000000000000000
y=
5.0000000000000000
5.0000000006356260.000080000412110
5.0000000228829070.000480014835347
5.0000006108989170.002480395940464
5.0000154768055770.012490016399744
5.0003887402727680.062729765988340
5.0098245486332430.318564990035898
.0816********
5.3079413752415691.861033444504114
5.7985550246162473.048473276894549
6.6699929290072004.437014602410657
.0379********
10.0111250073972837.378306529500154
12.6901253711142198.582014924615818
16.1963322498005139.238497672087590
19.9691544139016458.958461674000525
23.6170376367415717.630130032011948
26.3649160732226805.618043483159200
27.9279126974898523.668967408411018
28.7345333613971191.957557154672697
.0437********
.0714********
29.002319905912568-0.814292116839208
28.814731710854208-1.473350149009797
28.435981825387298-2.132********3079
27.819762614236211-2.621589449547680
26.936160651409942-2.688135353573485
26.175********5613-2.176********3862
25.651397927203703-1.155********2304
25.512240983073792-0.341307459132607
25.5136534848150910.372132415289034
25.6204153740232951.143482092578525
25.9030849179068532.139********6603
26.4791788365964393.384867129674014
27.4657960455782274.81039