结果调用了一个外接图形库,以函数图形展示:

主cpp:

// wangdiedang is a good guy! =v=
#include<iostream>
#include<vector>
#include<cmath>
#include<iomanip>
#include<fstream>
#include "graph2d.h"
using namespace std;double z(double x,double y) {return (pow(x, 2) + 4 * x - y - 1) / 2;
}void R_K(double x,double y,double ul,double h) {vector<pair<double, double>> re;double k1, k2, k3, k4;re.push_back(make_pair(0, 0));while (x * h < ul) {k1 = z(x * h, y);k2 = z(x * h + h/2, y + h / 2 * k1);k3 = z(x * h + h/2, y + h / 2 * k2);k4 = z(x * h + h, y + h * k3);y += h / 6 * (k1 + 2 * k2 + 2 * k3 + k4);re.push_back(make_pair((++x) * h, y));}//for (int i = 0; i < re.size(); i++) {// if (i % 5 == 0) cout << endl;// cout << setiosflags(ios::fixed) << setprecision(9) << re[i].second << "  ";//}vector<double> x1, y1;for (int i = 0; i < re.size(); i++) {x1.push_back(re[i].first);y1.push_back(re[i].second);}graph2d g2d(700, 590, { 0,-0.06 }, { 0.5,0.04 });g2d.xlabel("x轴");g2d.ylabel("y 轴");g2d.title("结果");g2d.plot(x1, y1, BLACK);g2d.waitKey();
}int main() {double x, y, h, ul; // xy初始值和步长和阈值cin >> x >> y >> h >> ul;R_K(x, y, ul, h);return 0;
}

外接图库cpp:

