gpt4 book ai didi

c++ - 无法获得 Matlab 生成的相同结果

转载 作者:行者123 更新时间:2023-11-30 03:28:02 34 4
gpt4 key购买 nike

看看下面的传递函数:

enter image description here

使用 Matlab Simulink:

enter image description here

结果是

enter image description here

在状态空间表示中,系统可以建模如下:

enter image description here

在 Matlab 中,我们可以在状态空间表示中对系统建模:

enter image description here

enter image description here

产生以下图:

enter image description here

这正是使用传递函数生成的结果。我试图用 odeint 生成相同的结果但失败了。这是代码

#include <iostream>
#include <Eigen/Dense>
#include <boost/numeric/odeint.hpp>
#include <iomanip>
#include <fstream>


using namespace std;
using namespace boost::numeric::odeint;

typedef std::vector< double > state_type;

void equations(const state_type &y, state_type &dy, double x)
{
Eigen::MatrixXd A(3, 3), B(3,1);

/*
x = y[0]
dx = y[1] = dy[0]
ddx = y[2] = dy[1]
dddx = dy[2]

*/
const double r(1);

A << 0, 1, 0,
0, 0, 1,
-24, -26, -9;

B << 0, 0, 1;
//#####################( ODE Equations )################################
Eigen::MatrixXd X(3, 1), dX(3,1);
X << y[0], y[1], y[2];
dX = A*X + B*r;

dy[0] = dX(0,0);
dy[1] = dX(1,0);
dy[2] = dX(2,0);
}


int main(int argc, char **argv)
{
const double dt = 0.01;
runge_kutta_dopri5 < state_type > stepper;

state_type y(3);
// initial values
y[0] = 0.0; // x1
y[1] = 0.0; // x2
y[2] = 0.0; // x3

ofstream data("file.txt");

for (double t(0.0); t <= 5.0; t += dt){
data << t << " " << 2*y[0] << " " << 7*y[1] << " " << 1*y[2] << std::endl;
stepper.do_step(equations, y, t, dt);
}

return 0;

}

这是所有状态 vector 的结果

enter image description here

前面的变量都不匹配 Matlab 生成的结果。有任何修复此代码的建议吗?

最佳答案

看看你对 y 的表达。当您将一个 1x3 矩阵与一个 3x1 矩阵相乘时,结果应该是一个 1x1 矩阵,其中单个元素的值是两个矩阵的点积。当您写入 data 而不是计算点积时,您当前正在做的是逐元素乘法。

关于c++ - 无法获得 Matlab 生成的相同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46941402/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com