# Analytical solution for 1D heat equation

37 views (last 30 days)
Dominik Horcicka on 6 May 2022
Edited: David Goodmanson on 12 May 2022
Hello,
I need help with analytical solution for following heat equation. I already have numerical solution using euler(explicit and implicit).
Adding code for explicit solution...
clear all;
close all;
clc
%def oblasti
a=0; %x nalezi {a,b}
b=2*pi;
d=1; %okrajova podminka t nalezi {0,d}
h=2*pi/100; %volba kroku x
p=100; %koeficient pred druhou parcialni derivaci U dle x
tau=(h^2*1/2)/p % výpočet časového kroku
%% priprava na vypocet
%vypocet mnozstvi kroku
stx=round(1+(2*pi-a)/h);
stt=1+d/tau;
x(1)=a;
t(1)=0;
%vypocet sigma
sig=p*tau/h^2
%% sestavovani site
%parametrizace delky x
for j=2:stx
x(j)=a+(j-1)*h;
end
%parametrizace casu t
for k=2:stt
t(k)=(k-1)*tau;
end
%preddefinovani matice vyslednych U
%U=zeros(stx,stt);
%funkce f
%for j=1:stx
% for k=1:stt
% f(j,k)=exp(-t(k));
% end
%end
% pocatecni podminky
for j=1:stx
U(j,1)=sin(x(j))+(1/10)*sin(10*x(j));
end
% okrajove podminky
for k=1:stt
U(1,k)=0;
U(stx,k)=0;
end
%% vypocet
%vypocet jednotlivych hodnot U uvnitr mnoziny
%tj bez OP a PP ktere byly jizvypocteny
for k=2:stt
for j=2:(stx-1)
U(j,k)=sig*(U(j+1,k-1)+U(j-1,k-1))+(1-2*sig)*U(j,k-1);%+tau*f(j,k-1);
end
end
figure(1)
sur=surf(t, x, U);
set(sur,'LineStyle','none')
figure(2)
plot(x, U(:,1))
%U1_p1=U;, t1_p1=t;
%U1_p10=U; t1_p10=t;
U1_p100=U; t1_p100=t;
%save U1_p1 , save t1_p1
%save U1_p10, save t1_p10
save U1_p100, save t1_p100
I need the same solution as my Figure 1 for different inputs of the constant 'a' in my code it is p, because I need to compare it with explicit and implicit solution.
FIgure (1) a=100 FIgure (1) a=1 I don't know how to do analytical solution in MATLAB for it.
Could someone help me please?
Thank you very much for your time and help! It will really help me.

David Goodmanson on 6 May 2022
Edited: David Goodmanson on 12 May 2022
Hi Dominick,
you have the sum of two sine terms for the boundary condition, sin(x) and (1/10) sin(10x). So consider a general sine term, A*sin(n*x). Taking the second derivative of this, you get -n^2*A*sin(n*x), same function as before, times a constant. This suggests separation of variables and a product-type solution for all of x and t. If you try
U(x,t) = A*sin(n*x)*exp(-b*t)
then the time derivative of the exponential is proportional to itself, which looks favorable. After taking the derivatives in the heat equation
a d^2U/dx^2 = dU/dt
you can solve for b in terms of a and n. For a particular choice of A and n you can satisfy the first boundary condition term, and for another choice you can satisfy the second boundary condition term. Since the heat equation is a linear partial differential equation, and since the two boundary terms are additive, the sum of the two solution satisfies the total boundary condition and you have the result.

R2021b

### Community Treasure Hunt

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

Start Hunting!