Explicit Eulers Method for time advancement

5 views (last 30 days)
Hello I am trying to use explicit Euler for time advancement and the second-order centraldifference scheme for the spatial derivative, solve the equation to steady
state on a uniform grid. Plot the exact and numerical steady solutions for Nx = 10, 20.
𝜕𝑇/𝜕𝑡 = 𝛼*( 𝜕^2𝑇/𝜕𝑥^2) + 𝑆(𝑥) on the boundary of 0 ≤ 𝑥 ≤ 𝐿𝑥 The initial and boundary conditions are 𝑇(𝑥, 0) = 0 𝑇(0,𝑡) = 0 𝑇(𝐿𝑥,𝑡) = 𝑇steady(𝐿𝑥) Take 𝛼 = 1, 𝐿𝑥 = 15, and 𝑆(𝑥) = −(𝑥 2 − 4𝑥 + 2)𝑒 −𝑥 . The exact steady solution is 𝑇steady(𝑥) = 𝑥 2𝑒 −𝑥
heres the code I have can someone explain where I went wrong
alpha =0;
x = 0;
n =10;
T(0)4ess = 0;
h=0.1;
s(x)=-(x^2-4*x+2)*exp^(-x);
for i=1:n
T(i+1)=T(i) + h;
T(i)^(n+1)=T(i+1)^n+((alpha*h)/(x+h)^2)*(T(i+1)^n-2T(i)^n+T(i-1)^n)+h*s(x);
x = x +1;
h = h +0.1;
end
plot(x,T);
grid on;

Answers (1)

Alan Stevens
Alan Stevens on 22 Feb 2021
T(i)^(n+1)
This will raise T(i) to the (n+1)th power!
You need another loop for time (say j = 1:something), then you can refer to T at position i and time j as
T(i,j)
On the right hand side
((alpha*h)/(x+h)^2)*(T(i+1)^n-2T(i)^n+T(i-1)^n)
should be
((alpha*h)/dx^2)*(T(i+1,j)-2T(i,j)+T(i-1,j))
where dx is the spatial interval (dx = Lx/n).
h is the timestep, so don't update it in the loop!

Categories

Find more on MATLAB 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!