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

답변 (1개)

Walter Roberson
Walter Roberson 2012년 6월 24일

0 개 추천

[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

John Miller
John Miller 2012년 6월 24일
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);
Walter Roberson
Walter Roberson 2012년 6월 24일
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);
John Miller
John Miller 2012년 6월 24일
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
Walter Roberson
Walter Roberson 2012년 6월 24일
Use subscripting of "y" right in your symbolic expression, and then just use myfun as the function handle.
John Miller
John Miller 2012년 6월 24일
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

이 질문은 마감되었습니다.

질문:

2012년 6월 24일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by