Euler's Method Numerical Methods
y' = x+2y 2<= x <= 3 , y(2)=3 , h=0.1
/* 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 (x+2*y);
}
void main()
{
int i,n;
double h=0.1 , y,x,y0=3,x0=2,x1=3;
n=(x1-x0)/h ;
y=y0;
x=x0;
printf("x y(t) \n");
printf("=====================\n");
printf("%.2lf %10.6lf \n",x,y);
for(i=1;i<=n;i++)
{
y=y+h*F(x,y);
x=x+h;
printf("%.2lf %10.6lf \n",x,y);
}
return;
}
輸出畫面
x y(t)
============
2.00 3.000000
2.10 3.800000
2.20 4.770000
2.30 5.944000
2.40 7.362800
2.50 9.075360
2.60 11.140432
2.70 13.628518
2.80 16.624222
2.90 20.229066
3.00 24.564880
Command exited with non-zero status 11
/* 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 (x+2*y);
}
void main()
{
int i,n;
double h=0.1 , y,x,y0=3,x0=2,x1=2.5;
n=(x1-x0)/h ;
y=y0;
x=x0;
printf("x y(t) \n");
printf("=====================\n");
printf("%.2lf %10.6lf \n",x,y);
for(i=1;i<=n;i++)
{
y=y+h*F(x,y);
x=x+h;
printf("%.2lf %10.6lf \n",x,y);
}
return;
}
輸出畫面
x y(t)
=====================
2.00 3.000000
2.10 3.800000
2.20 4.770000
2.30 5.944000
2.40 7.362800
2.50 9.075360
Command exited with non-zero status 6
沒有留言:
張貼留言