当前位置: 首页 > news >正文

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

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

📜流体力学电磁学运动学动力学化学和电路中欧拉法示例:Python重力弹弓流体晃动微分方程模型和交直流电阻电容电路
在这里插入图片描述

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

在数学和计算科学中,欧拉方法(也称为前向欧拉方法)是一种用于求解具有给定初值的常微分方程的一阶数值程序。考虑一个微分方程 d y / d x = f ( x , y ) d y / d x=f(x, y) dy/dx=f(x,y),初始条件为 y ( x 0 ) = y 0 y(x 0)=y 0 y(x0)=y0,则该方程的逐次逼近可由下式给出:
y ( n + 1 ) = y ( n ) + h ∗ f ( x ( n ) , y ( n ) ) y(n+1)=y(n)+h * f(x(n), y(n)) y(n+1)=y(n)+hf(x(n),y(n))
其中 h = ( x ( n ) − x ( 0 ) ) / n h=(x(n)-x(0)) / n h=(x(n)x(0))/n, $h $表示步长。选择较小的 h h h​ 值会导致更准确的结果和更多的计算时间。

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

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

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

现在我们可以使用欧拉公式计算 y 1 y_1 y1
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. \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} y1=y0+hf(x0,y0)y1=1+0.025(0+1+01)y1=1.025y(0.025)=1.025.
类似地我们可以计算 y ( 0.050 ) , y ( 0.075 ) , … y(0.050), y(0.075), \ldots y(0.050),y(0.075), y ( 0.1 ) y(0.1) y(0.1)

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

Python实现:

def func( x, y ):return (x + y + x * y)def euler( x0, y, h, x ):temp = -0while x0 < x:temp = yy = y + h * func(x0, y)x0 = x0 + hprint("Approximate solution at x = ", x, " is ", "%.6f"% y)x0 = 0
y0 = 1
h = 0.025
x = 0.1euler(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>

👉参阅:计算思维 | 亚图跨际

相关文章:

  • 20240621每日后端---------如何优化项目中的10000个if-else 语句?
  • ⭐Unity 控制任意UI的渐隐渐显
  • JDBC从入门到精通-笔记(一):JDBC基本概念与开发基础
  • 构建安全高效的前端权限控制系统
  • Flutter 实现软鼠标
  • 寻找重复数 - LeetCode 热题 100
  • QCombox绑定QMap
  • Map-JAVA面试常问
  • exzxml C语言XML解析库使用记录
  • selenium框架学习
  • Aigtek电压放大器的主要作用是什么
  • 华为手机数据恢复,2个技巧介绍,误删文件后的紧急处理
  • Python界面编辑器Tkinter布局助手 使用体验
  • 目标跟踪——KCF源码用python实现
  • 本地无法连接linux上的MariaDB数据库
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • axios 和 cookie 的那些事
  • php ci框架整合银盛支付
  • puppeteer stop redirect 的正确姿势及 net::ERR_FAILED 的解决
  • python_bomb----数据类型总结
  • unity如何实现一个固定宽度的orthagraphic相机
  • 成为一名优秀的Developer的书单
  • 初识 webpack
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 腾讯视频格式如何转换成mp4 将下载的qlv文件转换成mp4的方法
  • 突破自己的技术思维
  • 我建了一个叫Hello World的项目
  • 译米田引理
  • #define
  • $(this) 和 this 关键字在 jQuery 中有何不同?
  • $refs 、$nextTic、动态组件、name的使用
  • (02)Hive SQL编译成MapReduce任务的过程
  • (175)FPGA门控时钟技术
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (C语言)球球大作战
  • (c语言+数据结构链表)项目:贪吃蛇
  • (done) 两个矩阵 “相似” 是什么意思?
  • (pojstep1.3.1)1017(构造法模拟)
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (STM32笔记)九、RCC时钟树与时钟 第二部分
  • (vue)el-tabs选中最后一项后更新数据后无法展开
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (ZT)一个美国文科博士的YardLife
  • (二刷)代码随想录第15天|层序遍历 226.翻转二叉树 101.对称二叉树2
  • (附源码)小程序 交通违法举报系统 毕业设计 242045
  • (一)【Jmeter】JDK及Jmeter的安装部署及简单配置
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • .NET 材料检测系统崩溃分析
  • .net连接MySQL的方法
  • .NET企业级应用架构设计系列之技术选型
  • :=
  • @Import注解详解
  • @RequestBody详解:用于获取请求体中的Json格式参数
  • @TableId注解详细介绍 mybaits 实体类主键注解