MATLAB: How to correct ode45 errors

error using feval

I am trying to execute the following code
clear all
clc
global mdot mass Ti UA Cp Ts
mdot=0.2;
mass=760;
Cp=2300;
UA=191.66;
Ts=150;
Ti=25;
tspan=[0:10:3000];
T0=25;
[t,T]=ode45(@(t,T) Heatingcoil(t,T,tspan,T0);
disp('Time(sec) Temp(deg cel)');
disp([t,T]);
plot(t,T);
xlabel('Time(sec)');
ylabel('Temp(deg cel)');
title('Time vs Temp');
dT=Heatingcoil(t,T);
global mdot mass Ti UA Cp Ts
dT=(mdot*(Ti-T)+(UA*(Ts-T)/Cp))/mass;
But I am getting the following erros:
Error using feval
Undefined function 'Heatingcoil' 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, …
Error in heatingwithcoil (line 12)
[t,T]=ode45(@Heatingcoil,tspan,T0);
Please help me in fixing these errors

Best Answer

First, do not use global variables!
Second, you need to declare your function correctly, and pass the extra parameters to it.
This works (in R2018b):
mdot=0.2;
mass=760;
Cp=2300;
UA=191.66;
Ts=150;
Ti=25;
tspan=[0:10:3000];
T0=25;
[t,T]=ode45(@(t,T) Heatingcoil(t,T,mdot, mass, Ti, UA, Cp, Ts),tspan,T0);
disp('Time(sec) Temp(deg cel)');
disp([t,T])
figure
plot(t,T);
xlabel('Time(sec)');
ylabel('Temp(deg cel)');
title('Time vs Temp');
function dT=Heatingcoil(t,T,mdot, mass, Ti, UA, Cp, Ts)
dT=(mdot*(Ti-T)+(UA*(Ts-T)/Cp))/mass;
end
See the documentation on Function Basics (link).
Related Question