Azzera filtri
Azzera filtri

Question about a function

9 visualizzazioni (ultimi 30 giorni)
Michael Wang
Michael Wang il 15 Mar 2020
Commentato: Walter Roberson il 16 Mar 2020
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 Commenti
Michael Wang
Michael Wang il 15 Mar 2020
These are all the input values.
Michael Wang
Michael Wang il 15 Mar 2020
T1 = -41;
timeend = 60;
nt = 101;
[T,aliveFlag] = tutorial3(T1,timeend,nt);
These are the elements for input values.

Accedi per commentare.

Risposte (1)

Cris LaPierre
Cris LaPierre il 16 Mar 2020
Modificato: Cris LaPierre il 16 Mar 2020
"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 Commento
Walter Roberson
Walter Roberson il 16 Mar 2020
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 ?

Accedi per commentare.

Categorie

Scopri di più su MATLAB Report Generator Task Examples in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by