给定一个具有初始条件y(x0)= y0的微分方程dy / dx = f(x,y)。使用欧拉方法找到其近似解。
欧拉法:
在数学和计算科学中,欧拉方法(也称为正向
欧拉法)是求解常微分的一阶数值程序
给定初始值的方程式(ODE)。
考虑初始条件为y(x0)= y0的微分方程dy / dx = f(x,y)
那么该方程式的成功逼近可以由下式给出:
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.
例子 :
Consider below differential equation
dy/dx = (x + y + xy)
with initial condition y(0) = 1
and step size h = 0.025.
Find y(0.1).
Solution:
f(x, y) = (x + y + xy)
x0 = 0, y0 = 1, h = 0.025
Now we can calculate y1 using Euler formula
y1 = y0 + h * f(x0, y0)
y1 = 1 + 0.025 *(0 + 1 + 0 * 1)
y1 = 1.025
y(0.025) = 1.025.
Similarly we can calculate y(0.050), y(0.075), ....y(0.1).
y(0.1) = 1.11167
C++
/* CPP Program to find approximation
of a ordinary differential equation
using euler method.*/
#include
using namespace std;
// 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
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
// Printing approximation
cout << "Approximate solution at x = "
<< x << " is " << y << endl;
}
// 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;
}
Java
// Java program to find approximation of an ordinary
// differential equation using euler method
import java.io.*;
class Euler {
// 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
while (x0 < x) {
temp = y;
y = y + h * func(x0, y);
x0 = x0 + h;
}
// Printing approximation
System.out.println("Approximate solution at x = "
+ x + " is " + y);
}
// Driver program
public static void main(String args[]) throws IOException
{
Euler obj = new Euler();
// Initial Values
float x0 = 0;
float y0 = 1;
float h = 0.025f;
// Value of x at which we need approximation
float x = 0.1f;
obj.euler(x0, y0, h, x);
}
}
// This code is contributed by Anshika Goyal.
Python3
# Python Code to find approximation
# of a ordinary differential equation
# using euler method.
# Consider a differential equation
# dy / dx =(x + y + xy)
def func( x, y ):
return (x + y + x * y)
# Function for euler formula
def euler( x0, y, h, x ):
temp = -0
# Iterating till the point at which we
# need approximation
while x0 < x:
temp = y
y = y + h * func(x0, y)
x0 = x0 + h
# Printing approximation
print("Approximate solution at x = ", x, " is ", "%.6f"% y)
# Driver Code
# Initial Values
x0 = 0
y0 = 1
h = 0.025
# Value of x at which we need approximation
x = 0.1
euler(x0, y0, h, x)
C#
// C# program to find approximation of an ordinary
// differential equation using euler method
using System;
class GFG {
// Consider a differential equation
// dy/dx=(x + y + xy)
static float func(float x, float y)
{
return (x + y + x * y);
}
// Function for Euler formula
static void euler(float x0, float y, float h, float x)
{
// Iterating till the point at which we
// need approximation
while (x0 < x) {
y = y + h * func(x0, y);
x0 = x0 + h;
}
// Printing approximation
Console.WriteLine("Approximate solution at x = "
+ x + " is " + y);
}
// Driver program
public static void Main()
{
// Initial Values
float x0 = 0;
float y0 = 1;
float h = 0.025f;
// Value of x at which we need
// approximation
float x = 0.1f;
euler(x0, y0, h, x);
}
}
// This code is contributed by Vt_m.
PHP
Javascript
输出 :
Approximate solution at x = 0.1 is 1.11167