Backward euler method for heat equation with neumann b.c.

In summary: Well, not exactly. # Let's compute u_x(0,t) and u_x(1,t) using forward and backward Euler respectively # u_x(0,t) = (u(1,t)-u(-1,t))/2h, and u_x(1,t) = (u(2,t)-u(0,t))/2h # But u(-1,t) and u(2,t) are not known. So we will use the first order approximation: # u_x(0,t) = (u(1,t)-u(0,t))/h, and u_x(1,t) = (u(1,t)-u(0,t))/h # That's
  • #1
omer21
25
0
I am trying to solve the following pde numerically using backward f.d. for time and central di fference approximation for x, in MATLAB but i can't get correct results.

[itex]
\frac{\partial u}{\partial t}=\alpha\frac{\partial^{2}u}{\partial x^{2}},\qquad u(x,0)=f(x),\qquad u_{x}(0,t)=0,\qquad u_{x}(1,t)=2
[/itex]
for boundary conditions i used the following approximation
[itex]u_{x}(0,t)=\frac{u_{i+1}^{j}-u_{i-1}^{j}}{2h}[/itex]

what is wrong with the code i wrote
Code:
function [T,exact]=implicitheat2(t_i,t_f,a,b,dx,dt,alpha)

%U_t=U_xx , 0<x<1, U(x,0)=x.^2+1+cos(pi.*x), U_x(0,t)=0, U_x(1,t)=2

% dt: step size in t
% dx: step size in x
% a: left point of domain 
% b: right point of domain 
% alpha: equal to 1
% call func. as implicitheat2(0,0.1,0,1,0.1,0.00001,1) 

 

n=(t_f-t_i)/dt;
m=(b-a)/dx;
lambda=alpha*dt/dx^2;

if isinteger(m)==0
    m=round(m);
end
if isinteger(n)==0
    n=round(n);
end
T=zeros(m+1,n+1);

x=a:dx:b;
t=t_i:dt:t_f;
   
u0 = x.^2+1+cos(pi.*x);
T(:,1)=u0; %initial value

A = sparse(m-1,m-1);
    for i=1:m-1
        A(i,i-1) = -lambda;
        A(i,i ) = (1+2*lambda);
        A(i,i+1) = -lambda;
    end
A(1,2)=-2*lambda;
A(end,end-1)=-2*lambda;
b=zeros(m-1,1);
for j=2:n+1
    b=T(2:m,j-1);
    b(1,1)=T(2,j-1)-2*0*lambda*dx;  %T(0,j-1)-2*lambda*U'*dx
    b(m-1,1)=T(m,j-1)+2*lambda*2*dx;  %T(m+1,j-1)+2*lambda*U'*dx
    T(2:m,j)=A\b;
end
T=T(:,1); 
%exact soln
[xx,tt]=meshgrid(x,t);

exact=2.*tt+xx.^2+1+exp(-pi^2.*tt).*cos(pi.*xx);

exact=exact(end,:);
 
Physics news on Phys.org
  • #2
Hey Omer21,

I will have a look at this a bit later. Also, could you check your initial vector [itex]u_0[/itex]? I plotted it and I have

[itex]
u\left ( 0,0 \right ) =2\\
u\left ( 1,0\right ) = 1
[/itex]
J.
 
  • #3
Initial vector is right. You miss subscript at b. c. i guess. B.C's are specified at the derivative of u.
 
  • #4
Whatever. If you want a discontinuous function as boundary condition, that's your right.

Without having looked further, I see you are missing a

Code:
T(m+1,j)=2;

in your main loop. Also, the following lines are suspicious:

Code:
b(1,1)=T(2,j-1)-2*0*lambda*dx;

T=T(:,1);

First: you multiply by 0. You sure? And for the second, you are doing a bunch of calculations and then taking only the first column.
 
  • #5
You are right about
Code:
T=T(:,1);
. It ought to be
Code:
T=T(:,end);
.
I want to show what i did so i multiplied by 0 because of [itex]u'_x(0,t)=0[/itex].
I put
Code:
T(m+1,j)=2;
just after second for loop but still i can't get correct results.
 
  • #6
I redid your code. I think there is more to it than "just" backward Euler not working.

As I mentioned -

- Your initial vector first and last elements are not 0 and 2;
- You are assigning weird things in weird ways;
- Your "exact" solutions doesn't have value 0 for x=0 and value 2 for x=1, which are your space boundaries.

Here is my code in Octave. Feel free to inspire yourself. I will add the averaging with forward Euler to increase the precision.

Code:
function Y=heattrans(t0,tf,n,m,alpha,withfe)

# Calculate the heat distribution along the domain 0->1 at time tf, knowing the initial 
# conditions at time t0
# n - number of points in the time domain (at least 3)
# m - number of points in the space domain (at least 3)
# alpha - heat coefficient
# withfe - average backward Euler and forward Euler to reach second order

# The equation is 
#
#   du             d2u
#  ---- = alpha . -----
#   dt             dx2

if (or(n<3,m<3))
 disp("Really mate?!")
 Y=[]
 return
endif

dx=1/(m-1);
dt=(tf-t0)/(n-1);

# Initial conditions are
# U(x,0) = x.^2 + 1 + cos(pi*x)
# Warning - U(0,0) = 2 =/= U(0,t=/=0) = 0
# and     - U(1,0) = 3 =/= U(1,t=/=0) = 2

x=linspace(0,1,m);
t=linspace(t0,tf,n);
beta=(alpha*dt)/(dx^2);

# Let's initialize our values
Ut=x.^2+1+cos(pi*x);

