MATLAB: How to fix the error ”Conversion to function_handle from double is not possible.” for the below 3Eq euler matlab code

conversion to function_handle from double is not possible.

Hello. I have written the first-order Euler code to solve three differential equations, but it has been mistaken. What is the problem?
a=0; %initial time
b=20; %final time
h = 0.01; % time step
N = (b-a)/h;
%A = zeros(1,N+1);
%B = zeros(1,N+1);
%T = zeros(1,N+1);
t=a:h:b;
A = 10.1; % initial condition
B = 0;
T = 0;
Ca = @(t,A,B,T) -(A/(300+t))-(1.37*10^12*exp(-12628/T))*A*B;
Cb = @(t,A,B,T) (9.7-B)/(300+t)-(1.37*10^12*exp(-12628/T))*A*B;
dT = @(t,A,B,T) 16.92*(1.37*10^12*exp(-12628/T))*A*B-((T-328)/(300+t))-(0.253*10^-3)*(T-309);
for i=1:N
Ca(i+1) = A(i) + h*Ca(t(i), A(i), B(i), T(i));
Cb(i+1) = B(i) + h*Cb(t(i), A(i), B(i), T(i));
dT(i+1) = T(i) + h*dT(t(i), A(i), B(i), T(i));
t(i+1)=a+i*h;
end
plot(t,Ca,'-',t,Cb,'.-',t,dT,'--')
%plot(v,f,v,g);

Best Answer

First, name the variables slightly differently from the function names, second define the initial conditions to conform with those, and third, preallocate the vectors.
Try this:
a=0; %initial time
b=20; %final time
h = 0.01; % time step
N = (b-a)/h;
A = zeros(1,N+1);
B = zeros(1,N+1);
T = zeros(1,N+1);
t=a:h:b;
A(1) = 10.1; % initial condition
B(1) = 0;
T(1) = 0;
Ca = @(t,A,B,T) -(A/(300+t))-(1.37*10^12*exp(-12628/T))*A*B;
Cb = @(t,A,B,T) (9.7-B)/(300+t)-(1.37*10^12*exp(-12628/T))*A*B;
dT = @(t,A,B,T) 16.92*(1.37*10^12*exp(-12628/T))*A*B-((T-328)/(300+t))-(0.253*10^-3)*(T-309);
Cav = zeros(1,N+1);
Cbv = zeros(1,N+1);
dTv = zeros(1,N+1);
for i=1:N
Cav(i+1) = A(i) + h*Ca(t(i), A(i), B(i), T(i));
Cbv(i+1) = B(i) + h*Cb(t(i), A(i), B(i), T(i));
dTv(i+1) = T(i) + h*dT(t(i), A(i), B(i), T(i));
t(i+1)=a+i*h;
end
plot(t,Cav,'-',t,Cbv,'.-',t,dTv,'--')
It runs without error although it still has problems. I leave those for you to solve.
Related Question