MATLAB Answers

how to fix the error ''Conversion to function_handle from double is not possible.'' for the below 3Eq euler matlab code

16 views (last 30 days)
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);

Accepted Answer

Star Strider
Star Strider on 28 Feb 2021
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.
  2 Comments
Star Strider
Star Strider on 28 Feb 2021
Since this appears to be a homework assignment, our policy is not to provide complete solutions, and simply to get your code to run without errors. The code — including the plot — ran without error with the code I wrote, although it is likely not the result you want.
The remaining problems are yours to solve. If you have other problems, describe them in detail.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!