Ode45 calling a matrix and an array in a function

9 views (last 30 days)
%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 Comments
VBBV
VBBV on 14 Apr 2023
Moved: VBBV on 14 Apr 2023
Change this line
q = zeros(4,length(ts));
To
q = zeros(1,length(ts));
Alex Belew
Alex Belew on 14 Apr 2023
has to be the 4xn matrix because of definition of quaternion otherwise I would do that. Thank you so much for the answer!

Sign in to comment.

Accepted Answer

Torsten
Torsten on 14 Apr 2023
Edited: Torsten on 14 Apr 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 Comments
Alex Belew
Alex Belew on 14 Apr 2023
Thank you so much for your answer, I do not know why I had to switch q and ts but it works wonderfully now!
Torsten
Torsten on 14 Apr 2023
Edited: Torsten on 14 Apr 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox 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!