计算方法实验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 integration

int 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 plt

with 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()

结果

部分运算结果

在这里插入图片描述

轨迹可视化结果

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/607529.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

MTK平台ATE tool

一、校准测试环境搭建 ① 仪器端一个端口直接连接功分器。 ② 功分器输出端外接3dbm的衰减器。 ③功分器空出来的端口需要外接50 Ω的负载。 ④功分器与手机端口的连接没有顺序之分。 二、ATE设置介绍 ATE所支持的无线通信系统 — GSM — WCDMA — TDSCDMA — LTE — WI…

Redis持久化策略——Java全栈知识(17)

Redis持久化 1、Redis 持久化的三种方式 1、RDB&#xff1a; 以快照的方式将此刻 Redis 中的数据以二进制的文件形式保存在磁盘中。 RDB 的优点是&#xff1a;快照文件小、恢复速度快&#xff0c;适合做备份和灾难恢复。 RDB 的缺点是&#xff1a;定期更新可能会丢数据&#…

2024年软件测试最全Jmeter--【作为测试你必须要知道的】基础名词与环境搭建,2024年最新年末阿里百度等大厂技术面试题汇总

网上学习资料一大堆&#xff0c;但如果学到的知识不成体系&#xff0c;遇到问题时只是浅尝辄止&#xff0c;不再深入研究&#xff0c;那么很难做到真正的技术提升。 需要这份系统化的资料的朋友&#xff0c;可以戳这里获取 一个人可以走的很快&#xff0c;但一群人才能走的更…

使用videosapi开发微信聊天记录防撤回

接口地址&#xff1a; http://接口地址/post/api/ 接收到消息后&#xff0c;如若进行撤回比较繁琐。 记录消息即可。 {"TypeName": "AddMsg", 回调消息类型"Appid": "wx_*_**_***", 设备appid"Wxid": "wxid_****…

从零学算法42

42.接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&#xff1a;6 解释&#xff1a;上面是由数组 [0,1,0,2,1,0,1,3…

短信公司_供应群发短信公司

短信公司——供应群发短信公司 短信公司作为一种为企业提供群发短信服务的服务商&#xff0c;正逐渐受到市场的青睐。供应群发短信公司作为其中的一种类型&#xff0c;为各行各业的企业提供高效、便捷的短信推广渠道。本文将介绍短信公司的作用以及供应群发短信公司的特点和优势…

基于springboot+mybatis+vue的项目实战之增删改查CRUD

目录结构 PeotController.java package com.example.controller;import com.example.pojo.Peot; import com.example.pojo.Result; import com.example.service.PeotService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web…

10大排序方法,其中这里只介绍前7种(第4种C语言,其它C++语言)

排序方法有十种&#xff0c;分别是&#xff1a;一、冒泡排序&#xff1b;二、选择排序&#xff1b;三、插入排序&#xff1b;四、希尔排序&#xff1b;五、归并排序&#xff1b;六、快速排序&#xff1b;七、堆排序&#xff1b;八、计数排序&#xff1b;九、桶排序&#xff1b;…

Lora训练笔记1——快速上手

准备工具 AKI大佬的整合包&#xff0c;一键解压即可。 度盘链接 提取码&#xff1a;p8uy 图片预处理 图片预处理&#xff1a;以一定规则裁剪原始的训练素材图片&#xff0c;并进行打标处理。 新建两个文件夹 input&#xff1a;存放原始图片的文件夹 preprocess-output:…

CTF-Web Exploitation(持续更新)

CTF-Web Exploitation 1. GET aHEAD Find the flag being held on this server to get ahead of the competition Hints Check out tools like Burpsuite to modify your requests and look at the responses 根据提示使用不同的请求方式得到response可能会得到结果 使用…

JavaScript初了解

JS的三种书写位置&#xff1a;行内&#xff0c;内嵌&#xff0c;外部 JS的注释的书写&#xff1a;单行注释&#xff1a;// ctrl/ 多行注释&#xff1a;/**/ ShiftAltA JavaScript输入输出语句

