MATLAB: Hi, I have this code and there some errors in line 12 and 14 can any one help me. Thanks

alaa

%Inputs L = 0.01; % Plate thickness (m) k = 0.2; % Conductivity (W/m-K) rho = 2000; % Density (kg/m^3) cp = 200; % Specifi c heat capacity (J/kg-K) T_in = 343.15; % Initial temperature (K) T_f = 298; % Gas temperature (K) h = 17; % Heat transfer coeffi cient (W/m^2-K) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Setup Grid N = 10; % Number of nodes (-) for i = 1:N x(i)=(i-1)*L/(N-1); % Position of each node (m) end DELTAx=L/(N-1); % Distance between adjacent nodes (m) tau_sim = 10; % Simulation time (s) OPTIONS = odeset('RelTol',1e-6); [time,T] = ode45(@(time,T) dTdt_functionv(time,T,L,k,rho,cp,T_f,h),[0,tau_sim],T_in * ones(N,1),OPTIONS); % Plot fi gure of transient heat conduction through fuel cell plates surf(T); xlabel('Number of Nodes'); zlabel('Temperature (K)'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% FUNCTION(dTdt)= dTdt_functionv(time,T,L,k,rho,c,T_f,h); [N,g]=size(T); % Determine the size of T DELTAx=L/(N-1); % Calculate the distance between adjacent nodes dTdt=zeros(N,1); % Create dTdt vector dTdt(1)=2 * k * (T(2)-T(1))/(rho * c * DELTAx^2); for i=2:(N-1) dTdt(i)=k * (T(i-1)+T(i+1)-2 * T(i))/(rho * c * DELTAx^2); end dTdt(N)=2 * k * (T(N-1)-T(N))/(rho * c * DELTAx^2)+2 * h * (T_f-T(N))/(rho * c * DELTAx); END

Best Answer

You cannot write a FUNCTION inside a script. Functions can be nested only into other funcitons, not in scripts. So write this function in a separate file
function dTdt = dTdt_functionv(time,T,L,k,rho,c,T_f,h)
[N,g]=size(T); % Determine the size of T
DELTAx=L/(N-1); % Calculate the distance between adjacent nodes
dTdt=zeros(N,1); % Create dTdt vector
dTdt(1)=2 * k * (T(2)-T(1))/(rho * c * DELTAx^2);
for i=2:(N-1)
dTdt(i)=k * (T(i-1)+T(i+1)-2 * T(i))/(rho * c * DELTAx^2);
end
dTdt(N)=2 * k * (T(N-1)-T(N))/(rho * c * DELTAx^2)+2 * h * (T_f-T(N))/(rho * c * DELTAx);
end
and then invoke it in your script
%Inputs
L = 0.01; % Plate thickness (m)
k = 0.2; % Conductivity (W/m-K)
rho = 2000; % Density (kg/m^3)
cp = 200; % Specific heat capacity (J/kg-K)
T_in = 343.15; % Initial temperature (K)
T_f = 298; % Gas temperature (K)
h = 17; % Heat transfer coeffi cient (W/m^2-K)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Setup Grid
N = 10; % Number of nodes (-)
for i = 1:N
x(i)=(i-1)*L/(N-1); % Position of each node (m)
end
DELTAx=L/(N-1); % Distance between adjacent nodes (m)
tau_sim = 10; % Simulation time (s)
OPTIONS = odeset('RelTol',1e-6);
[time,T] = ode45(@(time,T) dTdt_functionv(time,T,L,k,rho,cp,T_f,h),[0,tau_sim],T_in * ones(N,1),OPTIONS);
% Plot figure of transient heat conduction through fuel cell plates
surf(T); xlabel('Number of Nodes'); zlabel('Temperature (K)');
Related Question