Ode45 calling a matrix and an array in a function
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
%question 7
ts = [0,1,2,3];
q = zeros(4,length(ts));
% q(1:4,1) = 0.5;
[t,q] = ode45(@(q,ts) q_dotf(q,ts), ts, q_b); %where q_b is [0.5,0.5,0.5,0.5] in early part of code
%and the function is
function q_dot = q_dotf(q,ts)
a = 0.2*cos(0.05*ts);
b = 0.2*sin(0.05*ts);
c = zeros(1,length(ts));
c(1,:) = 0.1;
d = zeros(1,length(ts));
w_bbif = [a;b;c;d];
I3 = eye(3);
q4 = q(4);
q_13 = q(1:3);
q_13x = [0 -q(3) q(2);
q(3), 0, -q(1);
-q(2), q(1), 0];
a1 = [(q4*I3+q_13x) q_13;
-q_13', q4];
q_dot = 1/2*(a1*w_bbif);
end
I do not understand why i keep getting errors such as w_bbif is not concatenated, and that q4 = q(4) produces aan index error. I am trying to produce a code that implements ode45 to produce q from q_dot. But errors seem to arise when trying to put matrices in function. Any help would be wonderful.
2 commentaires
Réponse acceptée
Torsten
le 14 Avr 2023
Modifié(e) : Torsten
le 14 Avr 2023
Your arguments to q_dot are inverted:
Use
function q_dot = q_dotf(ts,q)
instead of
function q_dot = q_dotf(q,ts)
And note that ts in q_dot is not your original ts, but some value in between ts(1) and ts(end) depending on the progress of the integration.
ts = [0,1,2,3];
q_b(1:4,1) = 0.5;
[t,q] = ode45(@q_dotf, ts, q_b);
plot(t,q(:,1))
function q_dot = q_dotf(ts,q)
a = 0.2*cos(0.05*ts);
b = 0.2*sin(0.05*ts);
c = zeros(1,length(ts));
c(1,:) = 0.1;
d = zeros(1,length(ts));
w_bbif = [a;b;c;d];
I3 = eye(3);
q4 = q(4);
q_13 = q(1:3);
q_13x = [0 -q(3) q(2);
q(3), 0, -q(1);
-q(2), q(1), 0];
a1 = [(q4*I3+q_13x) q_13;
-q_13', q4];
q_dot = 1/2*(a1*w_bbif);
end
2 commentaires
Torsten
le 14 Avr 2023
Modifié(e) : Torsten
le 14 Avr 2023
I do not know why I had to switch q and ts but it works wonderfully now!
Because ode45 transfers time at the first position and the values of your solution variables at the second position in the list of inputs. The names of the variables don't matter, of course.
So if you write your function as
function q_dot = q_dotf(q,ts)
then q is time and ts are your solution variables.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
