2用Python实现内推外插法

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

用Python实现内推外插法Author:ZDate:2015-12-3importnumpyasnpclassnode(object):def__init__(self):self.power=[]self.a=[]defset(self,ar,p):foriinxrange(len(ar)):self.a.append(ar[i])self.power.append(p[i])defprintf(self):print'a[]is:',foriinxrange(np.array(self.a).shape[0]):printself.a[i],print''print'power[]is:',foriinxrange(np.array(self.power).shape[0]):printself.power[i],print''#############################basefunctionarea#####################defparse(self,string):parsestrstringintolistaandpowerameansthefactorofxpowermeanstheindexofxself.power=[]self.a=[]string=string.replace('','')eq=string.find('=')new_string=string[(eq+1):]index=new_string.find('x')while(index!=-1):ifnew_string[index-1]=='*'andnew_string[index-2].isdigit():i=index-2num=whilei=0andnew_string[i].isdigit():num=new_string[i]+numi=i-1ifi=0andnew_string[i]=='-':num='-'+numself.a.append(float(num))else:self.a.append(1.)ifnew_string[index+1]=='^':num=0.i=index+2whileilen(new_string)andnew_string[i].isdigit():num=10*num+float(new_string[i])i=i+1self.power.append(num)else:self.power.append(1.)index=new_string.find('x',index+1)ifnew_string[-1]!='x':ll=len(new_string)-1num=whilell=0andnew_string[ll].isdigit():num=new_string[ll]+numll=ll-1ifll=0andnew_string[ll]=='-':num='-'+numself.a.append(float(num))self.power.append(0.)defget_fun_value(self,num):ret=0.foriinxrange(np.array(self.a).shape[0]):ret+=self.a[i]*pow(num,self.power[i])returnret#######################toolfunctionarea##############defMin_push_forward(self,start,step):ifself.get_fun_value(start)self.get_fun_value(start+step):step_len=-1*stepx1=startx2=start+stepy2=self.get_fun_value(x2)whileTrue:x3=x2+stepy3=self.get_fun_value(x3)ify2y3:x1=x2x2=x3else:breakreturnx1,x3defMin_insert_forword(self,lef,rig,max_epochs,min_error):epoch=0whileTrue:mid1=(3.*lef-rig)/2.mid2=(lef+rig)/2.y1=self.get_fun_value(mid1)y2=self.get_fun_value(lef)y3=self.get_fun_value(mid2)y4=self.get_fun_value(rig)ify1=y2andy1=y3andy1=y4:lef=mid1rig=lefelify2=y1andy2=y3andy2=y4:lef=mid1rig=mid2elify3=y4andy3=y1andy3=y2:lef=lefrig=rigelse:lef=mid2rig=rigepoch+=1ifepoch=max_epochsor(rig-lef)=min_error:breakx=(lef+rig)/2.y=self.get_fun_value(x)returnx,ydefMax_push_forward(self,start,step):ifself.get_fun_value(start)self.get_fun_value(start+step):step_len=-1*stepx1=startx2=start+stepy2=self.get_fun_value(x2)whileTrue:x3=x2+stepy3=self.get_fun_value(x3)ify2y3:x1=x2x2=x3else:breakreturnx1,x3defMax_insert_forword(self,lef,rig,max_epochs,min_error):epoch=0whileTrue:mid1=(3.*lef-rig)/2.mid2=(lef+rig)/2.y1=self.get_fun_value(mid1)y2=self.get_fun_value(lef)y3=self.get_fun_value(mid2)y4=self.get_fun_value(rig)ify1=y2andy1=y3andy1=y4:lef=mid1rig=lefelify2=y1andy2=y3andy2=y4:lef=mid1rig=mid2elify3=y4andy3=y1andy3=y2:lef=lefrig=rigelse:lef=mid2rig=rigepoch+=1ifepoch=max_epochsor(rig-lef)=min_error:breakx=(lef+rig)/2.y=self.get_fun_value(x)returnx,ydefmin_fun(self,start,step,max_epochs,min_error):[lef,rig]=self.Min_push_forward(start,step)x,y=self.Min_insert_forword(lef,rig,max_epochs,min_error)print'x:',xprint'y:',ydefmax_fun(self,start,step,max_epochs,min_error):[lef,rig]=self.Max_push_forward(start,step)x,y=self.Max_insert_forword(lef,rig,max_epochs,min_error)print'x:',xprint'y:',yif__name__=='__main__':ss='f(x)=x^3-2*x+1'a=node()a.parse(ss)a.printf()max_epochs=1000min_error=0.001start=0.0step=0.2a.min_fun(start,step,max_epochs,min_error)

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功