필터 지우기
필터 지우기

Question about a function

조회 수: 9 (최근 30일)
Michael Wang
Michael Wang 2020년 3월 15일
댓글: Walter Roberson 2020년 3월 16일
I am not sure what is wrong with my function coding, I tried code in the matlab, it works, however the matlab grader says run without outputs, and bring me back the inccorect solution as Variable T has an incorrect value.
Here is the coding I've typed below
function [T,aliveFlag] = tutorial3(T1,timeend,nt)
% Description: This file implements the Fourier Series solution to the heat equation.
%
% Input parameters:
% - T1 - temperature at r=0
% - timeend - time of simulation in seconds
% - nt - number of points in time vector
%
% Output parameters:
% - T - temperature profile along persons head
% - aliveFlag - 1 if person is alive, 0 if the person is dead
% Spatial (r) parameters
L = 0.12; % Size of the domain
rstart = 0; % Start of computational domain (m)
rend = L; % End of computational domain (m)
nr = 101; % Number of grid points along the radius vector
dr = (rend-rstart)/(nr-1); % Spatial step size, delta r (m)
r = rstart:dr:rend; % Vector of grid points in r
% Temporal (t) parameters
timestart = 0; % Starting time (0 seconds)
dt = (timeend-timestart)/(nt-1); % Temporal step size, delta t (s)
time = timestart:dt:timeend; % Vector of times craete this vector
% Phyiscal parameters
% Temperature at r=0
T2 = 37; % Temperature at r=L
A = T2-T1; % Amplitude of the saw tooth wave
k = 0.527; % Thermal conductivity (W/m-K)
rho = 1000; % Density (kg/m^3)
gamma = 3600; % Heat capacity (J/kg-K) sigma in this notes
c = sqrt(k/(rho*gamma)); % Heat equation constant
% Calculate B coefficients using a for loop
nfs = 1000; % Number of fourier terms
B = zeros(1,nfs); % Initialise B vector
lambda=zeros(1,nfs); % Initialise lambda vector
for n = 1:nfs
B(n)=2*A/(n*pi); % Calculate B coefficients
lambda(n)=(n*pi*c)/L;
end
% above loop is givinga vector of Fourie coefficient
%% Solve for T using three for loops in time, space, and Fourier series
% Loop through time
for i = 1:nt % For each time
t = time(i); % time in seconds
% Vector of zeros to initialise the Fourier series solution.
% This should be re-initialised at each new time step.
T = zeros(1,nr);
% Loop through space
for j = 1:nr % For each grid point
T(j) = 78*r(j)/L - 41; % Add the steady state solution
% Loop through the Fourier series
for n = 1:nfs
T(j) = T(j) + B(n)*sin(n*pi*r(j)/L)*exp(-lambda(n)^2*t); % Calculate series sum for T at r(i) and t
end
end
end
aliveFlag=mean(T)>22;
end
  댓글 수: 3
Michael Wang
Michael Wang 2020년 3월 15일
These are all the input values.
Michael Wang
Michael Wang 2020년 3월 15일
T1 = -41;
timeend = 60;
nt = 101;
[T,aliveFlag] = tutorial3(T1,timeend,nt);
These are the elements for input values.

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 3월 16일
편집: Cris LaPierre 2020년 3월 16일
"Code ran without output" is just a message to let you know your code has finished running and did not produce any outputs. Without the messgae, it might be difficult to recognize when the code has finished executing. If you want to see outputs, leave a semicolon off one of your lines. However, this is unnecessary and potentially undesirable.
If you are not passing the assessments, that means there is an error in your solution to the problem. Here, it appears you are not correctly computing the values for T.
  댓글 수: 1
Walter Roberson
Walter Roberson 2020년 3월 16일
It looks strange to me to start the fourier output with the value of the steady-state solution ? Is that intended to correspond to n = 0 ?

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

카테고리

Help CenterFile Exchange에서 MATLAB Report Generator Task Examples에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by