数值计算 第五章.docx
- 文档编号:29090929
- 上传时间:2023-07-20
- 格式:DOCX
- 页数:27
- 大小:93.28KB
数值计算 第五章.docx
《数值计算 第五章.docx》由会员分享,可在线阅读,更多相关《数值计算 第五章.docx(27页珍藏版)》请在冰豆网上搜索。
数值计算第五章
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
/**
*
*@author邓仕军
*@time2012-05-24
*@version0.1
*@function:
微分方程数值解法-改进的欧拉公式(预报-校正、嵌入公式、平均化形式)
*/
classProgram5_1{
Methodf;
doubleu_limit;
doublel_limit;
double[][]result;//结果值表
doublex0,y0;//初值
doubleh;//步长
publicProgram5_1(Methodf,doublel_limit,doubleu_limit,doublex0,
doubley0,doubleh){
this.f=f;
this.l_limit=l_limit;
this.u_limit=u_limit;
this.x0=x0;
this.y0=y0;
this.h=h;
//-----------初始化result结果集,(x0,y0)也存入其中---------------
result=newdouble[(int)((u_limit-l_limit)/h)+1][2];
result[0][0]=x0;
result[0][1]=y0;
}
//-------------------预报——矫正--------------------------
strictfpdouble[][]functionA(){
intn=(int)((this.u_limit-this.l_limit)/this.h)+1;
for(inti=1;i doubletemp=0d; this.result[i][0]=this.x0+i*this.h; try{ temp=this.result[i-1][1] +this.h *((Double)f.invoke(null,this.result[i-1][0], this.result[i-1][1])); this.result[i][1]=this.result[i-1][1]+this.h *((Double)f.invoke(null,this.result[i][0],temp)); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } }//for returnthis.result; }//functionA //-------------------嵌入公式-------------------------- strictfpdouble[][]functionB(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; for(inti=1;i //doubletemp=0d; this.result[i][0]=this.x0+i*this.h; try{ this.result[i][1]=this.result[i-1][1] +this.h /2 *((Double)f.invoke(null,this.result[i-1][0], this.result[i-1][1]) +(Double)f.invoke(null,this.result[i][0], this.result[i-1][1])+this.h *(Double)f.invoke(null, this.result[i-1][0], this.result[i-1][1])); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } }//for returnthis.result; }//functionB //-------------------平均化形式-------------------------- strictfpdouble[][]functionC(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; for(inti=1;i doubleYp=0d,Yc=0d;//Yp this.result[i][0]=this.x0+i*this.h; try{ Yp=this.result[i-1][1] +this.h *((Double)f.invoke(null,this.result[i-1][0], this.result[i-1][1])); Yc=this.result[i-1][1]+this.h *((Double)f.invoke(null,this.result[i][0],Yp)); this.result[i][1]=(Yp+Yc)/2; }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } }//for returnthis.result; }//functionC voidprint(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; System.out.printf("%8s","i: "); for(inti=0;i System.out.printf("%12s",i); } System.out.println(); this.functionA(); System.out.printf("%8s","X取值: "); for(inti=0;i System.out.printf("%12.5f",result[i][0]); } System.out.println(); System.out.println(); //-----------------预报-------校正---------- for(inti=0;i if(i==(int)(n/2-1)) System.out.printf("%12s","预报-------校正"); else System.out.printf("%12s","-------------"); } System.out.println(); System.out.printf("%8s","y=f(x)"); for(inti=0;i System.out.printf("%12.5f",result[i][1]); } System.out.println(); //-----------------嵌入公式---------- this.functionB(); for(inti=0;i if(i==(int)(n/2-1)) System.out.printf("%12s","---报嵌入公式---"); else System.out.printf("%12s","-------------"); } System.out.println(); System.out.printf("%8s","y=f(x)"); for(inti=0;i System.out.printf("%12.5f",result[i][1]); } System.out.println(); //-----------------平均化形式---------- this.functionC(); for(inti=0;i if(i==(int)(n/2-1)) System.out.printf("%12s","---平均化形式---"); else System.out.printf("%12s","-------------"); } System.out.println(); System.out.printf("%8s","y=f(x)"); for(inti=0;i System.out.printf("%12.5f",result[i][1]); } System.out.println(); } } publicclassTestProgram5_1{ staticdoublefX(doublex,doubley){ returny-2*x/y; } publicstaticvoidmain(String[]args){ try{ Program5_1pro5_1=newProgram5_1( TestProgram5_1.class.getDeclaredMethod("fX",double.class, double.class),0,1,0,1,0.1); pro5_1.print(); }catch(NoSuchMethodException|SecurityExceptione){ e.printStackTrace(); } } } 运行结果: importjava.lang.reflect.InvocationTargetException; importjava.lang.reflect.Method; /** * *@author邓仕军 *@time2012-05-28 *@version0.1 *@function: 四阶龙格-库塔法 */ classProgram5_2{ Methodf; doubleu_limit; doublel_limit; double[][]result;//结果值表 doublex0,y0;//初值 doubleh;//步长 publicProgram5_2(Methodf,doublel_limit,doubleu_limit,doublex0, doubley0,doubleh){ this.f=f; this.l_limit=l_limit; this.u_limit=u_limit; this.x0=x0; this.y0=y0; this.h=h; //-----------初始化result结果集,(x0,y0)也存入其中--------------- result=newdouble[(int)((u_limit-l_limit)/h)+1][2]; result[0][0]=x0; result[0][1]=y0; } strictfpdouble[][]functionA(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; doubleK1=0d,K2=0d,K3=0d,K4=0d; for(inti=1;i this.result[i][0]=this.x0+i*this.h; try{ K1=(Double)f.invoke(null,this.result[i-1][0], this.result[i-1][1]); K2=(Double)f.invoke(null,this.result[i-1][0]+h/2, this.result[i-1][1]+h/2*K1); K3=(Double)f.invoke(null,this.result[i-1][0]+h/2, this.result[i-1][1]+h/2*K2); K4=(Double)f.invoke(null,this.result[i-1][0]+h, this.result[i-1][1]+h*K3); this.result[i][1]=this.result[i-1][1]+this.h/6 *(K1+2*K2+2*K3+K4); }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){ e.printStackTrace(); } }//for returnthis.result; }//functionA voidprint(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; System.out.printf("%8s","i: "); for(inti=0;i System.out.printf("%12s",i); } System.out.println(); for(inti=0;i System.out.printf("%12s","-------------"); } this.functionA(); System.out.println(); System.out.printf("%8s","X取值: "); for(inti=0;i System.out.printf("%12.5f",result[i][0]); } System.out.println(); System.out.printf("%8s","y=f(x)"); for(inti=0;i System.out.printf("%12.5f",result[i][1]); } System.out.println(); } } publicclassTestProgram5_2{ staticdoublefX(doublex,doubley){ returny-2*x/y; } publicstaticvoidmain(String[]args){ try{ Program5_2pro5_2=newProgram5_2( TestProgram5_2.class.getDeclaredMethod("fX",double.class, double.class),0,1,0,1,0.1); pro5_2.print(); }catch(NoSuchMethodException|SecurityExceptione){ e.printStackTrace(); } } } 计算结果截图: importjava.lang.reflect.InvocationTargetException; importjava.lang.reflect.Method; /** * *@author邓仕军 *@time2012-05-31 *@version0.1 *@function: 线性多步法: Adams隐式求解公式说明: 用微分方程数值解法-改进的欧拉公式(预报-校正、嵌入公式、平均化形式) *求的前三个值加上已知的一个值 */ classAdams{ Methodf; doubleu_limit; doublel_limit; double[][]result;//结果值表 doublex0,y0;//初值 doubleh;//步长 publicAdams(Methodf,doublel_limit,doubleu_limit,doublex0, doubley0,doubleh){ this.f=f; this.l_limit=l_limit; this.u_limit=u_limit; this.x0=x0; this.y0=y0; this.h=h; //------结合---初始化result结果集,(x0,y0)也存入其中--------------- result=newdouble[(int)((u_limit-l_limit)/h)+1][2]; result[0][0]=x0; result[0][1]=y0; } //-----------结合----预报——矫正-------------------------- strictfpdouble[][]functionA(){ intn=(int)((this.u_limit-this.l_limit)/this.h)+1; for(inti=1;i doubletemp=0d; this.result[i][0]=this.x0+i*this.h; try{ if(i<4){ temp=this.result[i-1][1] +this.h *((Double)f.invoke(null,this.result[i-1][0], this.result[i-1][1])); this.result[i][1]=this.result[i-1][1] +this.h *((Double)f.invoke(null,this.result[i][0],temp)); }else{//具备前四个值了 temp=this.result[i-1][1] +this.h /24 *(55*this.result[i-1][1]-59 *this.result[i-2][1]+37 *this.result[i-3][1]-9*this.result[i-4][1]); this.result[i][1]=this.result[i-1][1] +this.h /24 *(9*temp+19*this.result[i-1][1]-5 *this.result[i-2][1]+this.result[i-3][1]); } }catch(IllegalAccessExceptione){ e.printStackTrace(); }catch(IllegalArgumentExceptione){ e.printStackTrace(); }catch(InvocationTargetExceptione){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数值计算 第五章 数值 计算 第五