Ode45 calling a matrix and an array in a function

9 ビュー (過去 30 日間)
Alex Belew
Alex Belew 2023 年 4 月 14 日
編集済み: Torsten 2023 年 4 月 14 日
%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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by