计算方法实验9:Romberg积分求解速度、位移

2024-05-10 01:36

本文主要是介绍计算方法实验9:Romberg积分求解速度、位移,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

任务

在这里插入图片描述

  1. 输出质点的轨迹 ( x ( t ) , y ( t ) ) , t ∈ { 0.1 , 0.2 , 0.3 , . . . , 10 } (x(t), y(t)), t\in \{0.1, 0.2, 0.3, ..., 10\} (x(t),y(t)),t{0.1,0.2,0.3,...,10},并在二维平面中画出该轨迹.
  2. 请比较M分别取4, 8, 12, 16, 20 时,Romberg积分达到要求精度的比例(达到误差要求的次数/调用总次数),分析该比例随M 的变化。

算法

现在要用数值方法求 ∫ a b f ( x ) d x \int_{a}^{b} f(x) \, dx abf(x)dx,设 h = b − a n h=\frac{b-a}{n} h=nba,已知:

复化梯形积分 T n ( f ) = h [ 1 2 f ( a ) + ∑ i = 1 n − 1 f ( a + i h ) + 1 2 f ( b ) ] T_{n}\left(f\right)=h\left[\frac{1}{2}f\left(a\right)+\sum_{i=1}^{n-1}f\left(a+ih\right)+\frac{1}{2}f\left(b\right)\right] Tn(f)=h[21f(a)+i=1n1f(a+ih)+21f(b)]

复化Simpson积分 S n ( f ) = h 3 [ f ( a ) + 4 ∑ i = 0 m − 1 f ( x 2 i + 1 ) + 2 ∑ i = 1 m − 1 f ( x 2 i ) + f ( b ) ] S_{n}\left(f\right)=\frac{h}{3}\left[f\left(a\right)+4\sum_{i=0}^{m-1}f\left(x_{2i+1}\right)+2\sum_{i=1}^{m-1}f\left(x_{2i}\right)+f\left(b\right)\right] Sn(f)=3h[f(a)+4i=0m1f(x2i+1)+2i=1m1f(x2i)+f(b)].

( T n ( f ) − T 2 n ( f ) ) ( T_n( f) - T_{2n}( f) ) (Tn(f)T2n(f)) 作 为 T 2 n ( f ) T_{2n}(f) T2n(f)的修正值补充到 I ( f ) I(f) I(f),即
I ( f ) ≈ T 2 n ( f ) + 1 3 ( T 2 n ( f ) − T n ( f ) ) = 4 3 T 2 n − 1 3 T n = S n I(f)\approx T_{2n}(f)+\frac{1}{3}\left(T_{2n}\left(f\right)-T_{n}\left(f\right)\right)=\frac{4}{3}T_{2n}-\frac{1}{3}T_{n}=S_{n} I(f)T2n(f)+31(T2n(f)Tn(f))=34T2n31Tn=Sn

其结果是将复化梯形求积公式组合成复化 Simpson 求积公式, 截断误差由 O ( h 2 ) O(h^2) O(h2)提高到 O ( h 4 ) O(h^4) O(h4),这种手段称为外推算法,该算法在不增加计算量的前提下提高了误差的精度.不妨对 S 2 n ( f ) , S n ( f ) S_{2n}(f),S_n(f) S2n(f),Sn(f)再作一次线性组合:

I ( f ) − S n ( f ) = − f ( 4 ) ( ξ ) 180 h 4 ( b − a ) ≈ d h 4 I\left(f\right)-S_{n}\left(f\right)=-\frac{f^{\left(4\right)}\left(\xi\right)}{180}h^{4}\left(b-a\right)\approx dh^{4} I(f)Sn(f)=180f(4)(ξ)h4(ba)dh4
I ( f ) − S 2 n ( f ) = − f ( 4 ) ( η ) 180 ( h 2 ) 4 ( b − a ) ≈ d ( h 2 ) 4 I(f)-S_{2n}(f)=-\frac{f^{(4)}(\eta)}{180}\left(\frac{h}{2}\right)^{4}(b-a)\approx d\left(\frac{h}{2}\right)^{4} I(f)S2n(f)=180f(4)(η)(2h)4(ba)d(2h)4

