Principles of PDEs
-
Upload
vinicius-oliveira-fontes -
Category
Documents
-
view
224 -
download
0
description
Transcript of Principles of PDEs
Homework Assignment #8
Student: Vinicius Fontes
ASU # ID: 1208318367
1.
(a) The heat equation is
๐๐
๐๐กโ ๐
๐2๐
๐๐ฅ2= 0
First, letโs assume a solution in the format
๐(๐ฅ, ๐ก) = ฯ(x) + ๐(๐ฅ, ๐ก)
And also
ฯ(0) = ๐(0, ๐ก) = 1
ฯ(๐) = ๐(๐, ๐ก) = 0
This solution is chosen because we have a non homogeneous condition for ๐ฅ = 0 and ๐ฅ = ๐. If we
had, ๐(๐ฅ) = 0 and the solution would be the same as a non homogeneous PDE. Nevertheless the
conditions for ๐(๐ฅ, ๐ก) will always be homogeneous using this method. Applying the solution to
the PDE gives
๐๐
๐๐กโ ๐ (
๐2๐
๐๐ฅ2+
๐2๐
๐๐ฅ2) = 0
Because ๐(๐ฅ) does not depend on ๐ก. Letโs also assume that ๐2๐
๐๐ฅ2 = 0 so the equation above can
be written as
๐๐
๐๐กโ ๐
๐2๐
๐๐ฅ2= 0
Letโs assume a solution ๐(๐ฅ, ๐ก) = ๐(๐ฅ)๐(๐ก) and rearrange the equation as
1
๐๐๐โฒ = ๐๐โฒโฒ
Dividing both sides by ๐๐ 1
๐ ๐โฒ
๐=
๐โฒโฒ
๐
As both sides depend on different variables, we can assume the expression above can only be
equal to a constant arbitrarily called ๐2.
1
๐ ๐โฒ
๐=
๐โฒโฒ
๐= โ๐2
From this argument we have two ODE.
๐. ๐โฒ = โ๐๐2๐
๐๐. ๐โฒโฒ + ๐2๐ = 0
To solve the first ODE we only need to use separation of variables
๐โฒ
๐= โ๐๐2
1
๐
๐๐
๐๐ก = โ๐๐2
๐๐
๐= โ๐๐2๐๐ก
โซ๐๐โ
๐โ
๐
๐ด
= โซโ๐๐2๐๐กโ
๐ก
0
ln(๐) โ ln(๐ด) = โ๐๐2๐ก
ln (๐
๐ด) = โ๐๐2๐ก
๐(๐ก)
๐ด= ๐โ๐๐2๐ก
๐(๐ก) = ๐ด๐โ๐๐2๐ก
Now, solving the second equation, we have
๐โฒโฒ + ๐2๐ = 0
We know the solution for this sort of ODEโs is a sum of sines and cosines
๐(๐ฅ) = ๐ต1 cos(๐๐ฅ) + ๐ต2 sin(๐๐ฅ)
Now, letโs apply the boundary conditions to find ๐ต1 and ๐ต2.
๐(0, ๐ก) = ๐(0) + ๐(0, ๐ก) = 1
๐(๐, ๐ก) = ๐(๐) + ๐(๐, ๐ก) = 0
But it has been defined that ฯ(0) = 1 and ฯ(๐) = 0 so
๐(0, ๐ก) = 1 + ๐(0, ๐ก) = 1
๐(๐, ๐ก) = 0 + ๐(๐, ๐ก) = 0
Then ๐(0, ๐ก) = ๐(๐, ๐ก) = 0. Know, we can use the solution for ๐(๐ฅ, ๐ก) and find the constants.
๐(0, ๐ก) = ๐(0) ๐(๐ก) = 0
๐(๐, ๐ก) = ๐(๐) ๐(๐ก) = 0
As we donโt want the trivial solution (๐(๐ก) = 0), ๐(0) = ๐(๐) = 0. Applying this results in the
solution found for ๐(๐ฅ).
๐(0) = ๐ต1 cos(๐0) + ๐ต2 sin(๐0) = 0
๐ต1 1 + ๐ต2 0 = 0
๐ต1 = 0
๐(๐) = 0 cos(๐๐) + ๐ต2 sin(๐๐) = 0
๐ต2 sin(๐๐) = 0
If both ๐ต1 and ๐ต2 are zero, we have a trivial solution. So, to avoid it, sin(๐๐) = 0
๐๐ = ๐๐, ๐ โ ๐
๐๐ =๐๐
๐
๐๐(๐ฅ) = ๐ต2 sin(๐๐๐ฅ)
Now we can rewrite the solution for ๐(๐ฅ, ๐ก) (using only one coefficient called ๐ถ๐) as
๐๐(๐ฅ, ๐ก) = ๐ถ๐๐โ๐๐๐2 ๐กsin (๐๐๐ฅ)
๐๐ =๐๐
๐
And the solution for each ๐ฅ and ๐ก will be the summation of all the ๐๐ from ๐ = 1 to infinity,
because when ๐ = 0, ๐0 = 0
Now, itโs possible to find ๐(๐ฅ) because we assumed ๐2๐
๐๐ฅ2 = 0 and we know that it is not a zero
function. If the second derivative of a function is zero, the first derivative is a constant and the
function itself is given by
๐(๐ฅ) = ๐ถ1๐ฅ + ๐ถ2
Applying the boundary conditions
๐(0) = ๐ถ10 + ๐ถ2 = 1
๐ถ2 = 1
๐(๐) = ๐ถ1๐ + 1 = 0
๐ถ1๐ + 1 = 0๐
๐ถ1 = โ1
๐
๐(๐ฅ) = โ1
๐๐ฅ + 1
Now we have both functions that makes up the solution, we can write it as
๐(๐ฅ, ๐ก) = โ๐ฅ
๐+ 1 + โ ๐ถ๐๐
โ๐๐๐2 ๐ก sin(๐๐๐ฅ)
โ
๐=1
Now, letโs apply an arbitrary initial condition ๐(๐ฅ, 0) = ๐๐
๐(๐ฅ, 0) +๐ฅ
๐โ 1 = โ ๐ถ๐๐โ๐๐๐
20 sin(๐๐๐ฅ)
โ
๐=1
To solve for ๐ถ๐, we multiply both sides by sin(๐๐๐ฅ)
(๐๐ +๐ฅ
๐โ 1) sin(๐๐๐ฅ) = โ ๐ถ๐1 sin(๐๐๐ฅ) sin(๐๐๐ฅ)
โ
๐=1
Now we integrate both sides o the equation in ๐ฅ from 0 to ๐.
โซ (๐๐ +๐ฅ
๐โ 1) sin(๐๐๐ฅ) ๐๐ฅ
๐
0
= โซ โ ๐ถ๐ sin(๐๐๐ฅ) sin(๐๐๐ฅ)
โ
๐=1
๐๐ฅ๐
0
To simplify the expression above we can take summation out of the integral (because โthe integral
of sum is the sum of the integralsโ) and so ๐ถ๐, because it is a constant for each ๐.
โซ (๐๐ +๐ฅ
๐โ 1) sin(๐๐๐ฅ)๐๐ฅ
๐
0
= โ ๐ถ๐ โซ sin(๐๐๐ฅ) sin(๐๐๐ฅ)๐
0
๐๐ฅ
โ
๐=1
We also know that the integral of a product of two sine functions only returns a non-zero result
when the argument of both functions are the same, therefore ๐ = ๐ and the result is ๐
2.
โซ (๐๐ +๐ฅ
๐โ 1) sin(๐๐๐ฅ) ๐๐ฅ
๐
0
= โ๐ถ๐๐
2
โ
๐=1
Applying these arguments and solving for ๐ถ๐ gives us
๐ถ๐ =2
๐โซ (๐๐ +
๐ฅ
๐โ 1) sin(๐๐๐ฅ)๐๐ฅ
๐
0
And that is a general solution that can be applied for any initial condition. As we know that the
initial condition is ๐๐ = sin (๐๐ฅ
๐), we can find each ๐ถ๐
๐ถ๐ =2
๐โซ (sin (
๐๐ฅ
๐) +
๐ฅ
๐โ 1) sin(๐๐๐ฅ)๐๐ฅ
๐
0
๐ถ๐ =2
๐[โซ sin (
๐๐ฅ
๐) sin (
๐๐๐ฅ
๐)๐๐ฅ
๐
0
+1
๐โซ ๐ฅ sin (
๐๐๐ฅ
๐)๐๐ฅ
๐
0
โ โซ sin (๐๐๐ฅ
๐) ๐๐ฅ
๐
0
]
The solution for the first term will only be non-zero when the arguments are the same, therefore,
for ๐ = 1 the solution for the first integral is ๐
2.
To get the solution for the second integral we should solve it by parts
โซ ๐ฅ sin (๐๐๐ฅ
๐)๐๐ฅ
๐
0
๐ข = ๐ฅ, ๐๐ข = ๐๐ฅ
๐๐ฃ = sin (๐๐๐ฅ
๐)๐๐ฅ, ๐ฃ = โ
๐
๐๐๐๐๐ (
๐๐๐ฅ
๐)
โซ ๐ฅ sin (๐๐๐ฅ
๐)๐๐ฅ
๐
0
= โ๐ฅ๐
๐๐cos (
๐๐๐ฅ
๐)0
๐
+๐
๐๐โซcos (
๐๐๐ฅ
๐)๐๐ฅ
๐
0
โซ ๐ฅ sin (๐๐๐ฅ
๐)๐๐ฅ
๐
0
= โ๐ฅ๐
๐๐cos (
๐๐๐ฅ
๐)0
๐
+๐
๐๐(
๐
๐๐sin (
๐๐๐ฅ
๐))
0
๐
โซ ๐ฅ sin (๐๐๐ฅ
๐)๐๐ฅ
๐
0
=๐2
๐๐cos(๐๐ )
The solution for the third and last integral is get using a simple substitution of variables
โซ sin(๐๐๐ฅ
๐) ๐๐ฅ
๐
0
๐ข =๐๐๐ฅ
๐
๐๐ฅ = ๐๐๐ข
๐๐
๐ข(0) = 0, ๐ข(๐) = ๐๐
๐
๐๐โซ sin(๐ข) ๐๐ข
๐๐
0
โซ sin(๐๐๐ฅ
๐)๐๐ฅ
๐
0
= โ๐
๐๐(cos(๐๐) โ cos(0))
โซ sin(๐๐๐ฅ
๐)๐๐ฅ
๐
0
=๐
๐๐(1 โ cos(๐๐))
Using these solution to find ๐ถ1
๐ถ1 =2
๐[๐
2+
1
๐(
๐2
1๐cos(1๐ )) โ (
๐
1๐(1 โ cos(1๐)))]
๐ถ1 =2
๐[๐
2โ
๐
๐โ (
๐
๐(2))]
๐ถ1 =2
๐[๐ (
1
2โ
1
๐โ
2
๐)]
๐ถ1 = 1 โ6
๐
For every ๐ โ 1
๐ถ๐ =2
๐[1
๐(
๐2
๐๐cos(๐๐ )) โ (
๐
๐๐(1 โ cos(๐๐)))]
๐ถ๐ =2
๐[
๐
๐๐cos(๐๐) โ (
๐
๐๐(1 โ cos(๐๐)))]
For odd ๐, cos(๐๐) = โ1
๐ถ๐,๐๐๐ =2
๐[
๐
๐๐(โ1) โ (
๐
๐๐(1 โ (โ1)))]
๐ถ๐,๐๐๐ =2
๐[โ
๐
๐๐โ
2๐
๐๐]
๐ถ๐,๐๐๐ =2
๐[โ
3๐
๐๐]
๐ถ๐,๐๐๐ = โ6
๐๐
For even ๐, cos(๐๐) = 1
๐ถ๐,๐๐ฃ๐๐ =2
๐[
๐
๐๐(1) โ (
๐
๐๐(1 โ 1))]
๐ถ๐,๐๐ฃ๐๐ =2
๐[
๐
๐๐]
๐ถ๐,๐๐ฃ๐๐ =2
๐๐
(b) The heat equation is
๐๐
๐๐กโ ๐
๐2๐
๐๐ฅ2= 0
First, letโs assume a solution in the format
๐(๐ฅ, ๐ก) = ฯ(x) + ๐(๐ฅ, ๐ก)
And also
ฯ(0) = ๐(0, ๐ก) = 1
But this time, we have one more expression
โT
โx(๐, ๐ก) = 0
Applying the solution to the PDE gives
๐๐
๐๐กโ ๐ (
๐2๐
๐๐ฅ2+
๐2๐
๐๐ฅ2) = 0
Assuming that ๐2๐
๐๐ฅ2 = 0
๐๐
๐๐กโ ๐
๐2๐
๐๐ฅ2= 0
We also assume the same solution ๐(๐ฅ, ๐ก) = ๐(๐ฅ)๐(๐ก) so when applied in the equation above we
have the same ODEโs as before which solutions are
๐(๐ฅ) = ๐ต1 cos(๐๐ฅ) + ๐ต2sin(๐๐ฅ)
๐(๐ก) = ๐ด๐โ๐๐2๐ก
Applying the first boundary condition
๐(0, ๐ก) = ๐(0) + ๐(0)๐(๐ก) = 1
1 + ๐(0)๐(๐ก) = 1
Again, to avoid the trivial solution, ๐(0) = 0 then
๐(๐ฅ) = ๐ต2sin(๐๐ฅ)
Applying the second boundary condition
๐๐
๐๐ฅ(๐ฅ, ๐ก) = ๐โฒ(๐ฅ) + ๐โฒ(๐ฅ)๐(๐ก)
๐๐
๐๐ฅ(๐, ๐ก) = ๐โฒ(๐) + ๐โฒ(๐)๐(๐ก) = 0
To apply the boundary condition, letโs assume ๐๐
๐๐ฅ(๐) = 0, so ๐โฒ(๐) = 0 to avoid the trivial solution
then
๐โฒ(๐ฅ) = ๐ต2๐ cos(๐๐ฅ)
๐โฒ(๐) = ๐ต2๐ cos(๐๐) = 0
Once again to avoid the trivial solution, letโs assume the cosine as zero. We know that a cosine is
zero when the argument is an odd number times ๐. This can be written as
๐๐ =๐๐
2
๐ =๐๐
2๐, ๐ = 1, 3, 5, โฆ
๐(๐ฅ) = ๐ต2 sin (๐๐๐ฅ
2๐)
Now we need to find ๐(๐ฅ). As its second derivative is zero the first derivative should be a
constant.
But, as we assumed ๐๐
๐๐ฅ(๐) = 0, then
๐๐
๐๐ฅ(๐ฅ) = 0. Therefore, ๐(๐ฅ) is a constant too.
As we assumed ๐(0) = 1, the constant is 1, so
๐(๐ฅ) = 1
Now we can write the function for the temperature (combining ๐ด and ๐ต2 into one constant ๐ถ๐)
๐(๐ฅ, ๐ก) = 1 + โ๐ถ๐๐โ๐๐๐2 ๐ก
โ
1
sin(๐๐๐ฅ)
To calculate ๐ถ๐ we need to apply the initial condition, which is ๐(๐ฅ, 0) = 0. We need to take the
same steps as in (a) to solve for ๐ถ๐
0 = 1 + โ๐ถ๐๐โ๐๐๐
20
โ
1
sin (๐๐๐ฅ
2๐)
โ๐ถ๐1
โ
1
sin (๐๐๐ฅ
2๐) = โ1
โ๐ถ๐
โ
1
sin (๐๐๐ฅ
2๐) sin (
๐๐๐ฅ
2๐) = โ1sin (
๐๐๐ฅ
2๐)
โซโ๐ถ๐ sin (๐๐๐ฅ
2๐) sin (
๐๐๐ฅ
2๐) ๐๐ฅ
โ
1
๐
0
= โซโ1sin (๐๐๐ฅ
2๐) ๐๐ฅ
๐
0
โ๐ถ๐ โซsin(๐๐๐ฅ
2๐) sin (
๐๐๐ฅ
2๐)๐๐ฅ
๐
0
โ
1
= โโซsin(๐๐๐ฅ
2๐)๐๐ฅ
๐
0
โ๐ถ๐๐
2
โ
1
= โโซsin (๐๐๐ฅ
2๐)๐๐ฅ
๐
0
โ๐ถ๐๐
2
โ
1
= (2l
nฯcos (
๐๐๐ฅ
2๐))
0
๐
๐ถ๐ =4
๐๐[cos (
๐๐
2) โ cos(0)]
๐ถ๐ = โ4
๐๐
The solution can be written simply as
๐(๐ฅ, ๐ก) = 1 โ โ4
๐๐๐โ๐๐๐
2 ๐ก
โ
1
sin(๐๐๐ฅ)
Where ๐๐ =๐๐
2๐ and ๐ = 1, 3, 5, โฆ
2. To solve this problem numerically, we need to divide the rod of length ๐ in ๐ผ divisions. Now we
solve the heat equation for ๐๐
๐๐ก
๐๐
๐๐กโ ๐
๐2๐
๐๐ฅ2= 0
๐๐
๐๐ก= ๐
๐2๐
๐๐ฅ2
First, we apply the approximation for the second degree derivative
๐๐
๐๐ก=
๐
ฮ๐ฅ2[๐๐โ1 โ 2๐1 + ๐๐+1]
Now we can approximate the temperature of the ๐๐กโ point of the rod at the ๐๐กโ point in time
applying Eulerโs explicit method
๐๐๐+1 = ๐๐
๐ +๐ฮ๐ก
ฮ๐ฅ2[๐๐โ1
๐ โ 2๐๐๐ + ๐๐+1
๐ ]
Where ๐ฮ๐ก
ฮ๐ฅ2 is the Courant or CFL number, which we will call ๐ฝ. The equation can be simplified to
๐๐๐+1 = ๐ฝ๐๐โ1
๐ + (1 โ 2๐ฝ)๐๐๐ + ๐ฝ๐๐+1
๐
However, due to the second initial condition is ๐๐
๐๐ฅ(๐, ๐ก) = 0, we donโt have any information about
the value of the temperature at ๐ฅ = ๐ except that its derivative is zero.
We can argument that if so, the temperature at that point doesnโt change in respect to length
only, so we can calculate the temperature of the point before (๐ผ โ 1) that the temperature of the
last point will be the same.
This can be proven using the backwards difference to calculate the derivative for the ๐ผ๐กโ point,
where ๐ฅ = ๐
๐๐
๐๐ฅ(๐, ๐ก) =
๐๐ผ๐ โ ๐๐ผโ1
๐
ฮ๐ฅ= 0
๐๐ผ๐ = ๐๐ผโ1
๐
Now we can apply this result to find the temperature of the second to last point
๐๐ผโ1๐+1 = ๐ฝ๐๐ผโ2
๐ + (1 โ 2๐ฝ)๐๐ผโ1๐ + ๐ฝ๐๐ผ
๐
๐๐ผ๐ = ๐๐ผโ1
๐
๐๐ผโ1๐+1 = ๐ฝ๐๐ผโ2
๐ + (1 โ 2๐ฝ)๐๐ผโ1๐ + ๐ฝ๐๐ผโ1
๐
๐๐ผโ1๐+1 = ๐ฝ๐๐ผโ2
๐ + (1 โ ๐ฝ)๐๐ผโ1๐
The temperature of the last point will be the same as the one calculated above as proven before.
We can calculate the temperature for all the points at the (๐ + 1)๐กโ instant using the following
vector equation
๏ฟฝโ๏ฟฝ ๐+1 = ๐๏ฟฝโ๏ฟฝ ๐
Where ๏ฟฝโ๏ฟฝ ๐ = {
๐1๐
๐2๐
โฆ๐๐ผโ1
๐
} and ๐ is the following matrix
1 0 0 0 โฏ 0 0 0 0 ๐ฝ 1 โ 2๐ฝ ๐ฝ 0 โฏ 0 0 0 0 0 ๐ฝ 1 โ 2๐ฝ ๐ฝ โฏ 0 0 0 0 โฎ โฎ โฎ โฎ โฑ 0 0 0 0 0 0 0 0 โฏ ๐ฝ 1 โ 2๐ฝ ๐ฝ 0 0 0 0 0 โฏ 0 0 ๐ฝ 1 โ ๐ฝ
Notice that the product between the two matrices doesnโt change the value for ๐1 at any time.
Also the only other different expression is the one to calculate ๐๐ผโ1 which was shown before.
The values for ๐๐ผ (where ๐ฅ = ๐) should be calculated โmanuallyโ equaling it to ๐๐ผโ1 which was also
shown before.
The attached MATLAB code implementing operations to calculate the temperature at each point
of a bar with ๐ = 1 and ๐ = 1 at ๐ก = 0.02 using the conditions given in the problem 1.b. In the
code, the true analytical solution is also calculated for a spatial grid of size 50 and 6 harmonics,
which are enough to produce a smooth curve using the equation derived in 1.b.
Before calculating the solution for this problem, it is needed to define the spatial grid size (๐ผ) and
the time step (ฮ๐ก) which will define the Courant number (๐ฝ).Through trial and error, it can be
seen that the maximum ๐ฝ that produces a reliable approximation cannot be bigger than 0.5. So it
has been tested values of ๐ผ and ฮ๐ก which ๐ฝ = 0.5 as shown in the next table.
๐ถ๐๐ ๐ 1 2 3 4 ๐ผ 20 25 30 35 ๐ฅ๐ก 0.00125 0.0008 0.0005554 0.0004082
The table above starts with 20 spatial divisions just so we have enough points to plot a curve.
Notice that even though all cases have the same Courant number, the time step used to get it
increases quicker than the spatial grid size. This happens because the Courant number varies with
the inverse of the square of ๐ผ (๐๐๐ ๐ฝ โ1
ฮ๐ฅ2 ๐๐๐ ฮ๐ฅ โ1
๐ผ). The graphics for each case are attached
to this document.
Analysing the curves, we can see that the three first cases produce results close to the correct
solution, but only the case 4 almost overlap it. And it uses only 49 time steps to do so, which is
about 3 times the amount of time steps in case 1. Notice that it was used the largest time step
possible for each case, so we can still get reliable results using the least amount of processing
power possible. Even a simple PDE as the one in this problem may require a long time to calculate
the solution after a long time and for a big spatial grid size.
So the solution chosen for this problem is the case 4, using 35 spatial divisions and a time
step ฮ๐ก = 4.082 โ 10โ4 because it produces a close solution to the analytical one for a โlow costโ.
Notice that for a small time as ๐ก = 0.02 it is needed a more precise time step to get a reliable solution.
The analytical solution requires infinite amount of harmonics to produce an exact solution. For this
example, using 1, 2 or 3 harmonics do not produces reliable enough. For 5 harmonics or more the
difference in the graphic is almost imperceptible.
Analyzing the behavior of the temperature, as ๐ก is small and all the points starts at ๐ = 0 except for the
first point, the rod is still โwarming upโ, and so the heat is not well distributed along it. After some seconds
the temperature of the bar will be at the same temperature ๐ = 1, which is the value of ๐(๐ฅ) calculated
in 1.b, because the exponential factor of the solution will render the ๐(๐ฅ, ๐ก) part closer to zero in the
expression for ๐(๐ฅ, ๐ก) as the time increases.
Finally, it can be seen that the approximation for a differential equation is harder to do where there is
more variation. This effect is further amplified in a PDE because the time step has time to be managed to
get a steady-state. However, for points where there is no variation this method approximates well, which
can be seen as the numerical solution overlaps the analytical solution after ๐ฅ = 0.6 ๐.
Attachments
Plots for the question 2
Full Code for Question 2
clear, clc
% Defining the spatial grid size and the time step I = 35; dt = 0.0004082;
% Setting the length and the constant k as 1 l = 1; k = 1;
% Calculating spatial step dx, the vector x for the plot % and the amount of time steps N dx = l/I; x = linspace(0, l, I); x = x'; t = 0.02; N = t/dt;
% Creating a matrix with the temperatures at initial instant. For % calculations, the last point is not considerated now T = zeros((I-1), 1); T(1) = 1;
% Calculating the CFL constant beta = k*dt/(dx)^2;
% Calculating the matrix b used in the multiplication to find each new % vector T with the new temperatures b = zeros(I-1); b(1,1) = 1; b(I-1,I-2) = beta; b(I-1,I-1) = 1 - beta;
for i=2:I-2 b(i,i-1) = beta; b(i,i) = 1 - 2*beta; b(i,i+1) = beta; end
% Calculating the temperatures for each point for i=1:N T = b*T; end
% Creating a vector y to plot the results. The last element of this vector % has the same value of the second to last, because their temperatures are % the same y = zeros(I,1); for i = 1:I-1 y(i) = T(i); end y(I) = y(I-1);
% Creating a vector (true) with the true results and getting the amount of % harmonics (H). The size (a) of the solution vector is just an amount big % enough to produce a smooth curve in the graphic. Another vector xx a = 50; true = zeros(a, 1); xx = linspace(0, 1, a+1); H = 6;
% Calculating the true results % First: each point is assigned with the value of 1 % Second: the current harmonic (h) is calculated and ranges from 1 to the % H-th odd natural number % Third: lambda is calculated % Fourth: With the variables calculated above, the true result for the i-th % point is accumulated H times in the vector element true(i) for i = 1:a+1 true(i) = 1; for j = 1:H h = 2*j-1; lambda = h*pi/(2*l); true(i) = true(i) + (-4/(h*pi))*(exp(-k*lambda^2*t))*(sin(lambda*((i-
1)*l/a))) ; end end
% Plotting the numerical and true result in the same plot hold on; plot(x,y,'k',xx,true,'r'); legend('Numerical Solution','Analytical Solution'); xlabel('length (m)'); ylabel('Temperature');