#include "graph2d.h"
graph2d::graph2d()
{width = 700;height = 590;initgraph(int(width), int(height), SHOWCONSOLE);setGrid({ 0,0 }, { 10,10 });setlen();initAxis();
}
graph2d::graph2d(double _width, double _height)
{width = _width;height = _height;initgraph(int(width), int(height), SHOWCONSOLE);setGrid({ 0,0 }, { 10,10 });setlen();initAxis();
}
graph2d::graph2d(double _width, double _height, Point _pointlb, Point _pointrt)
{width = _width;height = _height;initgraph(int(width), int(height), SHOWCONSOLE);setGrid(_pointlb, _pointrt);setlen();initAxis();
}
graph2d::~graph2d()
{closegraph();
}
wchar_t* graph2d::ctow(const char* str)
{int length = strlen(str) + 1;wchar_t* t = (wchar_t*)malloc(sizeof(wchar_t) * length);memset(t, 0, length * sizeof(wchar_t));MultiByteToWideChar(CP_ACP, 0, str, strlen(str), t, length);return t;
}
void graph2d::setlen(int _len)
{x_len = max(int(1 + log10(max(pointlb.x, pointrt.x))), _len);y_len = max(int(1 + log10(max(pointlb.y, pointrt.y))), _len);
}
std::string graph2d::dtos(double _num, char _axis)
{int _len = 3;std::string _str = "";    // 储存结果int _lg = 0;            // 10^__lgif (_axis == 'x' || _axis == 'X')_len = x_len;if (_axis == 'y' || _axis == 'Y')_len = y_len;if (int(_num * pow(10.0, _len - 1)) == 0) {for (int i = 0; i < _len - 1; i++) {_str += '0';}return "0." + _str;}if (_num < 0) { _num = -_num; _str += '-'; }else { _str += ' '; }while (int(_num) > 1) { _num /= 10; _lg++; }while (int(_num) < 1 && _lg > 0) { _num *= 10; _lg--; }for (int i = 0; i < _len; i++) {_str += ('0' + int(_num) % 10);if (i == _lg && i + 1 != _len) {_str += '.';}_num *= 10;}return _str;
}
void graph2d::waitKey(int _delay)
{_getch();
}
void graph2d::setGrid(Point _pointlb, Point _pointrt)
{pointlb = { min(_pointlb.x,_pointrt.x), min(_pointlb.y,_pointrt.y) };pointrt = { max(_pointlb.x,_pointrt.x), max(_pointlb.y,_pointrt.y) };
}
void graph2d::drawGrid()
{setlinecolor(0xE0E0E0);setlinestyle(PS_SOLID, 1);for (int i = 1; i < 10; i++) {line(numConversion(0.078 * width * i + 0.12 * width, 'x'), numConversion(0.1 * height, 'y'), numConversion(0.078 * width * i + 0.12 * width, 'x'), numConversion(0.88 * height, 'y'));line(numConversion(0.12 * width, 'x'), numConversion(0.078 * height * i + 0.1 * height, 'y'), numConversion(0.9 * width, 'x'), numConversion(0.078 * height * i + 0.1 * height, 'y'));}
}
void graph2d::drawAxisX()
{std::string _str = "     ";for (int i = 0; i <= 10; i++) {_str += dtos((pointrt.x - pointlb.x) / 10 * i + pointlb.x, 'x') + "  ";}RECT r = { numConversion(0,'x'), numConversion(0.04 * height,'y'), numConversion(width,'x'), numConversion(0.11 * height,'y') };settextcolor(BLACK);settextstyle(int((24 - 2 * x_len) * (width / 700) * 0.9), 0, _T("宋体"));drawtext(ctow(_str.c_str()), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
void graph2d::drawAxisY()
{std::string _str = "\n\n\n\n\n";for (int i = 10; i >= 0; i--) {_str += dtos((pointrt.y - pointlb.y) / 10 * i + pointlb.y, 'y') + "\n\n\n";}RECT r = { numConversion(0,'x'), numConversion(height,'y'), numConversion(0.11 * width,'x'), numConversion(0,'y') };settextcolor(BLACK);settextstyle(int(15 * height / 590), 0, _T("宋体"));drawtext(ctow(_str.c_str()), &r, DT_NOCLIP | DT_RIGHT);
}
bool graph2d::isBorder(Point _point)
{return (pointlb.x <= _point.x && _point.x <= pointrt.x && pointlb.y <= _point.y && _point.y <= pointrt.y);
}
int graph2d::numConversion(double _num, char _axis)
{if (_axis == 'x' || _axis == 'X') {return int(_num);}if (_axis == 'y' || _axis == 'Y') {return int(height - _num);}return 0;
}
Point graph2d::fucCSDataToAbsCSData(Point _point)
{return { ((_point.x - pointlb.x) / (pointrt.x - pointlb.x) * 0.78 * width + 0.12 * width),((_point.y - pointlb.y) / (pointrt.y - pointlb.y) * 0.78 * height + 0.1 * height) };
}
Point graph2d::absCSDataToFucCSData(Point _point)
{return { ((_point.x - 0.12 * width) * (pointrt.x - pointlb.x) / 0.78 / width + pointlb.x),((_point.y - 0.1 * height) * (pointrt.y - pointlb.y) / 0.78 / height + pointlb.y) };
}
void graph2d::showError(std::string _err)
{std::cout << "Maybe a " << _err << " is not in the coordinates" << std::endl;
}
void graph2d::setBackgroundColor(COLORREF _color)
{setbkcolor(_color);cleardevice();
}
void graph2d::setAxisColor()
{drawRectangle({ 0.12 * width ,0.1 * height }, { 0.9 * width ,0.88 * height }, BLACK, WHITE);
}
void graph2d::drawRectangle(Point _pointlb, Point _pointrt, COLORREF _colorl, COLORREF _colorf, int _style)
{setlinecolor(_colorl);setfillcolor(_colorf);setfillstyle(_style);fillrectangle(numConversion(_pointlb.x, 'x'), numConversion(_pointrt.y, 'y'), numConversion(_pointrt.x, 'x'), numConversion(_pointlb.y, 'y'));
}
void graph2d::initAxis()
{setBackgroundColor();setAxisColor();drawGrid();drawAxisX();drawAxisY();
}
void graph2d::plot(Point _point, COLORREF _color, int _size, int _type)
{if (isBorder(_point)) {setlinecolor(_color);setfillcolor(_color);setfillstyle(_type);fillcircle(numConversion(fucCSDataToAbsCSData(_point).x, 'x'), numConversion(fucCSDataToAbsCSData(_point).y, 'y'), _size);}else {showError("point");}
}
void graph2d::plot(std::vector<Point> _point, COLORREF _color, int _thickness, int _type)
{setlinecolor(_color);setlinestyle(_type, _thickness);for (int i = 1; i < _point.size(); i++) {if (isBorder(_point[i - 1]) && isBorder(_point[i])) {line(numConversion(fucCSDataToAbsCSData(_point[i - 1]).x, 'x'), numConversion(fucCSDataToAbsCSData(_point[i - 1]).y, 'y'), numConversion(fucCSDataToAbsCSData(_point[i]).x, 'x'), numConversion(fucCSDataToAbsCSData(_point[i]).y, 'y'));}else {showError("line");}}
}
void graph2d::plot(std::vector<double> x, std::vector<double> y, COLORREF _color, int _thickness, int _type)
{std::vector<Point> _point;for (int i = 0; i < x.size(); i++) {_point.push_back({ x[i], y[i] });}plot(_point, _color, _thickness, _type);
}
void graph2d::title(std::string _str)
{RECT r = { numConversion(0,'x'), numConversion(0.88 * height,'y'), numConversion(width,'x'), numConversion(height,'y') };settextcolor(BLACK);settextstyle(int(20 * height / 590), 0, _T("宋体"));drawtext(ctow(_str.c_str()), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
void graph2d::xlabel(std::string _str)
{RECT r = { numConversion(0,'x'), numConversion(0,'y'), numConversion(width,'x'), numConversion(0.05 * height,'y') };settextcolor(BLACK);settextstyle(int(20 * height / 590), 0, _T("宋体"));drawtext(ctow(_str.c_str()), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
void graph2d::ylabel(std::string _str)
{RECT r = { numConversion(0.01 * width,'x'), numConversion(0.6 * height,'y'), numConversion(0.05 * width,'x'), numConversion(0,'y') };settextcolor(BLACK);settextstyle(int(20 * width / 700), 0, _T("宋体"));drawtext(ctow(_str.c_str()), &r, DT_NOCLIP | DT_LEFT | DT_WORDBREAK);
}
void graph2d::text(std::string _str)
{//TODO
}
void graph2d::legend(std::string _str, COLORREF _color, int _num)
{//TODO
}
void graph2d::mouseClick()
{//TODO
}

外接头文件.h

#pragma once
#pragma once
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <vector>
#include <math.h>
#define f(_lambda) ([](double x) {return (_lambda); })
typedef struct mypoint {double x;double y;
}Point;
class graph2d
{
private:double height;                                          // 画板高度double width;                                            // 画板宽度Point pointlb;                                           // 坐标轴左下角的点Point pointrt;                                           // 坐标轴右上角的点int x_len;                                               // x轴字的宽度int y_len;                                             // y轴字的宽度
public:/*********************************** 初始化、坐标转换、绘制坐标等等**********************************/graph2d();                                                // 初始化为默认值graph2d(double _width, double _height);                   // 初始化画板宽高graph2d(double _width, double _height, Point _pointlb, Point _pointrt);       // 初始化画板宽高及网格~graph2d();                                                // 析构函数void waitKey(int _delay = 0);                           // 等待关闭
private:/***********************************    坐标转换、绘制坐标等等**********************************/wchar_t* ctow(const char* str);                           // char* to wchar_t*void setlen(int _len = 3);                             // 设置坐标文本长度std::string dtos(double _num, char _axis);               // string to doublevoid setGrid(Point _pointlb, Point _pointrt);            // 设置网格void drawGrid();                                     // 绘制网格void drawAxisX();                                        // 绘制X轴void drawAxisY();                                        // 绘制Y轴bool isBorder(Point _point);                         // 是否在边界内int numConversion(double _num, char _axis);                // 将y轴颠倒Point fucCSDataToAbsCSData(Point _point);               // 方程的点转换到画板的点Point absCSDataToFucCSData(Point _point);             // 画板的点转换到方程的点void showError(std::string _err);                     // 显示错误void drawRectangle(Point _pointlb, Point _pointrt, COLORREF _colorl, COLORREF _colorf, int _style = BS_SOLID);  //画正方形void setBackgroundColor(COLORREF _color = 0xEAEAEA); // 设置画板背景颜色void setAxisColor();                                 // 设置坐标系背景颜色void initAxis();                                        // 初始化坐标轴内的信息void mouseClick();                                     // 鼠标点击来获取该点函数坐标
public:/*********************************** 绘制坐标方程函数**********************************/void plot(Point _point, COLORREF _color = RED, int _size = 3, int _type = BS_SOLID);      // 绘制点void plot(std::vector<Point> _point, COLORREF _color = BLACK, int _thickness = 3, int _type = PS_SOLID);     // 绘制一连串的线void plot(std::vector<double> x, std::vector<double> y, COLORREF _color = BLACK, int _thickness = 3, int _type = PS_SOLID);        // 绘制f(x)方程void title(std::string _str);                            // 标题void xlabel(std::string _str);                         // x轴文本注释void ylabel(std::string _str);                         // y轴文本注释,可以用空格换行void text(std::string _str);                           // 在坐标中添加注释void legend(std::string _str, COLORREF _color, int _num);// 标签
};

数值分析试验四 runge_kutta 龙格库塔c++代码相关推荐

  1. 欧拉编程c语言作业数值分析,数值分析作业 欧拉 龙格库塔

    <数值分析作业 欧拉 龙格库塔>由会员分享,可在线阅读,更多相关<数值分析作业 欧拉 龙格库塔(5页珍藏版)>请在人人文库网上搜索. 1.9.2对初值问题试用欧拉方法取步长h= ...

  2. 四阶龙格库塔方程(Rungekutta)解二阶常微分方程组并计算船舶在迎浪下的纵摇埀荡耦合运动方程-附Matlab代码

    今年年初的时候给师姐做了DDPG算法的船舶减横摇控制算法,师姐还有想法要让我把纵摇-埀荡两个自由度的减摇也做出来,这个任务归我了.实际上不管是多少个自由度的减摇,其实都需要解运动方程,当初做单自由度横 ...

  3. 【Hydro】龙格-库塔方法的公式推导

    一.龙格-库塔方法的公式推导 摘自<数值计算方法(MATLAB版)> 二.龙格-库塔方法在水库调洪演算的应用 一般库水面坡降很小,忽略动库容影响,近似看成静水面,水库蓄水量V只随坝前水位Z ...

  4. 四阶龙格库塔方程解二阶常微分方程组并计算船舶在迎浪下的纵摇埀荡耦合运动方程-附Python代码

    0 写在前面 这篇博客是在将我上一篇博客的matlab代码移植到python中,应为后续要开展深度强化学习下的船舶减摇研究,总的来说还是在python上做这项工作比较合适.具体方程的解法和背景不在赘述 ...

  5. 数值分析—四阶龙格库塔python复现

    数值分析-四阶龙格库塔python复现 python入门,复现<数值分析>中的欧拉.龙格库塔,这里以经典四阶龙格库塔来示例. 四阶龙格库塔公式 y_(n+1)=y_n+h/6 (K_1+2 ...

  6. 2021-01-07 matlab数值分析 常微分方程初边值问题数值解 标准龙格库塔四阶四段公式 欧拉法

    matlab数值分析 常微分方程初边值问题数值解 标准龙格库塔四阶四段公式 欧拉法 1.标准龙格库塔四阶四段公式 function y=rk4(fun,a,b,y0,n) h=(b-a)/n; y(1 ...

  7. 数值分析C++实现用四阶龙格-库塔(Runge-Kutta)方法求解常微分方程初值问题

    问题:用四阶龙格-库塔(Runge-Kutta)方法求解常微分方程初值问题. 算法描述 算法的程序框图: 具体算法: (1)读取a,b,n,f (2)计算步长h = (b-a)/n,x0=a,y0=f ...

  8. [计算机数值分析]四阶龙格-库塔经典格式解常微分方程的初值问题

    龙格-库塔方法的设计思想: 四阶龙格-库塔方法的经典格式: 程序设计框图: 例:设取步长h=0.2,从x=0到x=1用四阶经典格式解决以下常微分方程的初值问题. 运行示例: 源码: #include& ...

  9. 常见的数值积分方法_欧拉积分/中值积分/龙格-库塔积分

    参考:常见的数值积分方法 (欧拉.中值.龙格-库塔,[常用于IMU中]) 1. 积分基本概念 设F(x)为函数f(x)的一个原函数,我们把函数f(x)的所有原函数F(x)+C(C为任意常数)叫做函数f ...

最新文章

  1. JavaScript获取当前日期,昨天,今天日期以及任意天数间隔日期
  2. python lambda函数_python入门基础之lambda匿名函数
  3. DPM2012保护sharepoint场
  4. 实战(多图):旧路由器刷panabit系统!一台路由器的新生……
  5. 计算机 网络访问保护,开启win2008网络访问保护的具体步骤
  6. JS中的Math.ceil和Math.floor函数的用法
  7. MSIL实用指南-比较运算
  8. Oracle CRS stack is already configured and will be running under init(1M)
  9. git学习(10):Git的使用--如何将本地项目上传到Github(两种简单、方便的方法)
  10. 使用sqlite3创建数据库表的时候须要注意
  11. 为右键新建菜单添加内容
  12. 解决Lost connection to MySQL server at 'reading initial communication packet', 的方法
  13. 华为设备BGP中的路由控制与实验
  14. 虚拟化--001 view win7优化
  15. Python编程工具:Jupyter notebook
  16. 春季养生知识多 吃萝卜可预防上火
  17. 删除vlan 华为s5720_华为S5720系列交换机快速配置手册常用命令
  18. Dev GridView 隔行换色的方法
  19. vissim跟驰模型_vissim简介
  20. 单片机 蓝牙/WIFI无线遥控智能家居系统设计

热门文章

  1. 秒数转换为天,小时,分钟,秒的公式
  2. C语言修饰词之violate使用
  3. 串级PID的一些理解
  4. 后台怎么接收处理从url 客户端传来的json数据格式
  5. 打开ps提示计算机中丢失,ps打开出现dll文件丢失怎么解决
  6. python 绘制箱型图
  7. 使用数字陷波器滤除工频信号
  8. 如何修改PyCharm窗口背景颜色?
  9. 总结python中的乱码问题
  10. 干货分享:RS485通信和Modbus通信协议汇总