Solve ODE equations with Simulink and Matlab function

5 次查看(过去 30 天)
Hi,
I'm trying to solve a differential equation using Simulink, the matlab funtion I want to use is the following:
function ud = single_pendulum_3(u)
global s1_A_local uz grav m N1 alpha beta J1_local Phi Phi_d Lag
c = u(1:7); % Baricentral position and orientation with 4 Euler parameters
c_d = u(8:13); % Baricentral velocity
%% Body
% Position
r1 = c(1:3);
e1_0 = c(4); % First Euler parameter
e1_1 = c(5); % Second Euler parameter
e1_2 = c(6); % Third Euler parameter
e1_3 = c(7); % Fourth Euler parameter
e1 = c(5:7);
p1 = c(4:7);
e1_til = [0, -e1_3, e1_2;
e1_3, 0, -e1_1;
-e1_2, e1_1, 0];
G1 = [-e1, e1_til + e1_0*eye(3)];
L1 = [-e1, -e1_til + e1_0*eye(3)];
A1 = G1*L1';
% Velocity
r1_d = c_d(1:3);
omega1_local = c_d(4:6);
p1_d = 0.5*L1'*omega1_local;
omega1 = 2*G1*p1_d;
omega1_til_local = [0, -omega1_local(3), omega1_local(2); % pag. 177
omega1_local(3), 0, -omega1_local(1);
-omega1_local(2), omega1_local(1), 0];
omega1_til = [0, -omega1(3), omega1(2); % pag. 177
omega1(3), 0, -omega1(1);
-omega1(2), omega1(1), 0];
s1_A = A1*s1_A_local;
r1_A = r1 + s1_A;
s1_A_d = omega1_til*s1_A; % (6.101) pag. 192
s1_A_til = [0, -s1_A(3), s1_A(2);
s1_A(3), 0, -s1_A(1);
-s1_A(2), s1_A(1), 0];
%% Gravitational forces
w1 = m*grav*uz;
%% Formulation
f1 = [60;
0;
-norm(w1)];
n1_local = [0;
0;
0];
M = [N1, zeros(3,3);
zeros(3,3), J1_local];
g = [f1;
n1_local];
b = [zeros(3,1);
omega1_til_local*J1_local*omega1_local];
B = [eye(3), -s1_A_til*A1];
Phi = r1_A;
c_d = [r1_d; omega1_local];
Phi_d = B*c_d;
gamma_sharp = omega1_til*s1_A_d;
gamma_sharp = gamma_sharp - 2*alpha*Phi_d - (beta^2)*Phi;
BMB = [M, B';
B, zeros(3,3)];
rhs = [g - b;
gamma_sharp];
solution = BMB\rhs;
%% Extract accelerations
r1_dd = solution(1:3);
omega1_local_d = solution(4:6);
c_dd = [r1_dd; omega1_local_d];
%% Extract Lagrange moltiplicators
Lag = solution(7:9);
%% Construct ud array
c_d = [r1_d; p1_d];
ud = [c_d; c_dd];
end
Normally I would use ode45 to integrate vector ud (velocity and acceleration) to obtain vector u (position and velocity).
I would like to do so using Simulink but I don't know how.
I have already tryied to do this as can be seen from the following figure but it seems doesn't work:
Could gently someone explain me how to solve this simple pendulum dynamics using Simulink connected to the function I have posted here?
Thank you in advice,
Gregory

回答(1 个)

Cris LaPierre
Cris LaPierre 2021-7-15
编辑:Cris LaPierre 2021-7-15
If you haven't yet done so, go though Simulink Onramp. Chs 8-10 cover modeling dynamic systems in Simulink.
  2 个评论
Gregory Cottone
Gregory Cottone 2021-7-16
Dear Cris,
thank you for the answer, but unfortunately, after having completed the chapetr you mentioned, I'm still having truble to model my system.
The question is not purely related to the pensulum system I want to model but is a more general question related to the use of integrators and matlab functions in Simulink environment.
In particular I would like to understand how to model a system consisting in an integrator which integrates the output of a matlab function and sends the result as the input to the matlab function itself:
The Matlab function contains the following equation:
which is the inverse form of this:
Now, I'm able to solve this problem by using only Matlab, but I would like to understand if I can do this by using Simulink that calls a Matlab function for each iteration of integration.
Thank you in advice and best regards,
Gregory
Cris LaPierre
Cris LaPierre 2021-7-16
I think these 2 slides from the Ch 10 summary are helpful in seeing how to solve odes in Simulink. It shows how to turn and into and V.
The second slide shows how to capture and V to use in other blocks, including a MATLAB Function.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by