Matlab error: No calculation, no increment in x

4 views (last 30 days)
amir azlan
amir azlan on 25 Dec 2013
Edited: dpb on 25 Dec 2013
clear all
h = 3;
n = 1;
T(n) = 0;
Q(n,1) = 0;
W(n,2) = 0;
% Let
% q'(t) = w(t) , q(0) = 0
% w'(t) = -w(t)-sind(q(t))+ cosd(180*t) , w(0) = 0
while T(n) < 360
T(n+1) = T(n)+ h;
K(1,1) = W(n,2);
K(1,2) = -W(n,2)- sind(Q(n,1)) + cosd(180*T(n));
K(2,1) = W(n,2);
K(2,2) = -(W(n,2)+(h/2)*K(1,2))-sind(Q(n,1))+cosd(180*((T(n))+h/2));
K(3,1) = W(n,2);
K(3,2) = -(W(n,2)+(h/2)*K(2,2))-sind(Q(n,1))+cosd(180*((T(n))+h/2));
K(4,1) = W(n,2);
K(4,2) = -(W(n,2)+(h)*K(3,2))-sind(Q(n,1))+cosd(180*((T(n))+h));
Q(n+1,1) = Q(n,1) + (h/6) * (K(1,1) + 2*K(2,1) + 2*K(3,1) + K(4,1));
W(n+1,2) = W(n,2) + (h/6) * (K(1,2) + 2*K(2,2) + 2*K(3,2) + K(4,2));
n = n + 1;
end
plot(T(:), Q(:,1), '.-', T(:), W(:,2), ':')
legend('t vs Q', 't vs W', 'Location', 'NW')
axis([0 360 0 10])
disp([T(:), Q(:,1), W(:,2)])
xlswrite('trial.xls', [T(:), Q(:,1), W(:,2)]);
can someone help me with this>is my coding wrong?
  1 Comment
dpb
dpb on 25 Dec 2013
Edited: dpb on 25 Dec 2013
I get a result albeit it starts to approach -inf very quickly; you've got a scaling problem I didn't investigate.
Try using
semilogy(T, [Q W(:,2)])
to get a better view.
Speaking of which, why are there two columns for W when only one would suffice?
The second subscript on Q as Q(n,1) is also superfluous and means more coding than needed; you can write simply Q(n) as you did with T.
I'm presuming this is a pedagogical exercise; if you were to try to use this for a large problem it would be mandatory to preallocate the two result vectors to prevent very large runtime penalties from the continual reallocation.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!