# Main loop - We apply backward Euler and solve successive equations
# Second order differences are computed using central difference, backward Euler is computed
# using the classic "one side" difference

for k = 1:n
  # Let's build the matrix of diff factors
  M=spalloc(m,m,3*m);
  M(1,1)=1;
  M(m,m)=1;
  for l = 2:(m-1)
    M(l,l-1)=beta;
    M(l,l)=-(2*beta+1);
    M(l,l+1)=beta;
  endfor
  # Now, let's build the vector of diff terms
  # That's actually -Ut, except for the first and last elements
  D=-Ut';
  D(1,1)=0;
  D(m,1)=2;
  # And the next step is the solution (transposed, as I use line vectors for U)
  Ut=(inv(M)*D)';
endfor
# The answer is in the last Ut
Y=Ut;
endfunction
 
  • #7
Actually my codes are working accurately when B.C.s are dirichlet, but when B.C.s are turned to Neumann i confused how to edit the codes anyway i will look your codes.
Thank you jfgobin for your help.
 
  • #8
Okay, I think I know where the problem lies. Part here, part there.

I will write some code later.
 
  • #9
Here is the code that solves it. Don't forget that both backward Euler and forward Euler are methods of the first order, and that imprecision can creep up.

My code doesn't use central difference for the first order derivative: the only cases I need them is for the corners. A better approximation could be made by taking more points.

Code:
function Y=heattrans(t0,tf,n,m,alpha,withfe)

# Calculate the heat distribution along the domain 0->1 at time tf, knowing the initial 
# conditions at time t0
# n - number of points in the time domain (at least 3)
# m - number of points in the space domain (at least 3)
# alpha - heat coefficient
# withfe - average backward Euler and forward Euler to reach second order

# The equation is 
#
#   du             d2u
#  ---- = alpha . -----
#   dt             dx2

if (or(n<3,m<3))
 disp("Really mate?!")
 Y=[]
 return
endif

dx=1/(m-1);
dt=(tf-t0)/(n-1);

# Initial conditions are
# U(x,0) = x.^2 + 1 + cos(pi*x)
# Warning - U(0,0) = 2 =/= U(0,t=/=0) = 0
# and     - U(1,0) = 3 =/= U(1,t=/=0) = 2

x=linspace(0,1,m);
t=linspace(t0,tf,n);
beta=(alpha*dt)/(dx^2);

# Let's initialize our values
Ut=x.^2+1+cos(pi*x);

# Main loop - We apply backward Euler and solve successive equations
# Second order differences are computed using central difference, backward Euler is computed
# using the classic "one side" difference

for k = 1:n
  # Let's build the matrix of diff factors
  M=spalloc(m,m,3*m);
  M(1,1)=-1;
  M(1,2)=1;
  M(m,m)=1;
  M(m,m-1)=-1;
  for l = 2:(m-1)
    M(l,l-1)=beta;
    M(l,l)=-(2*beta+1);
    M(l,l+1)=beta;
  endfor
  # Now, let's build the vector of diff terms
  # That's actually -Ut, except for the first and last elements
  D=-Ut';
  D(1,1)=0;
  D(m,1)=2*dx;
  # And the next step is the solution (transposed, as I use line vectors for U)
  Ut=(inv(M)*D)';
endfor
# The answer is in the last Ut
Y=Ut;
endfunction

Attached are two plots, one at t=0.05 and one at t=1. Is that what you are looking for?
 

Attachments

  • heattrans1.png
    heattrans1.png
    12.3 KB · Views: 795
  • heattrans005.png
    heattrans005.png
    14.5 KB · Views: 822
  • #10
Your plots are correct. Thank you for your effort.
 

1. What is the Backward Euler Method for solving the heat equation with Neumann boundary conditions?

The Backward Euler Method is a numerical method used to approximate the solution of a partial differential equation, such as the heat equation, with Neumann boundary conditions. It involves discretizing the domain into a grid and using a backward difference approximation for the time derivative.

2. How is the Backward Euler Method different from the Forward Euler Method?

The Backward Euler Method is an implicit method, meaning that the solution at the next time step depends on the solution at the current time step. This makes it more stable and accurate than the Forward Euler Method, which is an explicit method.

3. What is the stability condition for the Backward Euler Method?

The stability condition for the Backward Euler Method is that the time step size must be smaller than or equal to the spatial step size squared divided by twice the thermal diffusivity. In other words, the time step size must be small enough to ensure that the solution does not blow up.

4. How does the Backward Euler Method handle Neumann boundary conditions?

The Backward Euler Method uses a backward difference approximation for the derivative at the boundary points, which takes into account the Neumann boundary conditions. This results in a system of equations that can be solved to obtain the solution at the next time step.

5. What are the advantages and disadvantages of using the Backward Euler Method for the heat equation with Neumann boundary conditions?

The advantages of the Backward Euler Method include its stability and accuracy, particularly for problems with rapidly changing solutions. However, it can be computationally expensive compared to other methods, and it may not be suitable for problems with large spatial variations or complex boundary conditions.

Similar threads

  • Differential Equations
Replies
7
Views
380
  • Differential Equations
Replies
1
Views
757
Replies
1
Views
1K
  • Differential Equations
Replies
3
Views
1K
Replies
7
Views
2K
  • Differential Equations
Replies
10
Views
2K
  • Differential Equations
Replies
2
Views
2K
  • Differential Equations
Replies
1
Views
740
  • Differential Equations
Replies
3
Views
2K
  • Differential Equations
Replies
8
Views
3K
Back
Top