Question

In: Mechanical Engineering

Write a function to solve the two-dimensional in Matlab, unsteady heat conduction equation with no internal...

Write a function to solve the two-dimensional in Matlab, unsteady heat conduction equation with no internal heat generation on a square domain. The length of each side of the square is 1 m. The function will have the following 4 inputs:

npts                     number of grid points in each coordinate direction

nt                         number of time steps to take

dt                         size of time step (s)

alpha                   thermal diffusivity (m2/s)

Use the following initial and boundary conditions:

Initialize the body to T = 0 oC. At time t = 0, boundary conditions are imposed such that the temperatures on the sides of the square vary linearly. Here are the corner temperatures:

bottom left corner:         T =   50 oC

bottom right corner:      T = 500 oC

top right corner:             T = 350 oC

top left corner:               T = 150 oC

Write the function definition statement. If you type doc function at the MATLAB command prompt it will give you details on the proper syntax. The syntax is general, showing how to use both inputs and outputs. We will have no outputs, only the four inputs noted above.

Make the name of your function the same as the name of your file. For example, if you call your function heat, then your filename should be heat.m.

Create a variable name for each of the four corner boundary conditions and the initial condition, and set their values.

Noting that the temperature on each edge varies linearly, calculate the temperature at every grid point on the outer boundaries. You’ll need the corner temperatures to do this calculation.

Create an array to contain the “old” temperature field at time level p, and call it told. A convenient way to do this is to use the ones function (look at doc ones), which creates an array of all ones of specified dimensions. An easy way to set the field to the initial condition is to multiply this array by the initial temperature.

If there are any calculated variables that you will use multiple times, calculate them. For example, it’s handy to have a variable npm = npts – 1, where npts is the input number of points in each direction.

Create an array to contain the “current” solution at time level p+1, and call it tnew. An easy way to do initialize it in MATLAB is to simply use

tnew = told;

Create a time loop going from 1 to nt, where nt is the input number of time steps.

Inside the time loop, create two other nested loops, one for the “m” indices (x direction) and one for the “n” indices (y direction). Since we don’t have to re-calculate any values on boundaries, the loops will go from 2 to npm.

For each pair of indices within the loops, calculate the solution at time level p+1 (tnew) as a function of temperatures at time level p (told) using the finite-difference form of the 2-D heat equation.

We are going to do a simple animation of the unsteady results as they evolve. Use the contourf function to create the evolving contour plot. (doc contourf) One of the arguments is the number of contours, and 20 seems to work well for this problem. We want to re-draw the contour plot once per time step, so contourf should be called after the end of the nested spatial loops, but before the end of the time loop. It’s not necessary to use a plot handle as we did in the example in class. Simply call contour each time through the loop.

MATLAB tries to be efficient, so when it sees the contourf function, it will buffer the results rather than plotting them right away. We don’t want it to buffer since we want to create a real-time animation. To prevent buffering, include a drawnow statement on a separate line immediately after the call to contourf. (This is similar to the example we did in class.)

Now that the current time step is complete, we have to copy the new solution to the old one so we can go on to the next iteration, told = tnew. Make this the last line before the end of the time loop.

Add the following to the end of the time loop. The first line adds a “colorbar,” which is a scale showing the correspondence between temperatures and colors. The second line adds a label to the colorbar. Finally, the third line gets rid of the plot axes, since we don’t need them here.

hc = colorbar;

hc.Label.String = ‘Temperature, deg. C’;

axis off

When you’re got your program ready to run, use the following input values:

npts = 40

nt = 1000

dt = 0.1

alpha = 0.001

To run the program, simply type

heat(40, 1000, 0.1, 0.001)

Solutions

Expert Solution

%% Here is the Code of the main function. Just copy it all and paste in your Matlab software, then save it as it is and try for different values of inputs.

function Heat(np,nt,dt,alpha)
n= np ; m= np;
L = 1 ; % Length of plate
dx = L / n ;
Omega = (dt* alpha/(dx^2 ));

% Given Temperatures
T0 = 0 ; % Initial Temperature
T1 = 50 ; % Temperature at bottom left corner
T2 = 500 ; % Temperature at bottom right corner
T3 = 350 ; % Temperature at top right corner
T4 = 150 ; % Temperature at top left corner
T_old = T0*ones(nt,m,n);
% Initial conditions
for k=1:nt
for i = 1:m
for j = 1:n
T(k,i,j) = T_old(k,i,j);
end
end
end

