Error using ODE 45
Show older comments
I am at a lost with these error messages. Can any one please help me? I have post my code with the error.
Error using odearguments (line 92) RHS returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned by RHS and the initial conditions vector must have the same number of elements.
Error in ode45 (line 113) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in file1 (line 6) [t,x]=ode45(@rhs, [0,30],[0,0]) ;
function dxdt = rhs(x,Q) global Q dxdt_1=x(2); dxdt_2=-2*Q*x(2)-x(1)+1; dxdt=[dxdt_1; dxdt_2]; end
clc; clear all; close all; [t,x]=ode45(@rhs, [0,30],[0,0]) ; plot(t,x(:,2)); xlabel('t'); ylabel('x');
Answers (1)
Torsten
on 5 Nov 2015
Does this work ?
function main
clc;
clear all;
close all;
Q=1;
[t,x]=ode45(@(t,x)rhs(t,x,Q),[0 30],[0 0]);
plot(t,x(:,2))
xlabel('t')
ylabel('x')
function dxdt = rhs(t,x,Q)
dxdt_1=x(2);
dxdt_2=-2*Q*x(2)-x(1)+1;
dxdt=[dxdt_1; dxdt_2];
Best wishes
Torsten.
3 Comments
Image Analyst
on 5 Nov 2015
MJS's "Answer" moved here because it's not an answer to the original question:
No it gave me more errors. I need ode45 to solve for Q as it is a unknown value. Thanks for the updated code.
Error using rhs
Too many input arguments.
Error in file1>@(t,x)rhs(t,x,Q) (line 6)
[t,x]=ode45(@(t,x)rhs(t,x,Q),[0 30],[0 0]);
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in file1 (line 6)
[t,x]=ode45(@(t,x)rhs(t,x,Q),[0 30],[0 0]);
Torsten
on 5 Nov 2015
Just run the code from above (without your "global Q").
It should run without errors.
Best wishes
Torsten.
Jan
on 5 Nov 2015
@Torsten: A celar all on top of a function is a waste of time with no useful effect. It clears all local variables, but inside a function there are none at the beginning. Beside this, it deletes all loaded M-files from the memory and reloading them from the hard disk is very slow. The deleting of all breakpoints of the debugger is a very bad idea also.
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!