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');

답변 (1개)

Torsten
Torsten 2015년 11월 5일

0 개 추천

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

Image Analyst
Image Analyst 2015년 11월 5일
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
Torsten 2015년 11월 5일
Just run the code from above (without your "global Q").
It should run without errors.
Best wishes
Torsten.
Jan
Jan 2015년 11월 5일
@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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

태그

질문:

MJS
2015년 11월 5일

댓글:

Jan
2015년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by