🥑Python多语言欧拉法和预测校正器实现

Python | C++ | C# | Java | JavaScript | 欧拉法 | 数学 | 微分方程

🏈指点迷津 | Brief

📜流体力学电磁学运动学动力学化学和电路中欧拉法

📜流体力学电磁学运动学动力学化学和电路中欧拉法示例:Python重力弹弓流体晃动微分方程模型和交直流电阻电容电路

✒️多语言实现欧拉法和修正欧拉法

在数学和计算科学中,欧拉方法(也称为前向欧拉方法)是一种用于求解具有给定初值的常微分方程的一阶数值程序。考虑一个微分方程 dy/dx=f(x,y)d y / d x=f(x, y),初始条件为 y(x0)=y0y(x_0)=y_0,则该方程的逐次逼近可由下式给出:

y(n+1)=y(n)+hf(x(n),y(n))y(n+1)=y(n)+h * f(x(n), y(n))

其中 h=(x(n)x(0))/nh=(x(n)-x(0)) / nhh 表示步长。选择较小的 hh​ 值会导致更准确的结果和更多的计算时间。

例如,考虑微分方程 dy/dx=(x+y+xy)d y / d x=(x+y+x y) ,初始条件为 y(0)=1y (0)=1,步长为 h=0.025h =0.025。求y(0.1)y(0.1)​。

解:f(x,y)=(x+y+xy)f(x, y)=(x+y+x y)

x0=0,y0=1,h=0.025x 0=0, y 0=1, h=0.025

现在我们可以使用欧拉公式计算 y1y_1

y1=y0+hf(x0,y0)y1=1+0.025(0+1+01)y1=1.025y(0.025)=1.025.\begin{aligned} & y_1=y 0+h * f(x 0, y 0) \\ & y_1=1+0.025 *(0+1+0 * 1) \\ & y_1=1.025 \\ & y(0.025)=1.025 . \end{aligned}

类似地我们可以计算 y(0.050),y(0.075),y(0.1)y(0.050), y(0.075), \ldots y(0.1)​

y(0.1)=1.11167y(0.1)=1.11167

Python实现:

 def func( x, y ):
     return (x + y + x * y)
     
 def euler( x0, y, h, x ):
     temp = -0
 ​
     while x0 < x:
         temp = y
         y = y + h * func(x0, y)
         x0 = x0 + h
 ​
     print("Approximate solution at x = ", x, " is ", "%.6f"% y)
     
 x0 = 0
 y0 = 1
 h = 0.025
 x = 0.1
 ​
 euler(x0, y0, h, x)
 ​

C++实现:

 #include <iostream>
 using namespace std;
 ​
 float func(float x, float y)
 {
     return (x + y + x * y);
 }
 ​
 void euler(float x0, float y, float h, float x)
 {
     float temp = -0;
 ​
     while (x0 < x) {
         temp = y;
         y = y + h * func(x0, y);
         x0 = x0 + h;
     }
 ​
     cout << "Approximate solution at x = "
         << x << " is " << y << endl;
 }
 ​
 int main()
 {
 ​
     float x0 = 0;
     float y0 = 1;
     float h = 0.025;
 ​
     float x = 0.1;
 ​
     euler(x0, y0, h, x);
     return 0;
 }
 ​

C#实现:

 using System;
 ​
 class GFG {
 ​
     static float func(float x, float y)
     {
         return (x + y + x * y);
     }
 ​
     static void euler(float x0, float y, float h, float x)
     {
 ​
         while (x0 < x) {
             y = y + h * func(x0, y);
             x0 = x0 + h;
         }
 ​
         Console.WriteLine("Approximate solution at x = "
                         + x + " is " + y);
     }
 ​
     public static void Main()
     {
 ​
         float x0 = 0;
         float y0 = 1;
         float h = 0.025f;
         float x = 0.1f;
 ​
         euler(x0, y0, h, x);
     }
 }

Java实现:

 import java.io.*;
 ​
 class Euler {
     float func(float x, float y)
     {
         return (x + y + x * y);
     }
 ​
     void euler(float x0, float y, float h, float x)
     {
         float temp = -0;
         while (x0 < x) {
             temp = y;
             y = y + h * func(x0, y);
             x0 = x0 + h;
         }
 ​
         System.out.println("Approximate solution at x = "
                         + x + " is " + y);
     }
 ​
     public static void main(String args[]) throws IOException
     {
         Euler obj = new Euler();
         float x0 = 0;
         float y0 = 1;
         float h = 0.025f;
         float x = 0.1f;
 ​
         obj.euler(x0, y0, h, x);
     }
 }

JavaScript实现:

 <script>
 ​
     function func(x, y)
     {
         return (x + y + x * y);
     }
 ​
     function euler(x0, y, h, x)
     {
         let temp = -0;
 ​
         while (x0 < x) {
             temp = y;
             y = y + h * func(x0, y);
             x0 = x0 + h;
         }
         document.write("Approximate solution at x = "
                         + x + " is " + y);
     }
 ​
     let x0 = 0;
     let y0 = 1;
     let h = 0.025;
 ​
     let x = 0.1;
 ​
     euler(x0, y0, h, x);
 ​
 </script>
 ​

预测校正器或修正欧拉法

