基本信息
源码名称:rk法解常微分方程并输出误差
源码大小:0.50KB
文件格式:.cpp
开发语言:C/C++
更新时间:2021-03-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
rk法解常微分方程并输出误差

#include<stdio.h>

#include<math.h>

#define n 10

#define f(x,y) -y 2*cos(x)

#define y(x) cos(x) sin(x)

int main()

{

double a=0,b=3.1415926,x,y,h;

double K1,K2,K3,K4,y0=1;

double yxn;

x=a,y=y0;

h=(b-a)/n;

int k=0;

printf("xn R-K法解yn 精确解yxn 误差|yxn-yn|\n");

while(k<n)

{

K1=f(x,y);

K2=f(x h/2,y h/2*K1);

K3=f(x h/2,y h/2*K2);

K4=f(x h,y h*K3);

y=y h/6*(K1 2*K2 2*K3 K4);

x=x h;

yxn=y(x);

k ;

printf("x=%lf,y=%.9lf,yxn=%.9lf,yxn-y=%.3e\n",x,y,yxn,fabs(yxn-y));

}   

return 0;

}