数值分析上机作业.docx
《数值分析上机作业.docx》由会员分享,可在线阅读,更多相关《数值分析上机作业.docx(35页珍藏版)》请在冰豆网上搜索。
![数值分析上机作业.docx](https://file1.bdocx.com/fileroot1/2022-10/4/3b6f43ef-0582-4edb-b6a3-56a098dca279/3b6f43ef-0582-4edb-b6a3-56a098dca2791.gif)
数值分析上机实验报告
姓名:
班级:
学号:
1
第一次上机:
1.题目:
分别用不动点法和牛顿法求解方程
x-exp(x)+4=0
Fixedpointconvergetonegativeroot
Theprincipal:
anumberpisfixedpointforagivenfunctiongifg(p)=p.AndwecanchangeTheequation
tofixedpointform.asforfixedpointiflim
k®¥
pkmake
g(pk)=pk.
Wecanchangetheformto:
g1=exp(g0)-4
usetheinitialnumberas1
Thecode:
g0=1;
g1=exp(g0)-4;
whileabs(g1-g0)>0.00001
g0=g1;
g1=exp(g0)-4;
end
g1
theresults:
>>w1_fixed_point
g1=
2
-3.9813
结果分析
通过控制迭代式的形式可以决定收敛到正跟或负根。
Fixedpointconvergetopositiveroot
Theprincipal:
Wecanchangeanewfixedpointformtoproduceapositiveroot
Wecanchangetheformto:
g1=log(4+g0);
useinitialnumberas-3.9
Thecode:
g0=-3.9;
g1=log(4+g0);
whileabs(g1-g0)>0.00001
g0=g1;
g1=log(g0+4);
end
g1
theresults:
>>w1_fixed_point_positive
g1=
1.7490
Newton’smethodtofindnegativeroots
Theprincipal:
3
WederiveNewton’smethod,basedontaylorpolynomials.newton’s
methodisakindoffixedpointmethodwhichhastwoorderconvergence.
Wecanchangetheformto:
g1=g0-(g0-exp(g0)+4)/(1-exp(g0))
useinitialnuberas-3
Thecode:
g0=-3;
g1=g0-(g0-exp(g0)+4)/(1-exp(g0));
whileabs(g1-g0)>0.00001
g0=g1;
g1=g0-(g0-exp(g0)+4)/(1-exp(g0))
end
theresults:
>>w1_newton
g1=
-3.981342639636226
g1=
-3.981339370911418
结果分析:
收敛速度明显快于不动点(结果太长没打出)。
Newton’smethodtofindpositiveroots
Theprincipal:
Useinitialnumberas1
4
Thecode:
g0=1;
g1=g0-(g0-exp(g0)+4)/(1-exp(g0));
whileabs(g1-g0)>0.00001
g0=g1;
g1=g0-(g0-exp(g0)+4)/(1-exp(g0));
end
g1
theresults:
>>w1_newton
g1=
1.749031386012702
Multipleroots
结果分析:
通过确定初始值得正负来控制收敛到哪个根。
2.p100#9题
题目:
Useeachofthefollowingmethodstofindasolutionin[0.1,1]accurateto
within10^-4for
600*x^4-550*x^3+200*x^2-20*x-1=0
a.bisectionb.newton’smethod
c.secantmethodd.methodoffalseposition
e.mullers’smethed
5
a.Bisection
原理:
Themethodcallsforarepeatedhalvingofsubintervalsof[a,b]and,artificialeachstep,locating
thehalfcontainingtherootofequation.
代码:
Thecode:
%bisection
functionanser=Bisection(f,a,b)
tol=1.0e-3;
fa=subs(f,a);
fmeans=subs(f,(a+b)/2);
if(fa*fmeans>0)
t=(a+b)/2;
anser=Bisection(f,t,b);
else
if(fa*fmeans==0)
anser=(a+b)/2;
else
if(abs(b-a)<=tol)
anser=(b+3*a)/4;
6
else
s=(a+b)/2;
anser=Bisection(f,a,s);
end
end
end
运行结果:
>>Bisection('600*x^4-550*x^3+200*x^2-20*x-1',-5,5)
ans=
-0.035858154296875
b.newton’smethod
原理:
X=(600*x^4-550*x^3+200*x^2-1)/20
Useinitialnumberas1
代码:
g0=1;
g1=(600*g0^4-550*g0^3+200*g0^2-1)/20;
whileabs(g1-g0)>0.00001
g0=g1;
g1=g0-(g0-exp(g0)+4)/(1-exp(g0));
7
end
g1
结果:
>>w1_newton
g1=
1.749031386012702
c.secant
原理:
Useinterval[-5,5]
代码:
functionanser=Secant(f,a,b)
tol=1.0e-5;
error=0.1;
fa=subs(sym(f),a);
fb=subs(sym(f),b);
anser=a-(b-a)*fa/(fb-fa);
while(error>tol)
x1=anser;
fx=subs(sym(f),x1);
8
s=fx*fa;
if(s>0)
anser=b-(x1-b)*fb/(fx-fb);
else
anser=a-(x1-a)*fa/(fx-fa);
end
error=abs(anser-x1);
end
end
结果:
Secant('600*x^4-550*x^3+200*x^2-20*x-1',-5,5)
ans=
0.277662174145539
d.methodoffalseposition
原理:
Useinterval[-5,5]
代码:
%methodofFalsePosition
functionanser=Position(f,a,b)
tol=1.0e-5;
f1=subs(sym(f),findsym(sym(f)),a);
f2=subs(sym(f),findsym(sym(f)),b);
9
if(f1==0)
anser=a;
end
if(f2==0)
anser=b;
end
tol1=1;
r1=a;
r2=b;
fv=subs(sym(f),findsym(sym(f)),a);
while(tol1>tol)
f2=subs(sym(f),findsym(sym(f)),r2);
anser=r2-(r2-r1)*f2/(f2-fv);
fr=subs(sym(f),findsym(sym(f)),anser);
if(f2*fr<0)
tol1=abs(anser-r2);
r1=r2;
r2=anser;
fv=subs(sym(f),findsym(sym(f)),r1);
else
tol1=abs(anser-r2);
r2=anser;
10
fv=0.5*subs(sym(f),findsym(sym(f)),r1);
end
end
结果:
>>Position('600*x^4-550*x^3+200*x^2-20*x-1',-5,5)
ans=
0.277662174145539
结果分析:
牛顿法的收敛速度一般较快。
3.
题目:
应用newton法求f(x)的零点,e=10^-6
f(x)=x-sin(x)
再用求重根的方法求零点
1.
Newton'smethod
原理:
Theiterativeequation
g1=g0-(g0-sin(g0))/(1-cos(g0));
use1asinitialnumber
11
代码:
clear;
g0=1;
g1=g0-(g0-sin(g0))/(1-cos(g0));
whileabs(g1-g0)>0.000001
g0=g1;
g1=g0-(g0-sin(g0))/(1-cos(g0))
end
结果:
>>w3_newton
g1=
1.4990e-006
结果分析:
牛顿法在重根是为一介收敛,失去速度优势。
2.Mutiplerootsmethods1
原理:
g1=g0-(g0-sin(g0))/(1-cos(g0));
becausegisazerooff(x)ofmultiplicity3
sotheiterationequationis
12
g1=g0-3*(g0-sin(g0))/(1-cos(g0))
use1asinitialnumber
代码:
clear;
g0=1;
g1=g0-(g0-sin(g0))/(1-cos(g0));
whileabs(g1-g0)>0.000001
g0=g1;
g1=g0-3*(g0-sin(g0))/(1-cos(g0))
end
结果:
>>w3_mutiple_root
g1=
-0.0095
g1=
2.8751e-008
g1=
6.3996e-009
3.Mutiplerootsmethods2
原理:
g(x)=x–u(x)/u’(x)
=x–f(x)*f’(x)/f’(x)^2–f(x)*f’’(x)
代码:
13
functionanser=multiple(f,x0)
tol=1.0e-5;
df=diff(sym(f));
df2=