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

조회 수: 1 (최근 30일)
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);

채택된 답변

Star Strider
Star Strider 2021년 2월 28일
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
Star Strider
Star Strider 2021년 2월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by