MATLAB Answers

0

Keep getting this error and I cannot figure out why: Dimensions of arrays being concatenated are not consistent.

Asked by The Protoulis on 11 Nov 2018 at 1:08
Latest activity Edited by madhan ravi
on 11 Nov 2018 at 2:37
I have the following code trying to solve differential equation of second order:
[t,X] = ode45(@(t,X) odefun_4(t,X,u), [0 10], [0;0]);
function dx = odefun_4(t,x,u)
%xdisp(u);
m = 15;
b = 0.2;
k = 2;
%u = interp1(ut,u,t);
dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];
end
Vector u is a 1x100 vector. I expect to get a 2x100 matrix as the return value. What am I doing wrong ?

  0 Comments

Sign in to comment.

1 Answer

Answer by madhan ravi
on 11 Nov 2018 at 1:10
Edited by madhan ravi
on 11 Nov 2018 at 2:25
 Accepted Answer

EDITED
m = 15;
b = 0.2;
k = 2;
ut = linspace(0, 10);
u = 5 * sin(2 * ut) + 10.5; % input of our system - external forc
dx = @(t,x,u)[x(2); u/m - (b/m)*x(2) - (k/m)*x(1)]
for u=u
[t,X] = ode45(@(t,x)dx(t,x,u), [0 10], [0;0]); %function calling
plot(t,X)
hold on
end

  11 Comments

Each plot represents the change in the system according to each force this is the only way to solve your problem because the reason is like I told you before you can't put column vector with 100 values into a matrix of lesser column elements
Anytime :) make sure to accept the answer so that people know the question is solved

Sign in to comment.