ODE45 error Inputs must be floats, namely single or double.

I am trying to make a plot of conversion using a differential equation. I have divided up my work into two m files. The first is this
function dy = HWFOOL(t,y)
T = 300+200*y(1);
Kc = 10*exp(-6000/1.987*(1/450-1/T));
k = .01*exp((10000/1.987)*(1/300-1/T));
Fao = 0.2;
Cao = 0.1;
ra = -k*(Cao^2)*((1-y(1))^2-y(1)*Cao/Kc);
dy = sym(zeros(1,1));
dy(1) = -ra/Fao;
This runs fine and returns
ans =
(exp(1844508686086227/109951162777600 - 5533526058258681/(1099511627776*(200*y + 300)))*((y - 1)^2 - (y*exp(6640231269910417/989560464998400 - 6640231269910417/(2199023255552*(200*y + 300))))/100))/2000
My second m file, which should plot the conversion is
timerange = [0 10];
initial = [0];
[T,Y]=ode45(@HWFOOL,timerange,initial);
plot(t,Y(:,1),'-');
title('Conversion')
This returns
>> plotODE
Error using odearguments (line 110)
Inputs must be floats, namely single or double.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in plotODE (line 5)
[T,Y]=ode45(@HWFOOL,timerange,initial);
I have tried using matlabFunction after googling my problem, shown below
HWFL = matlabFunction(HWFOOL(t,y))
timerange = [0 10];
initial = [0];
[T,Y]=ode45(HWFL,timerange,initial);
plot(t,Y(:,1),'-');
title('Conversion')
Which returns
>> plotODE
HWFL =
@(y)exp((-5.032712632108706e3)./(y.*2.0e2+3.0e2)+1.677570877369569e1).*((y-1.0).^2-y.*exp((-3.019627579265224e3)./(y.*2.0e2+3.0e2)+6.710283509478275).*(1.0./1.0e2)).*5.0e-4
Error using
symengine>@(y)exp((-5.032712632108706e3)./(y.*2.0e2+3.0e2)+1.677570877369569e1).*((y-1.0).^2-y.*exp((-3.019627579265224e3)./(y.*2.0e2+3.0e2)+6.710283509478275).*(1.0./1.0e2)).*5.0e-4
Too many input arguments.
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 plotODE (line 6)
[T,Y]=ode45(HWFL,timerange,initial);
I'm not sure how to get this to work, and I am relatively new to matlab. Any help would be greatly appreciated

 Accepted Answer

The only way that HWFOOL could have returned the result you show,
(exp(1844508686086227/109951162777600 - 5533526058258681/(1099511627776*(200*y + 300)))*((y - 1)^2 - (y*exp(6640231269910417/989560464998400 - 6640231269910417/(2199023255552*(200*y + 300))))/100))/2000
is if you had passed a symbolic y to HWFOOL. However, that will not happen in practice as you timerange and initial are both numeric.
Your objective function must return specific numeric values, not a symbolic function. There should be no sym() in that code. A numeric t and numeric y will be passed in and it must create a numeric result.
You should not be using matlabFunction.
Just delete the sym() line from the HWFOOL that you posted at the top, and run with the other lines you show in plotODE

1 Comment

Awesome! Works perfectly, thank you so much. I changed my files to
function dy = HWFOOL(t,y)
T = 300+200*y(1);
Kc = 10*exp(-6000/1.987*(1/450-1/T));
k = .01*exp((10000/1.987)*(1/300-1/T));
Fao = 0.2;
Cao = 0.1;
ra = -k*(Cao^2)*((1-y(1))^2-y(1)*Cao/Kc);
dy(1) = -ra/Fao;
and
timerange = [0 10];
initial = [0];
[T,Y]=ode45(@HWFOOL,timerange,initial);
plot(T,Y,'-');
title('Conversion')
Then ran
>> HWFOOL(t,y)
ans =
(exp(1844508686086227/109951162777600 - 5533526058258681/(1099511627776*(200*y + 300)))*((y - 1)^2 - (y*exp(6640231269910417/989560464998400 - 6640231269910417/(2199023255552*(200*y + 300))))/100))/2000
and then
>> plotODE
And it works perfectly! Again thanks a million, especially for responding so quick.

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

on 20 Nov 2015

Commented:

on 20 Nov 2015

Community Treasure Hunt

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

Start Hunting!