Python实现

 def f(x, y):
     v = y - 2 * x * x + 1;
     return v;
 ​
 def predict(x, y, h):
     
     y1p = y + h * f(x, y);
     return y1p;
 ​
 ​
 def correct(x, y, x1, y1, h):
     
 ​
     e = 0.00001;
     y1c = y1;
 ​
     while (abs(y1c - y1) > e + 1):
         y1 = y1c;
         y1c = y + 0.5 * h * (f(x, y) + f(x1, y1));
 ​
     return y1c;
 ​
 def printFinalValues(x, xn, y, h):
     while (x < xn):
         x1 = x + h;
         y1p = predict(x, y, h);
         y1c = correct(x, y, x1, y1p, h);
         x = x1;
         y = y1c;
 ​
 ​
     print("The final value of y at x =",
                     int(x), "is :", y);
 ​
 ​
 if __name__ == '__main__':
 ​
     x = 0; y = 0.5;
     xn = 1;
     h = 0.2;
 ​
     printFinalValues(x, xn, y, h);
 ​

C++实现

 #include <bits/stdc++.h>
 using namespace std;
 ​
 double f(double x, double y)
 {
     double v = y - 2 * x * x + 1;
     return v;
 }
 ​
 double predict(double x, double y, double h)
 {
     double y1p = y + h * f(x, y);
     return y1p;
 }
 ​
 double correct(double x, double y,
             double x1, double y1,
             double h)
 {
 ​
     double e = 0.00001;
     double y1c = y1;
 ​
     do {
         y1 = y1c;
         y1c = y + 0.5 * h * (f(x, y) + f(x1, y1));
     } while (fabs(y1c - y1) > e);
 ​
     return y1c;
 }
 ​
 void printFinalValues(double x, double xn,
                     double y, double h)
 {
 ​
     while (x < xn) {
         double x1 = x + h;
         double y1p = predict(x, y, h);
         double y1c = correct(x, y, x1, y1p, h);
         x = x1;
         y = y1c;
     }
 ​
     cout << "The final value of y at x = "
         << x << " is : " << y << endl;
 }
 ​
 int main()
 {
     double x = 0, y = 0.5;
     double xn = 1;
     double h = 0.2;
 ​
     printFinalValues(x, xn, y, h);
 ​
     return 0;
 }
 ​

C#实现

 using System;
 ​
 class GFG
 {
     
 static double f(double x, double y)
 {
     double v = y - 2 * x * x + 1;
     return v;
 }
 ​
 static double predict(double x, double y, double h)
 {
     double y1p = y + h * f(x, y);
     return y1p;
 }
 ​
 static double correct(double x, double y,
             double x1, double y1,
             double h)
 {
 ​
     double e = 0.00001;
     double y1c = y1;
 ​
     do
     {
         y1 = y1c;
         y1c = y + 0.5 * h * (f(x, y) + f(x1, y1));
     }
     while (Math.Abs(y1c - y1) > e);
 ​
     return y1c;
 }
 ​
 static void printFinalValues(double x, double xn,
                     double y, double h)
 {
 ​
     while (x < xn) 
     {
         double x1 = x + h;
         double y1p = predict(x, y, h);
         double y1c = correct(x, y, x1, y1p, h);
         x = x1;
         y = y1c;
     }
 ​
     Console.WriteLine("The final value of y at x = "+
                         x + " is : " + Math.Round(y, 5));
 }
 ​
 static void Main()
 {
 ​
     double x = 0, y = 0.5;
     double xn = 1;
     double h = 0.2;
 ​
     printFinalValues(x, xn, y, h);
 }
 }

Java实现

 import java.text.*;
 ​
 class GFG
 {
 ​
 static double f(double x, double y)
 {
     double v = y - 2 * x * x + 1;
     return v;
 }
 ​
 static double predict(double x, double y, double h)
 {
     double y1p = y + h * f(x, y);
     return y1p;
 }
 ​
 static double correct(double x, double y,
                     double x1, double y1,
                     double h)
 {
     double e = 0.00001;
     double y1c = y1;
 ​
     do
     {
         y1 = y1c;
         y1c = y + 0.5 * h * (f(x, y) + f(x1, y1));
     }
     while (Math.abs(y1c - y1) > e);
 ​
     return y1c;
 }
 ​
 static void printFinalValues(double x, double xn,
                     double y, double h)
 {
 ​
     while (x < xn) 
     {
         double x1 = x + h;
         double y1p = predict(x, y, h);
         double y1c = correct(x, y, x1, y1p, h);
         x = x1;
         y = y1c;
     }
 ​
     DecimalFormat df = new DecimalFormat("#.#####");
     System.out.println("The final value of y at x = "+
                         x + " is : "+df.format(y));
 }
 ​
 public static void main (String[] args) 
 {
 ​
     double x = 0, y = 0.5;
     double xn = 1;
     double h = 0.2;
 ​
     printFinalValues(x, xn, y, h);
 }
 }
 ​

JavaScript实现

 <script>
     function f(x , y) {
         var v = y - 2 * x * x + 1;
         return v;
     }
 ​
     function predict(x , y , h) {
         var y1p = y + h * f(x, y);
         return y1p;
     }
 ​
     function correct(x , y , x1 , y1 , h) {
 ​
         var e = 0.00001;
         var y1c = y1;
 ​
         do {
             y1 = y1c;
             y1c = y + 0.5 * h * (f(x, y) + f(x1, y1));
         } while (Math.abs(y1c - y1) > e);
         return y1c;
     }
 ​
     function printFinalValues(x , xn , y , h) {
 ​
         while (x < xn) {
             var x1 = x + h;
             var y1p = predict(x, y, h);
             var y1c = correct(x, y, x1, y1p, h);
             x = x1;
             y = y1c;
         }
 ​
         document.write("The final value of y at x = " + x + " is : " + y.toFixed(5));
     }
 ​
         var x = 0, y = 0.5;
         var xn = 1;
 ​
         var h = 0.2;
         printFinalValues(x, xn, y, h);
 </script>
 ​

Last updated