% Corner Conditions
T(1,1,1) = 50;
T(1,m,1) = 500;
T(1,m,n) = 350;
T(1,1,n) = 150;
for k=1:nt
for i=2:m %% Bottom Boundary
  
T(k,i,1) = 50 + 450*(i).*dx./L ;
end
for i=2:m %% top Boundary
  
T(k,i,n) = 150 + 200*(i).*dx./L ;
end
for j=2:n %% left Boundary
  
T(k,1,j) = 50 + 100*(j).*dx./L ;
end
for j=2:m %% Right Boundary
  
T(k,m,j) = 500 - 150*(j).*dx./L ;
end
end

T_old = T;
for k = 1:nt-1;
T_old = T ;
for i = 2:m-1
for j = 2:n-1

T(k+1,i,j)= T_old(k,i,j) + Omega*(T_old(k,i+1,j) - 4*T_old(k,i,j)...
+ T_old(k,i-1,j) +T_old(k,i,j+1) + T_old(k,i,j-1));
%



end
end
  
end
x = linspace(0,L,40);
y=x;
[x,y]=meshgrid(x,y);
Tp=squeeze(T(nt,:,:));
Cs = contourf(y,x,Tp,20,'linewidth',2);
xlabel('Length of wall in m','fontsize',14,'fontweight','bold','fontangle','italic');
ylabel('Height of wall in m','fontsize',14,'fontweight','bold','fontangle','italic');
title('Temperature(\circ C) Distribution in Square plate','fontsize'...
,14,'fontweight','bold','fontangle','italic');
set(gca,'fontsize',14,'fontweight','bold','fontangle','italic');
clabel(Cs,'fontsize',14,'fontweight','bold','fontangle','italic');
end

%%%%%% End of the code %%%%%%%%%%%%%%%%%

%% Here is the result for

npts = 40

nt = 1000

dt = 0.1

alpha = 0.001



Related Solutions

Write the heat conduction equation (without flow in and out) in cartesian coordinates for the following...
Write the heat conduction equation (without flow in and out) in cartesian coordinates for the following case: 1. Steady-state, 1-D, without heat generation (2 points) 2.Transient, 1-D, without heat generation (2 points) 3.Transient, 3-D, with heat generation (3 points) 3. Write the three types of boundary conditions. (3 points)
Solve the schordingers equation for a particle in a rigid two-dimensional box.
Solve the schordingers equation for a particle in a rigid two-dimensional box.
Solve the heat equation (in one dimensional case) for c^2=9, the following boundary and initial conditions:...
Solve the heat equation (in one dimensional case) for c^2=9, the following boundary and initial conditions: u(0,t)=u(2Pie,t)=0 u(x,0)=5sinx = 2sin5x
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial...
Write a MATLAB function function = pivGauss(.....) to solve linear equations using Gaussian Elimination with Partial Pivoting. You'll need to employ Nested Loops. Thank you !
creat a Matlab simulation the solves the 2D heat equation.
creat a Matlab simulation the solves the 2D heat equation.
Solve the two-dimensional wave equation in polar coordinates and discuss the eigen values of the problem.
Solve the two-dimensional wave equation in polar coordinates and discuss the eigen values of the problem.
Develop and explain each step the general equation of heat transfer by conduction in a constructive...
Develop and explain each step the general equation of heat transfer by conduction in a constructive way starting from an element general differential in the cartesian coordinate system and then reduce to get: 1) Laplace's equation (in R3) Δ? = 0 2) The Poisson equation (with constant thermal conductivity ?) Δ? + ? '''= 0 Δ is the Laplacian operator and ? ''' is a source term. Note: start the analysis from a differential element with volume ??????. Make a...
Write MATLAB scripts to solve problem 5.21 from the textbook (bisection, specific heat) four times: a)...
Write MATLAB scripts to solve problem 5.21 from the textbook (bisection, specific heat) four times: a) with the bisection method (starting interval [0, 1200]) b) with the false-position method (starting interval [0, 1200]) c) with the Newton-Raphson method (x0 = 0) d) with the secant method ((x0, x1) = (0, 1)) use percent relative error = 10^-6 as stopping criterion. 5.21-Mechanical engineers, as well as most other engineers, use thermodynamics extensively in their work. The following polynomial can be used...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT