资源描述:
《c语言编写求解常微分方程初值问题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、本文档利用C语言编写求解常微分方程的方法,包括改进欧拉方法求解常微分方程的初值问题,以及四阶龙格-库塔法求解常微分方程的初值问题。程序均采用txt文件输入输出方式。/*改进欧拉方法求解常微分方程的初值问题*/#include#include#include#definef(x,y)(y-2*x/y)voidmain(){FILE*fp1;/*依次输入初值、步长、下限、上限*/floatx,h,l,f0,f1,f2;if((fp1=fopen("in.txt","r"))==NULL){pr
2、intf("Can'topenthisfile!");exit(0);}fscanf(fp1,"%f%f%f%f",&f0,&h,&x,&l);fclose(fp1);FILE*fp2;if((fp2=fopen("out.txt","w"))==NULL){printf("Can'topenthisfile!");exit(0);}while(x<=l)/*改进欧拉法算法*/{f1=f0+h*f(x,f0);x=x+h;f2=f0+h*f(x,f1);f0=0.5*(f1+f2);fprintf(fp2,"x=%f,f=%f",
3、x,f0);/*输出运算结果*/}fclose(fp2);}forthequalityofreviewsandreview.Article26threview(a)theCCRAcompliance,whethercopiesofchecks;(B)whetherdoubleinvestigation;(C)submissionofprogramcompliance,investigationorexaminationofwhetherviewsareclear;(D)theborrower,guarantorloans/*四阶龙格-库塔法求
4、解常微分方程的初值问题*/#include#include#includefloatf(floatx,floaty)/*定义一个函数*/{floatf;f=(y-2*x/y);returnf;}voidmain(){intn,N;/*定义各变量*/floatx0,y0,h,x1,y1,l;floatk1,k2,k3,k4;FILE*fp1;/*依次输入初值、步长、下限、上限*/if((fp1=fopen("in.txt","r"))==NULL){printf("Can'topenthis
5、file!");exit(0);}fscanf(fp1,"%f%f%f%f",&y0,&h,&x0,&l);fclose(fp1);FILE*fp2;if((fp2=fopen("out.txt","w"))==NULL){printf("Can'topenthisfile!");exit(0);}for(n=1;x06、/6*(k1+2*k2+2*k3+k4);fprintf(fp2,"x%d=%3.1f,y%d=%f",n,x1,n,y1);x0=x1;y0=y1;forthequalityofreviewsandreview.Article26threview(a)theCCRAcompliance,whethercopiesofchecks;(B)whetherdoubleinvestigation;(C)submissionofprogramcompliance,investigationorexaminationofwhetherviewsar
7、eclear;(D)theborrower,guarantorloans}fclose(fp2);}forthequalityofreviewsandreview.Article26threview(a)theCCRAcompliance,whethercopiesofchecks;(B)whetherdoubleinvestigation;(C)submissionofprogramcompliance,investigationorexaminationofwhetherviewsareclear;(D)theborrower,guara
8、ntorloans