# Need help with the functions to use for this problem [closed]

Consider the following example. Suppose there is a thin rod which is insulated along its length. Suppose that the temperature is initially zero everywhere, and that the left end is suddenly heated and kept at 20 degrees. Finally we set α = 1/2. In notation, we have set the spatial interval is [−5, 5], Δx = 0.1, Δt = 0.01 and λ = 1/2. For the initial setting take u(x, 0) = 0 and for the boundary values take u(– 5, t) = 20 and u(5, t) = 0. Using (4.2.4), we solve for approximate values of u along the interval.

A) Do Exercise 1 of Section 4.2 using implicit FDM. B) Do Exercise 1 of Section 4.2 using Crank Nicolson FDM.

• Using (4.2.4), what is this? what is $\alpha$? it will be easier and more clear if you just scan the whole problem from the textbook with any equations given. From which book is this problem? which section/page number? I might have the book myself. For Mathematica solution, you can use NDSolve to solve the heat pde numerically and compare with that . Dec 4, 2022 at 21:09
• I noticed now you asked the same question few times before. But have not provided the additional information requested before from you. Please see comments to your previous closed questions. I suggest you try scicomp.stackexchange.com which is a forum dedicated for numerical solving and numerical methods. It might be better received there. Dec 4, 2022 at 21:15
• Its the Elements of Numerical Analysis with Mathematica by John Loustau section 4.3 questions 1 and 2. Dec 4, 2022 at 21:42
• Are you sure? I have the book in front of me, and it says for problem 1 and 2, section 4.3 the following: !Mathematica graphics To get better help, it is better to post exact problem as given from the book. I know now the book is about using Mathematica to solve ode's and pde's. The above is from a pdf copy I found on the net. I do not know if there is different version or not. Dec 4, 2022 at 21:58
• sorry, meant to say 4.4 Dec 4, 2022 at 21:59

This is modification of the answer in Finite difference method for 1D heat equation to make it fit your problem.

To solve problem 1 at page 83 of book

Compare to the book plot

Here is the code

makeA[n_, λ_] :=
Module[{A, i, j}, A = Table[0, {i, n}, {j, n}];
Do[Do[A[[i, j]] =
If[i == j, 1 - 2 λ,
If[i == j + 1 || i == j - 1, λ, 0]], {j, 1, n}], {i, 1,
n}];
A[[1, 1]] = 1;
A[[1, 2]] = 0;
A[[-1, -1]] = 1;
A[[-1, -2]] = 0;
A]

makeInitialU[nPoints_, ic_, leftBC_, rightBC_] := Module[{u, j},
u = Table[0, {j, nPoints}];
Do[u[[j]] = If[j == 1, leftBC, If[j == nPoints, rightBC, ic]],
{j, 1, nPoints}
];
u]

updateU[currentU_, leftBC_, rightBC_, A_] := Module[{u},
u = A . currentU;
u[[1]] = leftBC;(*set to BC condition*)
u[[-1]] = rightBC;(*set to BC condition*)
u]

ic = 0;
leftBC = 20;
rightBC = 0;
α = 1/2;
delT = 0.01;
delX = 0.1;
λ = (α delT )/delX;
xGrid = Range[-5, 5, delX];
n = Length[xGrid];
(A = makeA[n, λ]) // MatrixForm;


And the Manipulate code is

Manipulate[
If[currentTime == 0,
currentU = makeInitialU[n, ic, leftBC, rightBC],
currentU = updateU[currentU, leftBC, rightBC, A]
];
Grid[{{Row[{"time = ", currentTime}]},
{ListPlot[currentU, PlotRange -> {Automatic, {0, 20}},
ImageSize -> 300, DataRange -> {-5, 5}]}
}],

{{currentTime, 0, "time?"}, 0, 10, delT, Appearance -> "Labeled"},
{{currentU, 0}, None},
TrackedSymbols :> {currentTime}
]


Problem 2 you just need to modify $$\alpha$$ which will change $$\lambda$$ and you should see from the simulation it becomes not stable when $$\lambda>0.5$$