I ( f ) ≈ S 2 n ( f ) + 1 15 ( S 2 n ( f ) − S n ( f ) ) = C n ( f ) I\left(f\right)\approx S_{2n}\left(f\right)+\frac{1}{15}\left(S_{2n}\left(f\right)-S_{n}\left(f\right)\right)=C_{n}\left(f\right) I(f)S2n(f)+151(S2n(f)Sn(f))=Cn(f)
复化 Simpson 公式组成复化 Cotes 公式,其截断误差是 O ( h 6 ) . O(h^6). O(h6).同理对 Cotes公式进行线性组合:
I ( f ) − C 2 n ( f ) = e ( h 2 ) 6 I ( f ) − C n ( f ) = e h 6 I\left(f\right)-C_{2n}\left(f\right)=e\left(\frac{h}{2}\right)^{6}\\I\left(f\right)-C_{n}\left(f\right)=eh^{6} I(f)C2n(f)=e(2h)6I(f)Cn(f)=eh6
得到具有 7 次代数精度和截断误差是 O ( h 8 ) O(h^8) O(h8)的 Romberg 公式:
R n ( f ) = C 2 n ( f ) + 1 63 ( C 2 n ( f ) − C n ( f ) ) R_{n}\left(f\right)=C_{2n}\left(f\right)+\frac{1}{63}\left(C_{2n}\left(f\right)-C_{n}\left(f\right)\right) Rn(f)=C2n(f)+631(C2n(f)Cn(f))

为了便于在计算机上实现 Romberg 算法,将 T n , S n , C n , R n , ⋯ T_n,S_n,C_n,R_n,\cdots Tn,Sn,Cn,Rn,统一用 R k , j R_{k,j} Rk,j 表示,列标 j = 1 , 2 , 3 , 4 j=1,2,3,4 j=1,2,3,4分别表示梯形、Simpson、Cotes 、Romberg积分,行标 k k k表示步长 h k = h 2 k − 1 h_k=\frac h{2^{k-1}} hk=2k1h,得到Romberg 计算公式:
R k , j = R k , j − 1 + R k , j − 1 − R k − 1 , j − 1 4 j − 1 − 1 , k = 2 , 3 , ⋯ R_{k,j}=R_{k,j-1}+\frac{R_{k,j-1}-R_{k-1,j-1}}{4^{j-1}-1},k=2,3,\cdots Rk,j=Rk,j1+4j11Rk,j1Rk1,j1,k=2,3,
对每一个 k , j k,j k,j从 2 做到 k k k,一直做到 ∣ R k , k − R k − 1 , k − 1 ∣ |R_k,k-R_{k-1,k-1}| Rk,kRk1,k1小于给定控制精度时停止计算.
注:下面代码中数组下标从0开始.

代码

C++实现Romberg积分运算

