I write a matlab code for the problem, and i want to compare the numerucal and the exact solution at t=0.5 but the graph of the soultions are very different.
The problem is in the following:
U(i,n+1) = U(i,n) + r * (U(i+1,n) - 2*U(i,n) + U(i-1,n));
t = linspace(0, T, Nt+1);
Exact= @(x, t) exp(-t) .* cos(pi * x - 0.5 * pi);
title('Heat Equation Solution');