Keep getting this error and I cannot figure out why: Dimensions of arrays being concatenated are not consistent.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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 ?
Accepted Answer
madhan ravi
on 11 Nov 2018
Edited: madhan ravi
on 11 Nov 2018
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
u must be a scalar
Why scalar ? I am simulating a system and I want to get one output value for each input value.
madhan ravi
on 11 Nov 2018
Edited: madhan ravi
on 11 Nov 2018
provide u values , plus your solution returns is 57 by 2 matrix how can you concatenate 1 by 100 in that matrix ?
u is created like this:
ut = linspace(0, 10);
u = 5 * sin(2 * ut) + 10.5; % input of our system - external force
see edited answer
Tried the edited version of your answer and got this:
@(T,X)ODEFUN_4(T,X,U) returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by @(T,X)ODEFUN_4(T,X,U) and the initial conditions vector must have the same number of elements.
Did you try my edited answer? I got the graph. Make sure to accept the answer if you got the answer to your question.
Yes, now I got the graph. I wonder why I don't get 100 values as output since I use 100 values as input.
madhan ravi
on 11 Nov 2018
Edited: madhan ravi
on 11 Nov 2018
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
Thank you very much!
madhan ravi
on 11 Nov 2018
Edited: madhan ravi
on 11 Nov 2018
Anytime :) make sure to accept the answer so that people know the question is solved
More Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)