Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab Simulink模拟的行为与使用rk4的同一模拟大不相同_Matlab_Simulink_Runge Kutta - Fatal编程技术网

Matlab Simulink模拟的行为与使用rk4的同一模拟大不相同

Matlab Simulink模拟的行为与使用rk4的同一模拟大不相同,matlab,simulink,runge-kutta,Matlab,Simulink,Runge Kutta,我很难在simulink中模拟由以下状态空间方程描述的对象: 状态空间方程的右侧由下面的函数描述 function dxdt = RHS( t, x, F) % parameters b = 1.22; % cart friction coeffitient c = 0.0027; %pendulum friction coeffitient g = 9.81; % gravity M = 0.548+0.022*2; % cart w

我很难在simulink中模拟由以下状态空间方程描述的对象:

状态空间方程的右侧由下面的函数描述

function dxdt = RHS( t, x, F)
% parameters
b = 1.22;          % cart friction coeffitient 
c = 0.0027;           %pendulum friction coeffitient
g = 9.81;           % gravity
M = 0.548+0.022*2; % cart weight
m = 0.031*2; %pendulum masses
I = 0.046;%0.02*0.025/12+0.02*0.12^2+0.011*0.42^2; % moment of inertia
l = 0.1313;
% x(1) = theta
% x(2) = theta_dot
% x(3) = x
% x(4) = x_dot


dxdt = [x(2);
    (-(M+m)*c*x(2)-(M+m)*g*l*sin(x(1))-m^2*l^2*x(2)^2*sin(x(1))*cos(x(1))+m*l*b*x(4)*cos(x(1))-m*l*cos(x(1))*F)/(I*(m+M)+m*M*l^2+m^2*l^2*sin(x(1))^2);
    x(4);
    (F - b*x(4) + l*m*x(2)^2*sin(x(1)) + (l*m*cos(x(1))*(c*x(2)*(M + m) + g*l*sin(x(1))*(M + m) + F*l*m*cos(x(1)) + l^2*m^2*x(2)^2*cos(x(1))*sin(x(1)) - b*l*m*x(4)*cos(x(1))))/(I*(M + m) + l^2*m^2*sin(x(1))^2 + M*l^2*m))/(M + m)];
end
具有简单可视化的响应rk4函数如下所示

function [wi, ti] = rk4 ( RHS, t0, x0, tf, N )

%RK4 approximate the solution of the initial value problem
%
% x'(t) = RHS( t, x ), x(t0) = x0
%
% using the classical fourth-order Runge-Kutta method - this 
% routine will work for a system of first-order equations as 
% well as for a single equation
%
% calling sequences:
% [wi, ti] = rk4 ( RHS, t0, x0, tf, N )
% rk4 ( RHS, t0, x0, tf, N )
%
% inputs:
% RHS string containing name of m-file defining the 
% right-hand side of the differential equation; the
% m-file must take two inputs - first, the value of
% the independent variable; second, the value of the
% dependent variable
% t0 initial value of the independent variable
% x0 initial value of the dependent variable(s)
% if solving a system of equations, this should be a 
% row vector containing all initial values
% tf final value of the independent variable
% N number of uniformly sized time steps to be taken to
% advance the solution from t = t0 to t = tf
%
% output:
% wi vector / matrix containing values of the approximate 
% solution to the differential equation
% ti vector containing the values of the independent 
% variable at which an approximate solution has been
% obtained
%

% x(1) = theta
% x(2) = theta_dot
% x(3) = x
% x(4) = x_dot
t0 = 0; tf = 5; x0 = [pi/2; 0; 0; 0]; N = 400;
neqn = length ( x0 );
ti = linspace ( t0, tf, N+1 );
wi = [ zeros( neqn, N+1 ) ];
wi(1:neqn, 1) = x0';

h = ( tf - t0 ) / N;
% force
u = 0.0;

%init visualisation
h_cart = plot(NaN, NaN, 'Marker', 'square', 'color', 'red', 'LineWidth', 6);
hold on
h_pend = plot(NaN, NaN, 'bo', 'LineWidth', 3);
axis([-5 5 -5 5]);
axis manual;
xlim([-5 5]);
ylim([-5 5]);

