Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function u - Heat Equation D(h, k, n, alpha, u0) This function finds the numeric

ID: 1843190 • Letter: F

Question

Function u - Heat Equation D(h, k, n, alpha, u0) This function finds the numerical solution to the one-dimensional heat equation for a symmetric rod. It returns a matrix u which has one column for each time point and each column gives the temperature profile along the rod. The boundary conditions are that the ends of the rod are maintained at a temperature of zero. is distance step; k is the time step? and n is the number of time steps alpha is the thermal diffusivity; u0 is a vector giving is the initial temperature distribution in the rod m = length(u0); u = zeros(m, n+1); r = alpha*k/h^Lambda 2; if r > 0.5 warning('Method is unstable and results will be inaccurate.') end copy over the initial conditions for i = l:m u(i, l)= u0(i); end solve for the output temperatures for j = l:n for i = 2:m-l u(i/j+1) = (l-2*r)*u(i, j)+r*(u(i-1, j)+u(i+1, j)); end end

Explanation / Answer

Explanation is given below:
m=length(u0)

will provide you number of elements available in u0. where, u0 is your initial temperature distribution.

for example if you have u0 = [0, 0.1, 0.15, 0.2, 0.25 ];

then it will provide you number of element u0 have , in this case u0 have 5 elements.

so m = length(u0) = 5.

u=zeros(m,n+1);

you need to solve the problem for n time step and at each step you need to calculate temperature of each point.

this statement will provide you matrix that contains 0 and the dimention of the matrix is m raws and n+1 columns.

Please note that you already have initial temperature with you so , for total n time step you have n+1 value of temperature at each point. and since total points are m, (as find out in previous step)

That is why we create matrix field with 0 and dimention of (m,n+1).

(Note : this statement will reduce the time required to solve the problem, because if you do not write this statement then matlab need to resize the matrix dimention at each iteration of for loop, so this statement allow matlab to only replace the value of zero by actual temperature and Matlab do not need to change dimention of matrix at each iteration. for example in the beggining variable u is stored as a single float, but in next step it need to convert it into matrix of 1 raw and two column and in next iteration matlab need tp convert it into 1 raw 3 column. This need much more time. if we provide information to matlab that u in metrix of dimention m*n+1, calculation can be done much faster.)

r= alpha*k/h^2;

This statement calculate error, if value of r is more than 0.5 it means method used is instable and provide inaccurate answer.

In that case you need to increase value of h.

if , else

this loop will provide you the ability to chech that calculated value of r is more than 0.5 or not.

If value if r is more than 0.5 , it will execute the warning message. otherwise it will ignore the warning message.

for loop statement for copy over initial condition

you already have initial temperature distribution of the temperature over the whole body.

your final answer is [u] matrix.

by this statement you copy your initial value to first column of the result matrix [u].

for loop for solve for output temerature:

This will calculate final answer for your tempearture and form matrix [u].

first for loop is from 1 to n ,

j=1:1:n

it means variable j, strat from 1 (first 1) goes upto n in increment of 1 (middle 1).

if middle number is not secified it will by default consider it as 1.

so j=1:1:n is equal j=1:n

Please not taht this first for loop allow you to nevigate different column of the matrix [u].

second loop calculate the value of [u] for different raw for same column.

Logic will provide you the answer and assign it to appropriate place in matrix [u].

please not that i=2:1:m-1 indicate that,

it will only modify 2nd to (m-1)th raw. .

because according to boundary condition temperature at first and last node is 0.

May this explaination gives some understanding about code.

.
If you require any further explaination, feel free to comment.
.


.
---------------------------------------------------------------------------------------
*****=====-----_____     Have a Nice Day     _____-----=====*****
---------------------------------------------------------------------------------------
.