Finite difference explicit method

127 Views Asked by At

I try to solve with matlab program the differential equation with finite difference explicit method. The problem sounds in this way: Using finite difference explicit and implicit finite difference method solve problem $\frac{\partial{u}}{\partial{t}}=\frac{\partial^2{u}}{\partial{t}}+x-t$ with initial condition: $u(0,x)=sin(x)$ and boundary conditions: $u(t,0)=e^t$, $u(t,1)=e^tsin{1}$. Do this task with mathematical package. So, I tried but get struggles and really need advises. Even I'm not sure how to describe this differential equation or choose number of time steps/space steps in Matlab. Here is what I have tried:

L = 1.; % Length of the wire
T =1; % Final time
% Parameters needed to solve the equation within the explicit method
maxk = 50; % Number of time steps
dt = T/maxk;
n = 10; % Number of space steps
dx = L/n;
a = 1; 
b = 2.*a*dt/(dx*dx); 
% Initial temperature of the wire: a sinus.
for i = 1:n+1
x(i) =(i-1)*dx;
u(i,1) =sin(x(i));
end
% Temperature at the boundary
for t=1:maxk+1
u(1,t) = exp(t);
u(n+1,t) = sin(1)*exp(t);
time(t) = (t-1)*dt;
end
% Implementation of the explicit method
for t=1:maxk % Time Loop
for i=2:n; % Space Loop
u(i,t+1) =u(i,t) + b*(u(i-1,t)+u(i+1,t)-2.*u(i,t));
end
end

% Graphical representation of the temperature at different selected times
figure(1)
plot(x,u(:,1),'-',x,u(:,10),'-',x,u(:,45),'-',x,u(:,30),'-',x,u(:,60),'-')
title('Temperature within the explicit method')
xlabel('X')
ylabel('T')
figure(2)
mesh(x,time,u')
title('Temperature within the explicit method')
xlabel('X')
ylabel('Temperature')