Ode45 calling a matrix and an array in a function

14 次查看(过去 30 天)
%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 个评论
VBBV
VBBV 2023-4-14
移动:VBBV 2023-4-14
Change this line
q = zeros(4,length(ts));
To
q = zeros(1,length(ts));
Alex Belew
Alex Belew 2023-4-14
has to be the 4xn matrix because of definition of quaternion otherwise I would do that. Thank you so much for the answer!

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2023-4-14
编辑:Torsten 2023-4-14
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 个评论
Alex Belew
Alex Belew 2023-4-14
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 2023-4-14
编辑:Torsten 2023-4-14
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by