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


/* ex5-1.c Forward Euler Method is used for solving y'=f(y,t) of first order
 * ordinary differential equation with initial condition y(0)=y0  known.
 */
#include <stdio.h>
#include <math.h>

double  F(double x, double y)
{
    return (-y+x+1);


void main()
{
    int i,n;
    double h=0.2 , y,x,y0=1,x0=0,x1=1;
    n=(x1-x0)/h ;

    y=y0;
    x=x0;
    printf("x         y(t)         真實解      abs(y(t)-真實解)   \n");
    printf("===========================================================\n");
    printf("%.2lf  %10.6lf    %10.6lf         %10.6lf    \n",x,y,exp(-x)+x , fabs(y-(exp(-x)+x)) );
    for(i=1;i<=n;i++)
    {
        y=y+h*F(x,y);
        x=x+h;
        printf("%.2lf  %10.6lf    %10.6lf         %10.6lf    \n",x,y,exp(-x)+x , fabs(y-(exp(-x)+x)) );
    }
    return;
}


輸出畫面
x         y(t)         真實解      abs(y(t)-真實解) 
===========================================================
0.00    1.000000      1.000000           0.000000   
0.20    1.000000      1.018731           0.018731   
0.40    1.040000      1.070320           0.030320   
0.60    1.112000      1.148812           0.036812   
0.80    1.209600      1.249329           0.039729   
1.00    1.327680      1.367879           0.040199   

Command exited with non-zero status 6

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...