Please how can I plot the graph of the function it keep giving me Error in QCS2 (line 13), ode45 (line 115) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
1 view (last 30 days)
Show older comments
%%System Parameters
M = [400 0;0 40];
C= [12000 -12000;-12000 12125];
K = [80000 -80000;-80000 280000];
B = [0 0;200000 125];
[t,y] = ode45(@(t,y) MSsys(t,y,M,K,C,B),[0 20],[0 0 0 0])
x1 = y(:,1); x2 = y(:,2);
plot(t,x1,t,x2)
xlabel('Time (seconds)')
ylabel('x_1(t)and x_2(t) (m/sec)')
title('Speed Bump')
%%ODEs
function dydt= MSsys(t,y,M,K,C,B)
A = [zeros(2) eye(2);-inv(M)*K -inv(M)*C];
F = [0 0;200000 125]*0.03*sin(50*t);
dydt = A*y+[zeros(2);inv(M)*B]*F;
end
3 Comments
Answers (1)
Walter Roberson
on 16 Dec 2018
Your code cannot be corrected. Your function needs to calculate exactly two derivatives but you are calculating numerous more (16 total I think). Your equations are wrong for the situation and you need to find the right ones.
I get the impression you might be working with a state-space system?
1 Comment
Walter Roberson
on 17 Dec 2018
The result of [zeros(2);inv(M)*B] is 4 x 2
Your F is 2 x 2.
The result of * between a 4 x 2 and a 2 x 2 is 4 x 2, so [zeros(2);inv(M)*B]*F is 4 x 2.
Your A is 4 x 4. Your y is 4 x 1.
The result of * between a 4 x 4 and a 4 x 1 is 4 x 1, so A*y is 4 x 1.
You are adding a 4 x 1 + 4 x 2. In MATLAB before R2016b that would be an error. In R2016b and later, it is treated as if you had bsxfun, like
dydt = bsxfun(@plus, A*y, [zeros(2);inv(M)*B]*F);
so the x 1 of the A*y is replicated to 4 x 2, giving you a 4 x 2 + 4 x 2, which has a 4 x 2 result.
Therefore your dydt is a 4 x 2 matrix.
Now, you could get around your immediate error message by putting
dydt = dydt(:);
at the end of your function, so that the function returned 8 x 1: you would no longer get the error about "must return a column vector". Instead you would get an error:
@(T,Y)MSSYS(T,Y,M,K,C,B) returns a vector of length 8, but the length of initial conditions vector is 4. The vector returned by @(T,Y)MSSYS(T,Y,M,K,C,B) and the initial conditions vector must
have the same number of elements.
You need to rework the equations, or at the very least you need to rework the logic of
[zeros(2);inv(M)*B]*F
so that you end up with 4 x 1 as the result instead of 4 x 2.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!