2019年5月12日 星期日

C語言 例題5-1利用向前Euler尤拉 向前近似法 解一階常微分方程ODE y' = -y+x+1 0<= x <= 1 , y(0)=1 , h=0.2

C語言 例題5-1利用向前Euler尤拉 向前近似法 解一階常微分方程ODE y' = -y+x+1 0<= x <= 1 , y(0)=1 , h=0.2

/***********************************************
****EULER METHOD FOR DIFFERENTIAL EQUATIONS*****
***********************************************/
#include<stdio.h>
#include<math.h>
/*Define the RHS of the first order differential equation here(Ex: dy/dx=f(x,y))  */
double f(double x, double y){
return -y+x+1;
}
int main()
{
int i;
double y,xi,yi,xf,h;
yi=1;
xi=0;
xf=1;
h=0.2;

printf("x\t\ty\t\ty'\t\thy'\t\ty+hy'\n");
printf("_________________________________________________\n");
//Begin Euler Routine
while(xi<=xf){
y=yi+h*f(xi,yi);
printf("%0.2lf\t%0.4lf\t%0.4lf\t%0.4lf\t%0.4lf\n",xi,yi,f(xi,yi),h*f(xi,yi),y);
yi=y;
xi=xi+h;
}
return 0;
}


輸出畫面
y y' hy' y+hy'
_________________________________________________
0.00 1.0000 0.0000 0.0000 1.0000
0.20 1.0000 0.2000 0.0400 1.0400
0.40 1.0400 0.3600 0.0720 1.1120
0.60 1.1120 0.4880 0.0976 1.2096
0.80 1.2096 0.5904 0.1181 1.3277
1.00 1.3277 0.6723 0.1345 1.4621

沒有留言:

張貼留言

ESP32 (ESP-IDF in VS Code) MFRC522 + MQTT + PYTHON TKinter +SQLite

 ESP32 (ESP-IDF in VS Code) MFRC522 + MQTT + PYTHON TKinter +SQLite  ESP32 VS Code 程式 ; PlatformIO Project Configuration File ; ;   Build op...