2019年4月27日 星期六

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

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


/* 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>
#define  w(t)   (exp(-t)+t)

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

void main()
{
    int i,n;
    double h=0.2 , y,t,y0=1,t0=0,t1=1;
    n=(t1-t0)/h ;

    y=y0;
    t=t0;
    printf("t         y(t)         w(t)        error\n");
    printf("========================================\n");
    printf("%.2lf  %10.6lf  %10.6lf  %10.6lf\n",t,y,w(t),fabs(y-w(t)));
   
    for(i=1;i<=n;i++)
    {
        y=y+h*F(y,t);
        t=t+h;
        printf("%.2lf  %10.6lf  %10.6lf  %10.6lf\n",t,y,w(t),fabs(y-w(t)));
    }
    return;
}

輸出畫面
t         y(t)         w(t)        error
========================================
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...