CODE: function [errmax] = heat1derr1a(L,T,n,maxk,K,f,c) %%%%%%%%%%%%%%%%%%%%%%%%
ID: 3602901 • Letter: C
Question
CODE:
function [errmax] = heat1derr1a(L,T,n,maxk,K,f,c)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Discretization Errors for Heat Diffusion in a Thin Insulated Wire
% du/dt = f + K*d^2u/dx^2 - cu,
% subject to u(0,t) = u(L,t) = f/c, u(x,0) = f/c + sin(x/sqrt(K)).
% Its exact solution is: u(x,t) = f/c + exp(-(1+c)t)*sin(x/sqrt(K)).
%
% Inputs:
% L = Lenth of the Wire, e.g., L =1
% T = Final Time, e.g., T = 1
% n = Number of Space Steps, e.g., n =5
% maxk = Number of Time Steps, e.g., maxk = 50
% K = Thermal conductivity, e.g., K = 1/pi^2
% f = heat source, e.g., f = 1;
% c = coefficient, e.g., c = 0.1
%
% Output:
% index = k x 2 matrix of (maxk, n);
% errmax = k x 1 matrix of max(abs(u(:,k+1)-uexac(:,k+1)));
% The approxomated solution is compared to the exact solution
% over x = 0 to x = L at t = T
%
% Usage: [index,errmax] = heat1derr1a(1,1,5,50,1/(pi*pi),1,0.1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:4
[err,u,uexac,x,time] = heat1derr1(L,T,n,maxk,K,f,c);
subplot(4,2,2*k-1)
mesh(x,time,uexac')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('temperature') % label for the 3rd axis
title('Exact Temperature')
subplot(4,2,2*k)
mesh(x,time,u')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('temperature') % label for the 3rd axis
title('Approx Temperature')
index(k,1) = maxk/T;
index(k,2) = n/L;
errmax(k,1) = T/maxk;
errmax(k,2) = L/n;
errmax(k,3) = err;
n = 2*n;
maxk = 4*maxk;
end
errmax(1,4) = 1;
for k=2:4
errmax(k,4) = errmax(k,3)/errmax(k-1,3);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab subroutine heat1derr1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [err,u,uexac,x,time] = heat1derr1(L,T,n,maxk,K,f,c)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dt = T/maxk;
dx = L/n;
b = dt/(dx*dx);
cond = K;
d = cond*b;
% Initial Temperature
for i = 1:n+1
x(i) =(i-1)*dx;
if (abs(c)>0.)
u(i,1) = f/c + sin(x(i)/sqrt(cond));
else
u(i,1) = sin(x(i)/sqrt(cond));
end
uexac(i,1) = u(i,1);
end
% Boundary Temperature
for k=1:maxk+1
time(k) = (k-1)*dt;
if (abs(c)>0.)
u(1,k) = f/c;
u(n+1,k) = f/c + exp(-(1+c)*time(k))*sin(L/sqrt{cond});
else
u(1,k) = f*time(k);
u(n+1,k) = f*time(k) + exp(-time(k))*sin(L/sqrt{cond});
end
uexac(1,k) = u(1,k);
uexac(n+1,k) = u(n+1,k);
end
% Time Loop
for k=1:maxk
% Space Loop
for i=2:n;
u(i,k+1) =f*dt + (1-2*d-c*dt)*u(i,k) + d*(u(i-1,k)+u(i+1,k));
if (abs(c)>0.)
uexac(i,k+1) = f/c + exp(-(1+c)*time(k))*sin(x(i)/sqrt(cond));
else
uexac(i,k+1) = f*time(k) + exp(-time(k))*sin(x(i)/sqrt(cond));
end
error(i,k+1)= abs(u(i,k+1)-uexac(i,k+1));
end
end
err = max(error(2:n,maxk));
end
7. Given L = T = 1, K = thcond = ,p = c = 1,f = 0, (n, mark) = (5,50), (10, 100), (20, 200), and (40, 400), use the MATLAB code heatlderrla.m (obtained from modifying heatld.m by inserting an additional line inside the time-space loops for the error)) to verify the computations in Table 1.6.3.Explanation / Answer
the code is correct
function [errmax] = heat1derr1a(L,T,n,maxk,K,f,c)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Discretization Errors for Heat Diffusion in a Thin Insulated Wire
% du/dt = f + K*d^2u/dx^2 - cu,
% subject to u(0,t) = u(L,t) = f/c, u(x,0) = f/c + sin(x/sqrt(K)).
% Its exact solution is: u(x,t) = f/c + exp(-(1+c)t)*sin(x/sqrt(K)).
%
% Inputs:
% L = Lenth of the Wire, e.g., L =1
% T = Final Time, e.g., T = 1
% n = Number of Space Steps, e.g., n =5
% maxk = Number of Time Steps, e.g., maxk = 50
% K = Thermal conductivity, e.g., K = 1/pi^2
% f = heat source, e.g., f = 1;
% c = coefficient, e.g., c = 0.1
%
% Output:
% index = k x 2 matrix of (maxk, n);
% errmax = k x 1 matrix of max(abs(u(:,k+1)-uexac(:,k+1)));
% The approxomated solution is compared to the exact solution
% over x = 0 to x = L at t = T
%
% Usage: [index,errmax] = heat1derr1a(1,1,5,50,1/(pi*pi),1,0.1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k = 1:4
[err,u,uexac,x,time] = heat1derr1(L,T,n,maxk,K,f,c);
subplot(4,2,2*k-1)
mesh(x,time,uexac')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('temperature') % label for the 3rd axis
title('Exact Temperature')
subplot(4,2,2*k)
mesh(x,time,u')
xlabel('x') % label for the 1st axis
ylabel('time') % label for the 2nd axis
zlabel('temperature') % label for the 3rd axis
title('Approx Temperature')
index(k,1) = maxk/T;
index(k,2) = n/L;
errmax(k,1) = T/maxk;
errmax(k,2) = L/n;
errmax(k,3) = err;
n = 2*n;
maxk = 4*maxk;
end
errmax(1,4) = 1;
for k=2:4
errmax(k,4) = errmax(k,3)/errmax(k-1,3);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Matlab subroutine heat1derr1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [err,u,uexac,x,time] = heat1derr1(L,T,n,maxk,K,f,c)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dt = T/maxk;
dx = L/n;
b = dt/(dx*dx);
cond = K;
d = cond*b;
% Initial Temperature
for i = 1:n+1
x(i) =(i-1)*dx;
if (abs(c)>0.)
u(i,1) = f/c + sin(x(i)/sqrt(cond));
else
u(i,1) = sin(x(i)/sqrt(cond));
end
uexac(i,1) = u(i,1);
end
% Boundary Temperature
for k=1:maxk+1
time(k) = (k-1)*dt;
if (abs(c)>0.)
u(1,k) = f/c;
u(n+1,k) = f/c + exp(-(1+c)*time(k))*sin(L/sqrt{cond});
else
u(1,k) = f*time(k);
u(n+1,k) = f*time(k) + exp(-time(k))*sin(L/sqrt{cond});
end
uexac(1,k) = u(1,k);
uexac(n+1,k) = u(n+1,k);
end
% Time Loop
for k=1:maxk
% Space Loop
for i=2:n;
u(i,k+1) =f*dt + (1-2*d-c*dt)*u(i,k) + d*(u(i-1,k)+u(i+1,k));
if (abs(c)>0.)
uexac(i,k+1) = f/c + exp(-(1+c)*time(k))*sin(x(i)/sqrt(cond));
else
uexac(i,k+1) = f*time(k) + exp(-time(k))*sin(x(i)/sqrt(cond));
end
error(i,k+1)= abs(u(i,k+1)-uexac(i,k+1));
end
end
err = max(error(2:n,maxk));
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.