How to use ode() solver

Hello, I have following code:
ftod_sym=detofJac(doy); %here I calculate my symbolic vectorfield ftod_sym which is a function of y1 y2 and t
myfun = matlabFunction(ftod_sym); %conversion
[T N]=ode15s(@(y,t)myfun,tspan,n0,optionsO); % n0 is a vector with 2 elements
I get following ERROR:
Error using odearguments (line 93)
@(Y,T)MYFUN returns a vector of length 1, but the length of initial conditions vector is 2. The vector returned
by @(Y,T)MYFUN and the initial conditions vector must have the same number of elements.
Error in ode15s (line 149)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in flipflop_NR (line 46)
[T N]=ode15s(@(y,t)myfun,tspan,n0,optionsO);
Thank You

Answers (1)

[T N]=ode15s(myfun,tspan,n0,optionsO);
You were not passing y and t to myfun, so you were not getting back the output you were expecting.

5 Comments

But now I get following ERROR:
Error using
makeFhandle/@(t,y1,y2)[(pi.*cos(pi.*t.*2.0e3).*(exp(y2.*(5.0e2./1.3e1)).*3.34887935568294e20+1.267650600228229e30).*9.833292471030398e37)./(exp(y1.*(5.0e2./1.3e1)).*1.793600770949138e54+exp(y2.*(5.0e2./1.3e1)).*5.452546343685381e54-exp(y1.*(5.0e2./1.3e1)).*exp(y2.*(5.0e2./1.3e1)).*1.311649693202332e45+2.063951224046247e64);(pi.*exp(y1.*(5.0e2./1.3e1)).*cos(pi.*t.*2.0e3).*-6.392393147662723e58)./(exp(y1.*(5.0e2./1.3e1)).*1.793600770949138e54+exp(y2.*(5.0e2./1.3e1)).*5.452546343685381e54-exp(y1.*(5.0e2./1.3e1)).*exp(y2.*(5.0e2./1.3e1)).*1.311649693202332e45+2.063951224046247e64)]
Not enough input arguments.
Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode15s (line 149)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in flipflop_NR (line 46)
[T N]=ode15s(myfun,tspan,n0,optionsO);
Looks like you split your y into two arguments instead of treating it as a vector. Easy to account for:
[T N]=ode15s( @(t,y) myfun(t, y(1), y(2)), tspan, n0, optionsO);
Thank you, but is there a more general possibility? I mean not typing explicit y(1), y(2) because sometimes I have more variables than two and I want to automating this task...thank you
Use subscripting of "y" right in your symbolic expression, and then just use myfun as the function handle.
For my symbolic expression I need for example the symbolic jacobian. What I do is:
y=sym('y',[2 1]);
syms t
f_algebraic=ALG_y(t,y); %my function
J = jacobian(f_algebraic, y);
I dont know how to do in another way...
Hope somone can help
Thank you

This question is closed.

Asked:

on 24 Jun 2012

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!