#include<bits/stdc++.h>
using namespace std;int satisfiedCount;long double ax(long double t);
long double ay(long double t);
long double romberg(function<long double(long double)> f, long double a, long double b, long double eps, int maxIter, bool isX);// Perform the Romberg integrationint main() 
{long double eps = 1e-6, proportion;int maxIter;satisfiedCount = 0;maxIter = 4;cout << "maxIter = " << maxIter << endl;for (long double t = 0.1; t <= 10; t += 0.1) { long double vx = romberg(ax, 0, t, eps, maxIter, 0);long double vy = romberg(ay, 0, t, eps, maxIter, 0);long double x = romberg([&](long double t) { return vx; }, 0, t, eps, maxIter, 1);long double y = romberg([&](long double t) { return vy; }, 0, t, eps, maxIter, 0);cout << fixed << setprecision(1) << "At t = " << t << ", vx = " << setprecision(6) << vx << ", vy = " << setprecision(6) << vy << ", " << "(x, y) = (" << setprecision(6) << x << ", " << setprecision(6) << y << ")" << endl;}proportion = (long double)satisfiedCount / 100;cout << "At maxIter = " << maxIter << ", proportion of times the error requirement of x was satisfied: " << proportion << endl;satisfiedCount = 0;maxIter = 8;cout << "maxIter = " << maxIter << endl;ofstream outFile("trajectory.txt");for (long double t = 0.1; t <= 10; t += 0.1) {long double vx = romberg(ax, 0, t, eps, maxIter, 0);long double vy = romberg(ay, 0, t, eps, maxIter, 0);long double x = romberg([&](long double t) { return vx; }, 0, t, eps, maxIter, 1);long double y = romberg([&](long double t) { return vy; }, 0, t, eps, maxIter, 0);cout << fixed << setprecision(1) << "At t = " << t << ", vx = " << setprecision(6) << vx << ", vy = " << setprecision(6) << vy << ", " << "(x, y) = (" << setprecision(6) << x << ", " << setprecision(6) << y << ")" << endl;outFile << fixed << setprecision(6) << x << " " << y << "\n";//把坐标写入文件,方便画轨迹}proportion = (long double)satisfiedCount / 100;cout << "At maxIter = " << maxIter << ", proportion of times the error requirement of x was satisfied: " << proportion << endl;satisfiedCount = 0;maxIter = 12;cout << "maxIter = " << maxIter << endl;for (long double t = 0.1; t <= 10; t += 0.1) {long double vx = romberg(ax, 0, t, eps, maxIter, 0);long double vy = romberg(ay, 0, t, eps, maxIter, 0);long double x = romberg([&](long double t) { return vx; }, 0, t, eps, maxIter, 1);long double y = romberg([&](long double t) { return vy; }, 0, t, eps, maxIter, 0);cout << fixed << setprecision(1) << "At t = " << t << ", vx = " << setprecision(6) << vx << ", vy = " << setprecision(6) << vy << ", " << "(x, y) = (" << setprecision(6) << x << ", " << setprecision(6) << y << ")" << endl;}proportion = (long double)satisfiedCount / 100;cout << "At maxIter = " << maxIter << ", proportion of times the error requirement of x was satisfied: " << proportion << endl;satisfiedCount = 0;maxIter = 16;cout << "maxIter = " << maxIter << endl;for (long double t = 0.1; t <= 10; t += 0.1) {long double vx = romberg(ax, 0, t, eps, maxIter, 0);long double vy = romberg(ay, 0, t, eps, maxIter, 0);long double x = romberg([&](long double t) { return vx; }, 0, t, eps, maxIter, 1);long double y = romberg([&](long double t) { return vy; }, 0, t, eps, maxIter, 0);cout << fixed << setprecision(1) << "At t = " << t << ", vx = " << setprecision(6) << vx << ", vy = " << setprecision(6) << vy << ", " << "(x, y) = (" << setprecision(6) << x << ", " << setprecision(6) << y << ")" << endl;}proportion = (long double)satisfiedCount / 100;cout << "At maxIter = " << maxIter << ", proportion of times the error requirement of x was satisfied: " << proportion << endl;satisfiedCount = 0;maxIter = 20;cout << "maxIter = " << maxIter << endl;for (long double t = 0.1; t < 10.1; t += 0.1) {long double vx = romberg(ax, 0, t, eps, maxIter, 0);long double vy = romberg(ay, 0, t, eps, maxIter, 0);long double x = romberg([&](long double t) { return vx; }, 0, t, eps, maxIter, 1);long double y = romberg([&](long double t) { return vy; }, 0, t, eps, maxIter, 0);cout << fixed << setprecision(1) << "At t = " << t << ", vx = " << setprecision(6) << vx << ", vy = " << setprecision(6) << vy << ", " << "(x, y) = (" << setprecision(6) << x << ", " << setprecision(6) << y << ")" << endl;}proportion = (long double)satisfiedCount / 100;cout << "At maxIter = " << maxIter << ", proportion of times the error requirement of x was satisfied: " << proportion << endl;return 0;
}long double ax(long double t) 
{return sin(t) / (1 + sqrt(t));
}long double ay(long double t) 
{return log(t + 1) / (t + 1);
}// Perform the Romberg integration
long double romberg(function<long double(long double)> f, long double a, long double b, long double eps, int maxIter, bool isX) {long double h[maxIter], r[maxIter][maxIter];h[0] = b - a;r[0][0] = 0.5 * h[0] * (f(a) + f(b));for (int i = 1; i < maxIter; i++) {h[i] = 0.5 * h[i-1];long double sum = 0;for (int k = 0; k < pow(2, i-1); k++)sum += f(a + (2*k+1) * h[i]);r[i][0] = 0.5 * r[i-1][0] + h[i] * sum;for (int j = 1; j <= i; j++)r[i][j] = r[i][j-1] + (r[i][j-1] - r[i-1][j-1]) / (pow(4, j) - 1);if (i > 1 && fabs(r[i][i] - r[i-1][i-1]) < eps){if(isX)satisfiedCount++;return r[i][i];}}return r[maxIter-1][maxIter-1];
}

python可视化运动轨迹

import matplotlib.pyplot as pltwith open('trajectory.txt', 'r') as file:lines = file.readlines()x, y = zip(*[(float(line.split()[0]), float(line.split()[1])) for line in lines])plt.plot(x, y, 'o-')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Plot of points with smooth curve')
plt.show()

结果

部分运算结果

在这里插入图片描述

轨迹可视化结果

在这里插入图片描述

这篇关于计算方法实验9:Romberg积分求解速度、位移的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/975067

相关文章

numpy求解线性代数相关问题

《numpy求解线性代数相关问题》本文主要介绍了numpy求解线性代数相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 在numpy中有numpy.array类型和numpy.mat类型,前者是数组类型,后者是矩阵类型。数组

STM32(十一):ADC数模转换器实验

AD单通道: 1.RCC开启GPIO和ADC时钟。配置ADCCLK分频器。 2.配置GPIO,把GPIO配置成模拟输入的模式。 3.配置多路开关,把左面通道接入到右面规则组列表里。 4.配置ADC转换器, 包括AD转换器和AD数据寄存器。单次转换,连续转换;扫描、非扫描;有几个通道,触发源是什么,数据对齐是左对齐还是右对齐。 5.ADC_CMD 开启ADC。 void RCC_AD

HNU-2023电路与电子学-实验3

写在前面: 一、实验目的 1.了解简易模型机的内部结构和工作原理。 2.分析模型机的功能,设计 8 重 3-1 多路复用器。 3.分析模型机的功能,设计 8 重 2-1 多路复用器。 4.分析模型机的工作原理,设计模型机控制信号产生逻辑。 二、实验内容 1.用 VERILOG 语言设计模型机的 8 重 3-1 多路复用器; 2.用 VERILOG 语言设计模型机的 8 重 2-1 多

微积分-积分应用5.4(功)

术语“功”在日常语言中用来表示完成一项任务所需的总努力量。在物理学中,它有一个依赖于“力”概念的技术含义。直观上,你可以将力理解为对物体的推或拉——例如,一个书本在桌面上的水平推动,或者地球对球的向下拉力。一般来说,如果一个物体沿着一条直线运动,位置函数为 s ( t ) s(t) s(t),那么物体上的力 F F F(与运动方向相同)由牛顿第二运动定律给出,等于物体的质量 m m m 与其

IBS和IBD的区别和计算方法介绍

大家好,我是邓飞。 今天介绍一下IBS和IBD的区别: IBS(肠易激综合症)和IBD(炎症性肠病)是两种不同的消化系统疾病,主要区别如下: IBS(Irritable Bowel Syndrome):是一种功能性肠道疾病,主要表现为腹痛、腹胀、腹泻或便秘,症状通常与饮食、压力和心理因素相关,没有明显的器质性病变。 IBD(Inflammatory Bowel Disease):是一组

使用WebP解决网站加载速度问题,这些细节你需要了解

说到网页的图片格式,大家最常想到的可能是JPEG、PNG,毕竟这些老牌格式陪伴我们这么多年。然而,近几年,有一个格式悄悄崭露头角,那就是WebP。很多人可能听说过,但到底它好在哪?你的网站或者项目是不是也应该用WebP呢?别着急,今天咱们就来好好聊聊WebP这个图片格式的前世今生,以及它值不值得你花时间去用。 为什么会有WebP? 你有没有遇到过这样的情况?网页加载特别慢,尤其是那

组合c(m,n)的计算方法

问题:求解组合数C(n,m),即从n个相同物品中取出m个的方案数,由于结果可能非常大,对结果模10007即可。       共四种方案。ps:注意使用限制。 方案1: 暴力求解,C(n,m)=n*(n-1)*...*(n-m+1)/m!,n<=15 ; int Combination(int n, int m) { const int M = 10007; int

2024 年高教社杯全国大学生数学建模竞赛题目——2024 年高教社杯全国大学生数学建模竞赛题目的求解

2024 年高教社杯全国大学生数学建模竞赛题目 (请先阅读“ 全国大学生数学建模竞赛论文格式规范 ”) 2024 年高教社杯全国大学生数学建模竞赛题目 随着城市化进程的加快、机动车的快速普及, 以及人们活动范围的不断扩大,城市道 路交通拥堵问题日渐严重,即使在一些非中心城市,道路交通拥堵问题也成为影响地方经 济发展和百姓幸福感的一个“痛点”,是相关部门的棘手难题之一。 考虑一个拥有知名景区

关于一次速度优化的往事

来自:hfghfghfg, 时间:2003-11-13 16:32, ID:2292221你最初的代码 Button1 34540毫秒 5638毫秒  Button2 我的代码 这个不是重点,重点是这个  来自:hfghfghfg, 时间:2003-11-13 16:54, ID:22923085528毫秒 不会吧,我是赛杨1.1G  128M内存  w2000, delphi6  128M

61.以太网数据回环实验(4)以太网数据收发器发送模块

(1)状态转移图: (2)IP数据包格式: (3)UDP数据包格式: (4)以太网发送模块代码: module udp_tx(input wire gmii_txc ,input wire reset_n ,input wire tx_start_en , //以太网开始发送信