Need to fix error "index exceeds the number of array elements"

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

Antworten (2)

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
Star Strider am 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.)

Kategorien

Gefragt:

am 27 Nov. 2019

Beantwortet:

am 27 Nov. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by