资源描述:
《数值分析python代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、frommathimportlogfrommathimportsin,cosimportrandom#Q1defbifection(f,a,b,error,n):"""fstandsforthefunctionwewanttoapproximateto,whileaandbaretheleftandrightnumberoftheintervalwherethefeffectsin,andnmeansforthetimewehavetriedtomakeaapproximation""""""bifection
2、(f,0,1,10**-6,20)"""FA=f(a)foriinrange(n):p=a+(b-a)/2FP=f(p)ifFP==0or(b-a)/20:a=pelse:b=pdefiteration(f,p0,n,error):"""iteration(f,0.5,22,10**(-6))"""p=0foriinrange(n):p=(2.71828)**(-p0)ifabs(p-p0)3、f,p0,n,error):"""fastitertion(f,0.5,20,10**-6)"""p=0q0=p0foriinrange(n):p=(2.71828)**(-p0)q=p+g(p)/(1-g(p))*(p-q0)ifabs(q-q0)4、urnpelse:p0=pdefsecant(f,p0,p1,n,error):"""secont(f,0.5,1,6,10**-6)"""foriinrange(n):p=p0-f(p0)*(p1-p0)/(f(p1)-f(p0))ifabs(p-p0)5、)ifabs(p-p0)6、on(q2_eqp,q2_eqq,0,20,10**-12)#Q3defF(x,y):returnx**2+y**2-1defG(x,y):returnx**3-ydeff1(x):return2*xdeff2(x):return2*xdefg1(x):return3*x**2defg2(x):return-1defnewtonforsystem(p,q,x0,y0,error):"""newtonforsystem(F,G,0.8,0.6,error)[[1.0,0.0,0.137],[0.0,1.0,0.8
7、6301]]"""lst1=[[f1(x0),f2(x0)],[g1(x0),g2(x0)]]lst2=[-p(x0,y0)+f1(x0)*x0+f2(x0)*y0,-q(x0,y0)+g1(x0)*x0+g2(x0)*y0]lst=[[f1(x0),f2(x0),lst2[0]],[g1(x0),g2(x0),lst2[1]]]returngaussjordaneli(lst)#Q4defitermeth(lst1,lst2,n,x_lst):"""thisisaprogrammewhichstandsfor
8、thesimpleiterationmethodtosolvethesystemoflinearequation.wherethelstisanalogyforthematrix.lst1=[[8,1,2],[4,-10,-1],[-3,-2,5]],lst2=[12,21,16],error=10**-5answer:[1.0,-2.0,3.0]"""temp_lst=[0forxi