C語言 尤拉方法Euler Method 求一階常微分方程式ODEdy/dx=(x + y + xy) y(0.1) 的解
Euler Method :
In mathematics and computational science, the Euler method (also called forward
Euler method) is a first-order numerical procedurefor solving ordinary differential
equations (ODEs) with a given initial value.
Consider a differential equation dy/dx = f(x, y) with initialcondition y(x0)=y0
then succesive approximation of this equation can be given by:
In mathematics and computational science, the Euler method (also called forward
Euler method) is a first-order numerical procedurefor solving ordinary differential
equations (ODEs) with a given initial value.
Consider a differential equation dy/dx = f(x, y) with initialcondition y(x0)=y0
then succesive approximation of this equation can be given by:
y(n+1) = y(n) + h * f(x(n), y(n))where h = (x(n) – x(0)) / n
h indicates step size. Choosing smaller
values of h leads to more accurate results
and more computation time.
/* C Program to find approximation of a ordinary
differential equation using euler method.*/
#include <stdio.h>
#include <math.h>
// Consider a differential equation
// dy/dx=(x + y + xy)
float func(float x, float y)
{
return (x + y + x * y);
}
// Function for Euler formula
void euler(float x0, float y, float h, float x)
{
float temp = -0;
// Iterating till the point at which we
// need approximation
int i=0;
printf("%d , Initial Values y0 = 1 , x0 = 0; \n",i);
i++;
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
// y1 = y0 + h * f(x0, y0)
printf("%d , yn+1=yn + h* f(xn ,yn)= %0.4lf+ %0.4lf* %0.4lf= %0.4lf\n",i,temp,h,func(x0, temp),y );
x0 = x0 + h;
i++;
}
// Printing approximation
printf("Approximate solution at x = %0.3lf is %0.6lf ",x, y);
}
// Driver program
int main()
{
// Initial Values
float x0 = 0;
float y0 = 1;
float h = 0.025;
// Value of x at which we need approximation
float x = 0.1;
euler(x0, y0, h, x);
return 0;
}
輸出畫面
0 , Initial Values y0 = 1 , x0 = 0;
1 , yn+1=yn + h* f(xn ,yn)= 1.0000+ 0.0250* 1.0000= 1.0250
2 , yn+1=yn + h* f(xn ,yn)= 1.0250+ 0.0250* 1.0756= 1.0519
3 , yn+1=yn + h* f(xn ,yn)= 1.0519+ 0.0250* 1.1545= 1.0808
4 , yn+1=yn + h* f(xn ,yn)= 1.0808+ 0.0250* 1.2368= 1.1117
Approximate solution at x = 0.100 is 1.111673
沒有留言:
張貼留言