2019年4月28日 星期日

C語言 例題5-4 二階 Runge-Kutta 解ODE y'= -y + t +1 , 0<=t <= 1 , y(1)=?

C語言 例題5-4  二階 Runge-Kutta 解ODE y'= -y + t +1  ,  0<=t <= 1  ,  y(1)=?
#include<stdio.h>
#include <math.h>

/*
Enter the value of x0: 0
Enter the value of y0: 2
Enter the value of h: 0.05
Enter the value of last point: 0.1
*/
//dy/dx = y - x
#define F(x,y)  (-y+x+1)
void main()
{
  double y0,x0,y1,n,h,f,f1,k1,k2;
  int i=0;

  x0=0.0;
  y0=1.0;
  h=0.2;
  n=1.0;
  printf("\nEnter the value of x0:%1.2lf",x0 );
  printf("\nEnter the value of y0:%1.2lf",y0 );
  printf("\nEnter the value of h:%1.2lf",h );
  printf("\nEnter the value of last point:%1.2lf",n);

  for(; x0<n; x0=x0+h)
  {
    f=F(x0,y0);
    k1 = h * f;
    f1 = F(x0+h,y0+k1);
    k2 = h * f1;
    y1 = y0 + ( k1 + k2)/2;
    printf("\n i  = %2d ",i);
    printf("\n k1 = %.4lf ",k1);
    printf("\n k2 = %.4lf ",k2);
    printf("\n y(%.4lf) = %.3lf ",x0+h,y1);
    y0=y1;
    i++;
  }
}


輸出畫面
Enter the value of x0:0.00
Enter the value of y0:1.00
Enter the value of h:0.20
Enter the value of last point:1.00
 i  =  0
 k1 = 0.0000
 k2 = 0.0400
 y(0.2000) = 1.020
 i  =  1
 k1 = 0.0360
 k2 = 0.0832
 y(0.4000) = 1.080
 i  =  2
 k1 = 0.0641
 k2 = 0.1169
 y(0.6000) = 1.170
 i  =  3
 k1 = 0.0860
 k2 = 0.1432
 y(0.8000) = 1.285
 i  =  4
 k1 = 0.1031
 k2 = 0.1637
 y(1.0000) = 1.418

沒有留言:

張貼留言

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

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