财政部、交通运输部:推动北斗导航等新技术与交通基础设施融合

财政部、交通运输部&#xff1a;推动北斗导航等新技术与交通基础设施深度融合 近日&#xff0c;为深入贯彻落实中共中央、国务院关于加快建设交通强国、数字中国等决策部署&#xff0c;推进公路水路交通基础设施数字转型、智能升级、融合创新&#xff0c;加快发展新质生产力&a…

FebHost:什么是域名DNS服务器?

域名服务器是一种将域名转换为IP地址的计算机。在域名系统&#xff08;DNS&#xff09;中&#xff0c;它起着至关重要的作用。用户只需在浏览器的地址栏输入域名&#xff0c;而无需手动输入网站服务器的IP地址&#xff0c;就可以访问网站。 每个已注册的域名都必须在其DNS记录…

Java基于B/S医院绩效考核管理平台系统源码java+springboot+MySQL医院智慧绩效管理系统源码

Java基于B/S医院绩效考核管理平台系统源码javaspringbootMySQL医院智慧绩效管理系统源码 医院绩效考核系统是一个关键的管理工具&#xff0c;旨在评估和优化医院内部各部门、科室和员工的绩效。一个有效的绩效考核系统不仅能帮助医院实现其战略目标&#xff0c;还能提升医疗服…

HFSS学习-day2-T形波导的优化设计

入门实例–T形波导的内场分析和优化设计 HFSS--此实例优化设计 优化设计要求1. 定义输出变量Power31、Power21、和Power11&#xff0c;表示Port3、Port2、Port1的输出功率2.参数扫描分析添加扫描变量和输出变量进行一个小设置添加输出变量进行扫描分析 3. 优化设计&#xff0c…

第十篇:数字堡垒:操作系统安全深度解析与实战指南

数字堡垒&#xff1a;操作系统安全深度解析与实战指南 1 *引言 1.1 数字世界的守护者 在遥远的比特海中&#xff0c;有一座名为“操作系统”的数字堡垒&#xff0c;它守护着我们的数据宝藏&#xff0c;确保每一次计算的航行都能安全抵达彼岸。然而&#xff0c;这片海域并非风…

Javaweb第五次作业

poet数据库sql语言 create table poet(id int unsigned primary key auto_increment comment ID,name varchar(10) not null comment 姓名,gender tinyint unsigned not null comment 性别, 说明: 1 男, 2 女,dynasty varchar(10) not null comment朝代,title varchar(20) not…

Flume进阶

目录 第1关&#xff1a;拦截器的使用 第2关&#xff1a;自定义拦截器 第1关&#xff1a;拦截器的使用 代码文件&#xff1a; # Define source, channel, sink #agent名称为a1# Define source #source类型配置为avro,监听8888端口&#xff0c;后台会自动发送数据到该端口 #拦截后…

248 基于matlab的GA-RBF神经网络预测

基于matlab的GA-RBF神经网络预测&#xff0c;遗传算法优化来训练RBF网络权值&#xff0c;RBF优化后的结果用于预测。输出真实值、RBF预测结果、GA-RBF预测结果&#xff0c;并进行对比。程序已调通&#xff0c;可直接运行。 248 RBF神经网络 GA-RBF 时间序列预测 - 小红书 (xiao…

OpenSSL实现AES的ECB和CBC加解密,可一次性加解密任意长度的明文字符串或字节流(QT C++环境)

本篇博文讲述如何在Qt C的环境中使用OpenSSL实现AES-ECB/CBC-Pkcs7加/解密&#xff0c;可以一次性加解密一个任意长度的明文字符串或者字节流&#xff0c;但不适合分段读取加解密的&#xff08;例如&#xff0c;一个4GB的大型文件需要加解密&#xff0c;要分段读取&#xff0c;…
最新文章