Solve ODE equations with Simulink and Matlab function
4 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Cris LaPierre
on 15 Jul 2021
Edited: Cris LaPierre
on 15 Jul 2021
If you haven't yet done so, go though Simulink Onramp. Chs 8-10 cover modeling dynamic systems in Simulink.
2 Comments
Cris LaPierre
on 16 Jul 2021
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.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!