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
채택된 답변
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
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 Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