for i = 1:N
    k1 = h * feval ( 'RHS', t0, x0, u );
    k2 = h * feval ( 'RHS', t0 + (h/2), x0 + (k1/2), u);
    k3 = h * feval ( 'RHS', t0 + h/2, x0 + k2/2, u);
    k4 = h * feval ( 'RHS', t0 + h, x0 + k3, u);
    x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6;
    t0 = t0 + h;
    % model output
    wi(1:neqn,i+1) = x0';

    % model visualisation
    %plotting cart
    l = 2;
    set(h_cart, 'XData', x0(3), 'YData', 0, 'LineWidth', 5);
    %plotting pendulum
    %hold on;
   set(h_pend, 'XData', sin(x0(1))*l+x0(3), 'YData', -cos(x0(1))*l, 'LineWidth', 2);
   %hold off;
    % regulator
    pause(0.02);
end;

figure;
plot(ti, wi);
legend('theta', 'theta`', 'x', 'x`');
这为推车上的钟摆提供了逼真的结果。

现在来谈谈问题。 我想在simulink中重新创建完全相同的方程。我认为这将与创建以下simulink模型一样简单。 其中,我用RHS文件中的第二个和第四个等式填充fcn块。像这样

((M+M)*c*u(2)-(M+M)*g*l*sin(u(1))-M^2*l^2*u(2)^2*sin(u(1))*cos(u(1))+M*l*b*u(3)*cos(u(1))-M*l*cos(u(1))*u(4))/(I*(M+M)+M*M*l^2*sin u(1))^2)

(u(5)-b*u(4)+l*m*u(2)^2*sin(u(1))+(l*m*cos(u(1))*(c*u(2)*(m+m)+g*l*sin(u(1))*(m+m)+u(5)*l*m*cos(u(1))+l^2*m^2*u(2*cos(u(1))*sin u(1))-b*l*m*m*u(4)*cos(u(1))/(I*(m+m)+l*m*2*m+m^m)
问题是,这并没有给出上面的正确结果,而是下面的结果

有人知道我做错了什么吗

编辑:在@am304评论之后,我决定添加以下信息。我将simulink解算器的设置更改为使用固定步长rk4解算器,以便获得相同的结果。上述模型中的第二个积分器3已初始化为pi/2

编辑2:如果有人想自己查看simulink模型,请单击下载文件


编辑3:正如你在下面的答案中所看到的,这个问题是微不足道的。您可以下载正确的模型

我查看了您的Simulink模型,您可能混淆了您使用的两个功能。您使用了theta_dd函数,您想在其中放置x_dd,反之亦然

在模型中,还可以强制将
x_d
设置为常量值0。我假设您实际上打算将初始条件设置为0,您可以看到这是通过积分器块完成的
x_d
(作为
f
的输入)应该是状态向量,也是积分器的输出。这只是你定义x_d的结果,x_dd的积分,这也是RK4的工作原理;首先使用初始状态向量,然后使用预测的状态向量来驱动下一个RK4步骤

作用域的结果输出(我在这里输出了您的整个状态向量)如下所示,与您期望的结果类似:


我认为我不应该从外部链接到simulink文件,因此如果您想要该文件的副本,可以打开聊天室并索取。否则,上面的图片应该足以帮助您重现相同的结果。

只是想一想:您在积分器块中设置了什么初始条件?在代码中,将
theta
初始化为
pi/2
,其余初始化为
0
。另外,您使用的是什么解算器和解算器设置?@am304感谢您查看我的问题。我在问题末尾的编辑中添加了您询问的信息。好的,看起来您使用的是固定步长解算器。我会尝试使用较小的时间步长或可变步长解算器,如
ode15s
ode23t
,以确保问题不是数值不稳定。@am304我一直在这样做。我改变了步长,使用了各种解算器,但没有任何帮助。我还添加了一个链接来下载simulink模型。非常感谢。对不起,给你添了不必要的麻烦。作为我的借口,我写这篇文章的时候已经很晚了。我想这里唯一能学到的就是对每件事进行三次检查。