您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 改进欧拉法和龙格库塔法C&C++程序
1)改进欧拉法求解常微分方程的初值问题#includestdio.hdoublefunc(doublex,doubley){return(y-x);}doubleeuler(doublex0,doublexn,doubley0,intN){doublex,y,yp,yc,h;inti;x=x0;y=y0;h=(xn-x0)/(double)N;for(i=1;i=N;i++){yp=y+h*func(x,y);x=x0+i*h;yc=y+h*func(x,yp);y=(yp+yc)/2.0;}return(y);}main(){doublex0,xn,y0,e;intn;printf(\ninputn:\n);scanf_s(%d,&n);printf(inputx0,xn:\n);scanf_s(%f,%f,&x0,&xn);printf(inputy0:\n);scanf_s(%f,&y0);e=euler(x0,xn,y0,n);printf(y(%f)=%6.4f,y0,e);}inputn:20inputx0,xn:1,6inputy0:2y(2.000000)=7.0000Pressanykeytocontinue(2)四阶龙格—库塔法#includestdio.hdoublefunc(doublex,doubley){return(x-y);}doublerunge_kutta(doublex0,doublexn,doubley0,intN){doublex,y,y1,y2,h,xh;doubled1,d2,d3,d4;inti;x=x0;y=y0;h=(xn-x0)/(double)N;for(i=1;i=N;i++){xh=x+h/2;d1=func(x,y);d2=func(xh,y+h*d1/2.0);d3=func(xh,y+h*d2/2.0);d4=func(xh,y+h*d3);y=y+h*(d1+2*d2+2*d3+d4)/6.0;x=x0+i*h;}return(y);}main(){doublex0,xn,y0,e;intN;printf(\ninputn:\n);scanf_s(%d,&N);printf(inputx0,xn:\n);scanf_s(%f,%f,&x0,&xn);printf(inputy0:\n);scanf_s(%f,&y0);e=runge_kutta(x0,xn,y0,N);printf(y(%f)=%8.6f,y0,e);}inputn:10inputx0,xn:1,2inputy0:5y(5.000000)=2.833863Pressanykeytocontinue
本文标题:改进欧拉法和龙格库塔法C&C++程序
链接地址:https://www.777doc.com/doc-1742973 .html