Need to fix error "index exceeds the number of array elements"
Show older comments
Dear friends, please guide me to solve the following problem
function xdot=func(t,w,x)
xdot(1)=x(2);
xdot(2)=-(w^2)*x(1);
xdot=xdot';
i need to code this differential equation and obtain solution and plot a graph of it. But the most common error i get is "index exceeds the number of array elements" which appears after i try to execute the following command:
[t,w,x]=ode45(@(t,x) func(t,w,x), tspan, x0);
please note w is a parameter in this system and i have already given values of w, tspan and x0.
it also says:
Error in func (line 2)
xdot(1)=x(2);
Thats all, I would appreciate if someone can help me out on this. Thanks
Answers (2)
Image Analyst
on 27 Nov 2019
Your x is probably a scalar, not a two element array. If, before that line you put
whos x
what does it show in the command window?
Star Strider
on 27 Nov 2019
The added parameter must be the third (or greater) argument:
function xdot=func(t,x,w)
and the ode45 call will then be:
[t,x]=ode45(@(t,x) func(t,x,w), tspan, x0);
I did not run your code because I am not certain what ‘w’ is. (It appears to ba a scalar.)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!