MATLAB: Plotting a temperature graphs of a heat equation of a rod

curvefourier seriesheat equationMATLABone dimensionalpartial differential equationplottemperature profile

The values of c, L and deltat are choosen by myself. The equation is attached in the picture and this my code. I can't seem to get the curve correctly for the temperature of the rod. Is there anything wrong with my code?
syms n;
c = 4;
L = 6;
deltat = 0.2;
x = [0:6];
for k = 1:5
t = (deltat*k);
u = (200/pi)*symsum((((-1)^(n+1))/n)*(sin((n*x*pi)/L))*(exp(-(((c*n*pi)/L)^2)*t)),n,1,1000);
plot(x,u)
hold on
end

Best Answer

The Symbolic Math Toolbox isn’t the best option for this. It’s possible to vectorise the sin*exp term, and is only necessary to sum over ‘n’ in a loop:
c = 4;
L = 6;
deltat = 0.2;
k = 0:4;
t = k*deltat;
x = linspace(0,6,50);
xp = c*pi/L^2;
[T,X] = meshgrid(t,x);
u = zeros(size(T));
for n = 1:1000
u = u + ((-1)^(n+1)/n) * (sin(n*pi*X/L).*exp(-n*xp*T));
end
figure(1)
surf(T, X, u)
grid on
xlabel('\itt\rm')
ylabel('\itx\rm')
zlabel('\itu(x,t)\rm')
view(125, 35)
Experiment to get the result you want.
Related Question