Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What is wrong with this code?

Subject: What is wrong with this code?

From: Daniel

Date: 18 Apr, 2013 19:04:10

Message: 1 of 2

i have a function for an ODE but i keep on getting this message

Error in ode_second_fun_main (line 5)
[x,y]=ode45(@second_fun,tspan,x0,options);

i cannot figure out what is wrong any help would be much appreciated




function ode_second_fun_main
options=odeset('RelTol',1e-6);
x0=[0;1];
tspan=[0,5];
[x,y]=ode45(@second_fun,tspan,x0,options);
figure;
hold on;
plot(x,y(:,1),'r');
legend('x','y');
ylabel('y');xlabel('x')
end




function dx_dt=second_fun(t,x)
dx_dt(1)=x(2);
dx_dt(2)=exp(t)*x(1)-sin(t)*x(2)+t^2-3*t;
end

Subject: What is wrong with this code?

From: Steven_Lord

Date: 19 Apr, 2013 15:23:41

Message: 2 of 2



"Daniel " <djtilley@student.bradford.ac.uk> wrote in message
news:kkpg3a$rij$1@newscl01ah.mathworks.com...
> i have a function for an ODE but i keep on getting this message
>
> Error in ode_second_fun_main (line 5)
> [x,y]=ode45(@second_fun,tspan,x0,options);

Since you didn't give the full text of the error message, I will (line
numbers may be a little different depending on the version of MATLAB you're
using.)

Error using odearguments (line 91)
SECOND_FUN must return a column vector.

Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0,
f0, odeArgs, odeFcn, ...

Error in ode_second_fun_main (line 5)
[x,y]=ode45(@second_fun,tspan,x0,options);

Have you checked whether or not your second_fun function returns a column
vector? Try putting this line right before your call to ODE45 in
ode_second_fun_main to check:

second_fun(0, x0)

Modify second_fun so that it does return a _column_ vector and see if that
fixes the problem.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us