Euler's method to solve the heat equation

6 views (last 30 days)
hello,
I want to plot the exact and proximative curves for the solution of the heat equation here
but my code has a problem:
x1=0;
a = 1;
x2=a;
T = 1; % length of time for solution
n = 599;
N = 150;
dx = a/(n+1.);
dt = T/N;
t=0.; % initial time = 0
beta = 1.e-5;
s = beta*dt/(dx^2);
if s > 0.5
fprintf('gain parameter > 0.5');
end
Adiag = (1-2*s)*ones(n,1);
Asubs = s*ones(n,1);
Asuper = s*ones(n,1);
A = spdiags([Asubs,Adiag,Asuper],[-1 0 1],n,n);
A(n,n) = 1-s;
b= zeros(n,1);
b(1) = 0;
b(n) = 0;
x = linspace(0,a, n+1)';
f=inline('sin(pi*x)');
U_tk = f(x);
a=1 ;
v = inline('exp(-a^2*pi*t)*sin(pi*x)','x','t','a'); % exact solution
for n=1:N
plot(x,U_tk,'b',x,v(x,t,a),'r')
axis([0,1.5,-0.5, 1.5])
title(['lambda=',num2str(s)])
pause,
end;
also, I have to use Euler's explicit method here:

Answers (0)

Categories

Find more on Thermal Analysis in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!