首页 > 编程语言 >【c++】R-K法求解常微分方程

【c++】R-K法求解常微分方程

时间:2023-01-31 21:25:35浏览次数:32  
标签:count 求解 int double c++ func 微分方程 x0 dt

最常用:四阶龙格库塔方法

          

 

         

 

 例:

 

 

#include <iostream>
#include <fstream>
#include <cstring>

double func(double x) {
    return x * (1 - x);
}

void RK(double x0,double x[20]) {
    double dt{ 0.1 };
    double k1, k2, k3, k4;
    int count{ 0 };
    int flag{ 0 };
    for (int count{ 0 };count < 100;++count) {
        if (count % 5 == 0) {
            flag = count / 5;
            x[flag] = x0;
        }
        k1 = func(x0) * dt;
        k2 = func(x0 + 0.5 * k1) * dt;
        k3 = func(x0 + 0.5 * k2) * dt;
        k4 = func(x0 + 0.5 * k3) * dt;
        x0 += (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);

    }
}

void show(double* x, int num) {
    for (int i = 0;i < num;i++) {
        std::cout << x[i] << '\n';
    }
}

void savetxt(double* x,int num,std::string name) {
    std::ofstream write;
    write.open(name,std::ostream::app); //以添加模式打开文件
    for (int i = 0;i < num;i++) {
        write << x[i] << '\n';
    }
    write.close();
}



int main()
{
    double x[20]{};
    RK(0.5,x);
    //show(x, 20);
    savetxt(x, 20, "k2.txt");
   RK(1.0, x);
    savetxt(x, 20, "K.txt");
    RK(0.2, x);
    savetxt(x, 20, "less than k2.txt");
    RK(0.8, x);
    savetxt(x, 20, "more than k2.txt");
    RK(1.5, x);
    savetxt(x, 20, "more than k.txt");
}

 

使用vector:

/*
    x0:初值
    dt:步长
    D:总时长
    p:控制输出的数量
*/
vecRow RK2(double x0,double dt,int D,int p) {
    double k1, k2, k3, k4;
    int count{ 0 };
    int flag{ 0 };
    int t{ static_cast<int>(D / dt) };
    int num{ static_cast<int>(t / p) };
    vecRow x(num);
    for (int count{ 0 };count < t;++count) {
        if (count % 5 == 0) {
            flag = count / p;
            x[flag] = x0;
        }
        k1 = func(x0) * dt;
        k2 = func(x0 + 0.5 * k1) * dt;
        k3 = func(x0 + 0.5 * k2) * dt;
        k4 = func(x0 + 0.5 * k3) * dt;
        x0 += (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
    }
    return x;
}

 

标签:count,求解,int,double,c++,func,微分方程,x0,dt
From: https://www.cnblogs.com/zhimingyiji/p/17080783.html

相关文章

  • c++对文件的写入和读取操作
     写文件#include<iostream>usingnamespacestd;#include<fstream>voidtest01(){//1、包含头文件 //2、创建流对象 ofstreamofs;//3、指定打开方式 ofs.open("tes......
  • vscode 开发c++
     makefile.mk#makefile.mk公共头文件ifndefTARGET#/root/make/src/test_include#notdirTARGET:=$(notdir$(shellpwd))#test_includeendifCXXFLAGS:=$(CX......
  • 【c++】多项式曲线拟合
    源代码,截取至数值分析期末大作业。其中一步为多项式曲线拟合,求解出符合拟合精度的函数表达式。拟合和插值的区别?1.拟合:不必经过所有点2.插值:必须经过所有点(1)曲......
  • C++信奥赛题目 1154:亲和数
    1154:亲和数时间限制:1000ms      内存限制:65536KB提交数:41239   通过数:24946【题目描述】自然数a的因子是指能整除a的所有自然数,但不含a本......
  • 【KAWAKO】在windows上用CMake和MinGW编译c++工程
    目录安装CMake安装MinGW编写CMakeLists.txt编译一条龙安装CMake在网上随便找个教程照着安装就行了,不再赘述。安装MinGW参考这篇博客。从MinGW官网下载的安装包在安装的......
  • 【KAWAKO】TVM-使用c++进行推理
    目录前言修改cpp_deploy.cc文件修改DeployGraphExecutor()函数numpy与bin文件的互相转换numpy转binbin转numpy使用CMakeLists.txt进行编译运行前言在tvm工程的apps目录下......
  • c++
    #include<iostream>usingnamespacestd;intmain(intargc,char**argv){ inta; cin>>a; if(a==95||a==96||a==97||a==98||a==99||a==100){ cout<<"你获得了......
  • 小熊猫C++错误【Permission denied】与纠正
    问题很早以前,在使用VisualStudioC++时,就经常遭遇到如题所示的编译链接错误。【Permissiondenied】的意思很明确:无权限,不允许操作。什么原因导致这种错误呢?请大家结合自己......
  • C++知识点捕捉
    1.对于提高cin运行时间代码:ios::sync_with_stdio(false); cin.tie(0);//cin.tie(nullptr);减少运行时间,scanf永远的神13倍,……………………………………2、......
  • C++运算符重载引用传参与返回引用的小小心得
    1#include<bits/stdc++.h>23usingnamespacestd;45//平面向量类,提供完成向量运算和比较的API6//除递增运算符和左移运算符重载外其他函数省略78......