Simple ODE solving using ODE45

I am trying to solve a ODE using ODE45, but Matlab keeps give me this message. I really don't understand where is the problem. Error using feval Undefined function 'func' for input arguments of type 'double'.
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, ...
My code is
function f = func(t)
f = 0.594689181*(1/(0.594689181)^2+(0.298222773)^4/4*(1/t)+(0.127125232)^2+4*(0.357907906)^2*t^2+9*(0.291984971)^2*t^4+16*(0.105174606)^2*t^6-(0.298222773)*(0.127125232)*1/(sqrt(t))-2*(0.298222773)*(0.357907906)*sqrt(t)+3*(0.298222773)*(0.291984971)*t*sqrt(t)-4*(0.298222773)*(0.105174606)*sqrt(t)*t^2+4*(0.127125232)*(0.357907906)*t-6*(0.127125232)*(0.291984971)*t^2+8*(0.127125232)*(0.105174606)*t^3-12*(0.357907906)*(0.291984971)*t^3+16*(0.357907906)*(0.105174606)*t^4-24*(0.291984971)*(0.105174606)*t^5)^0.5;
[xsolv tsolv] = ode45('func',[0.1:0.001:1.02], 0.12523);
end
As simple as that. Thanks in advance!

Answers (1)

The error I got initially was due to your not having two input arguments in your ODE function. It’s also best to not put your ODE solver call inside your ODE function. This can create recursion problems.
This works for me:
func = @(t,y) 0.594689181*(1/(0.594689181)^2+(0.298222773)^4/4*(1/t)+(0.127125232)^2+4*(0.357907906)^2*t^2+9*(0.291984971)^2*t^4+16*(0.105174606)^2*t^6-(0.298222773)*(0.127125232)*1/(sqrt(t))-2*(0.298222773)*(0.357907906)*sqrt(t)+3*(0.298222773)*(0.291984971)*t*sqrt(t)-4*(0.298222773)*(0.105174606)*sqrt(t)*t^2+4*(0.127125232)*(0.357907906)*t-6*(0.127125232)*(0.291984971)*t^2+8*(0.127125232)*(0.105174606)*t^3-12*(0.357907906)*(0.291984971)*t^3+16*(0.357907906)*(0.105174606)*t^4-24*(0.291984971)*(0.105174606)*t^5)^0.5;
[xsolv, tsolv] = ode45(func,[0.1:0.001:1.02], 0.12523);
I created ‘func’ as an anonymous function, and added the second argument (that must be in the function definition even if you don’t use it).

Asked:

on 8 Sep 2015

Answered:

on 8 Sep 2015

Community Treasure Hunt

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

Start Hunting!