Matlab code for Euler method help

1 次查看(过去 30 天)
Zuhair Khan
Zuhair Khan 2019-10-23
编辑: James Tursa 2019-10-23
Hi, I wrote the folowing code for Euler's method but Matlab is generating an error for the T(i+1) term. I need hep in understanding why is that happening and how to fix it. Thank you
%solving the equation using Euler's explicit method
% dT/dz = (Q + (4*U_eff*w/Dr)*(T-To))/G*cp;
z = 0.5;
Dr= 0.025;
cp_co2 = 0.85058;
cp_h2 = 14.865;
cp = 0.2*cp_co2 + 0.8*cp_h2;
To= 298;
Sco = -66000;
Ac = (3.1416*Dr^2);
Sc = Sco*(T-To);
Q= Sc*Ac*z ;
E = 0.4;
c = 0.75;
Gfr = 1;
p_co2 = 1.786;
p_h2 = 0.081347;
p = 0.2*p_co2 + 0.8*p_h2;
v_z = Gfr/p;
G = p*E*v_z;
tc_h2 = 0.19201;
tc_co2 = 0.01663;
tc_f = 0.2*tc_co2 + 0.8*tc_h2;
A = 1.75*(1-E)*E^3;
U_co2= 14.923*10^(-6);
U_h2 = 8.9117*10^(-6);
U = 0.2*U_co2 + 0.8*U_h2 ;
R= (p*v_z*z)/U ;
Su = A*R;
htc_w =tc_f/Su;
U_w = htc_w;
% Euler's Method notations
h= 0.001; %step size
z= 0:h:0.5; % range of bed length
N= numel(T); %range of T values
% At z= 0 T= To -- Initial Condition
%%Initializing Solutiion
T = zeros(size(z));
%% Solving equation using Euler's method
for i = 1:N-1
T(1)= To; %the initial tempereature in Euler
f = (Sc*Ac*z(i) + (4*U_w/Dr)*(T(i)-To))/G*cp;
% z(i+1) = z(i) + h*fi;
T(i+1) = T(i) + h*f;
end
plot(z,T);
  1 个评论
Steven Lord
Steven Lord 2019-10-23
What is the full and exact text of the error message you received? That may give us useful information that will help us help you correct the problem. Show us all the text displayed in red (error message) and/or orange (warning message.)

请先登录,再进行评论。

回答(2 个)

James Tursa
James Tursa 2019-10-23
编辑:James Tursa 2019-10-23
z(i+1) = z(i) + f*h;
T(i+1) = T(i) + h;
plot(T,z);
  2 个评论
Zuhair Khan
Zuhair Khan 2019-10-23
It is still generating an error in line z(i+1) = z(i) + f*h;. This is how I am entering it:
%% Solving equation using Euler's method
for i = 1:N-1
T(1)= To; %the initial tempereature in Euler
f = (Sc*Ac*z(i) + (4*U_w/Dr)*(T(i)-To))/G*cp;
z(i+1) = z(i) + f*h;
T(i+1) = T(i) + h;
end
%%Plot solution
plot(T,z);
James Tursa
James Tursa 2019-10-23
编辑:James Tursa 2019-10-23
Sorry ... I did not read your code properly. I now understand that z is your independent variable and T is your dependent variable. So these would be proper:
% z(i+1) = z(i) + h; % this line not really needed
T(i+1) = T(i) + h*f;
But I am still confused. In your code you use z and T in calculations prior to defining the z vector and prior to the loop. What is the value that z(1) is supposed to start at? 0.5 or 0.0 or ...?

请先登录,再进行评论。


Jim Riggs
Jim Riggs 2019-10-23
编辑:Jim Riggs 2019-10-23
One thing that I notice is that you are defining N based on numel(T), but it looks like T is not yet defined, so what is the value of N? It might be zero or 1. So your for loop is looping from i=1 to N-1 (i.e. i=1 to -1 or zero?).
I'm guessing that maybe N sould be numel(z)?

类别

Help CenterFile Exchange 中查找有关 Error Detection and Correction 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by