Question on heat equation 1D Forward in Time Centered in Space
Show older comments
Hi all I have the following question I am trying to solve the PDE with forward time centered in space with the following parameters:

My code so far
function E=Expheat(h,k)
tmax = 1;
t = linspace(0,tmax);
N = 1/h;
M = 1/k;
x = linspace(-pi/2, pi/2);
g(x) = sin*(pi/2*x) + 1/2*sin*(2*pi*x);
alpha = 1;
mu = alpha^2*k/h^2;
%loop for IC
for j = 1 : M
U( 1, j) = 0
end
%loop for
for n = 1: tmax
U(end, j)= exp(pi^2*t/4)
end
for i = 1 : M+1
U(g(x), 0) = (1 - 2*U(n,j))-U(n+1)+U(n-1,j+1)
for j = 2 : N
U(g(x), 0) = (1 - 2*U(n,j))-U(n+1)+U(n-1,j+1)
end
end
I am getting errors and am tying to figure out how to solve this any help would be greatly appreciated.
Thank you
4 Comments
Jan
on 5 Jun 2018
Please post the error messages.
Janvier Solaris
on 5 Jun 2018
Abraham Boayue
on 6 Jun 2018
Here is a code that you may find useful to help solve your problem. I can't really say much about the solution since you did not post the original problem; my code is based on the numerical equations you posted. I would have like to see how the boundary conditions were given instead of the two boundary functions you provided. For example, you may have u(x=0,t) = O1(t), u(x=1,t)= O2(t) or u(0,t) = O1(t), ux(1,t) = O2(t) or something else. If this isn't right, post the original problem as it was given.
clear all
close all
n = 500;
Lx = 1;
dx = Lx/(n-1);
x = 0:dx:Lx;
% 2. Parameters for the t vector
m = 100;
tf = 5;
dt = tf/(m-1);
t = 0:dt:tf;
% 3. Other parameters needed for the solution
% The value of alpha
Fo = 1/4; % a mutiplicative constant that should be < = 1/2
% to insure stability
% Initial and boundary conditions
f = @(x)sin(pi/2*x)+(1/2)*sin(2*pi*x);
g1 = @(t)0;
g2 = @(t)exp(-pi^2/4*t);
u = zeros(n,m);
u(2:n,1) = f(x(2:n)); % Put in the initial condition starting from
% 2 to n-1 since f(0) = 0 and f(N) = 1 for
% N = n-1
u(1,1:m) = g1(t); % The boundary conditions, g1 and g2 at
u(n,1:m) = g2(t); % x = 0 and x = 1
% Implementation of the explicit method
for k = 2:m-1 % Time Loop
for i= 2:n-1 % Space Loop
u(i,k+1) = Fo*(u(i-1,k)+u(i+1,k))+(1-2*Fo)*u(i,k);
end
end
plot(x,u,'--','linewidth',2);
a = ylabel('Pressure');
set(a,'Fontsize',14);
a = xlabel('x');
set(a,'Fontsize',14);
a=title(['Using The Explicit Method - Fo =' num2str(Fo)]);
set(a,'Fontsize',16);
grid;
figure
[X, T] = meshgrid(x,t);
s2 = mesh(X',T',u);
title(['3-D plot of the 1D Heat Equation using the Explicit Method - Fo =' num2str(Fo)])
set(s2,'FaceColor',[0 0 1],'edgecolor',[0 0 0],'LineStyle','--');
a = title('Exact solution of the 1D Diffusivity Equation');
set(a,'fontsize',14);
a = xlabel('x');
set(a,'fontsize',20);
a = ylabel('y');
set(a,'fontsize',20);
a = zlabel('z');
set(a,'fontsize',20);
% disp(u');
Janvier Solaris
on 6 Jun 2018
Accepted Answer
More Answers (1)
Rauhussaba Rauhy
on 24 Mar 2022
0 votes
How to Build an algorithm for piecewise linear Rayleigh ritz method? _d/dx(p(x)d/dx)+q(x)y=f(x), for x E [0,1] with y(0)=y(1)=0 with piece wise linear function
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

