给出一个初始条件为y(x0)=y0的微分方程dy/dx=f(x,y)。使用 欧拉法 . 欧拉法: 在数学和计算科学中,Euler方法(也称为forward) 欧拉方法是求解常微分方程的一阶数值方法 具有给定初始值的方程(ODE)。 考虑初始条件y(x0)=y0的微分方程Dy/Dx= f(x,y) 然后,该方程的逐次逼近可通过以下公式得出:
null
y(n+1)=y(n)+h*f(x(n),y(n)) 其中h=(x(n)–x(0))/n h表示步长。选择较小的 h值会导致更准确的结果 以及更多的计算时间。
例子:
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 <iostream> 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
<?php // PHP Program to find approximation // of a ordinary differential equation // using euler method // Consider a differential equation // dy/dx=(x + y + xy) function func( $x , $y ) { return ( $x + $y + $x * $y ); } // Function for Euler formula function 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 echo "Approximate solution at x = " , $x , " is " , $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 ); // This code contributed by aj_36 ?> |
Javascript
<script> // JavaScript program to find approximation of an ordinary // differential equation using euler method // Consider a differential equation // dy/dx=(x + y + xy) function func(x, y) { return (x + y + x * y); } // Function for Euler formula function euler(x0, y, h, x) { let 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 document.write( "Approximate solution at x = " + x + " is " + y); } // Driver Code // Initial Values let x0 = 0; let y0 = 1; let h = 0.025; // Value of x at which we need approximation let x = 0.1; euler(x0, y0, h, x); // This code is contributed by chinmoy1997pal. </script> |
输出:
Approximate solution at x = 0.1 is 1.11167
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END