20
Computer Based Modelling Project Report Robert Tanner, University of Bristol May 2, 2014

Robert_Tanner_Temperature_Distribution

Embed Size (px)

Citation preview

Computer Based Modelling Project Report

Robert Tanner, University of Bristol

May 2, 2014

Contents

1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Script Development . . . . . . . . . . . . . . . . . . . . . . . . . . 2

3.1 GUI Design . . . . . . . . . . . . . . . . . . . . . . . . . . 23.2 Correcting Temperature Values . . . . . . . . . . . . . . . 43.3 Temperatures in the Hole . . . . . . . . . . . . . . . . . . 6

4 Completed Script . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Future Developments . . . . . . . . . . . . . . . . . . . . . . . . . 18

1 Summary

Designing this GUI was extremely challenging, and one of the most difficultprojects I did this year. The project s[ecification was to design a GUI thatwould model the distribution of temperature across a plate in accordance withLaplace’s equation, in an iterative process where the temperature at every arbi-trary point on the plate was corrected to the mean of the four points immediatlyadjacent to it. The data used could either be a dataset specified by the useror a default set. The code grew from the a program that used the default setand allowed the user to only specify the spacing between the points and preci-sion of the iterations to a full-fledged GUI that allowed the userr to change anyvalue and produce a series of charts to model this data. Designing a method tocorrect the temperatures to the mean of the surrounding values in accordancewith Laplace’s equation was a diffcult puzzle that I eventually solved through aseries of complex matrix operations, in a method that produced corrected tem-peratures quickly and efficiently, but only through a systematic series of lengthymatrix operations. I also encountered a significant challenge in attempting tocode the behavior of the hole filled with conducting liquid in the centre of theplate that is fixed at a temperature the average of all points around it, as it re-peatedly encountered NaN errors and would not outpu any useful values. Boththese problems were resolved however, and I was able to program a GUI thatallows the user to input important variables, whether of their own choosing ora default case, and produce a set of clear and informative graphs that modelledthe temperature distribution. Ultimately this resulted in a code that was an ef-fective tool for the purpose it was designed, but could potentially receive furtherdevelopment to increase user friendliness, aesthetics or allow the comparison ofseveral data sets in quick succession.

1

2 Introduction

The purpose of this project was to write a MATLAB script that would modelthe temperature distribution across a plate that was heated to a constant tem-perature in the upper left corner and cooled to a constant temperature in thebottom right, and had a hole containing a conductive liquid that ensured allpoints in the hole are equal to the average of the temperatures surrounding thehole. The script must therefore first define a matrix that represents the temper-atures across the plate, estimate the initial temperatures of all the points on theplate, and from that carry out an interative process to calculate each correctedtemperature, repeating the process until the error of the iteration drops belowa set level. The corrected temperature of each point is the average of the pointssurrounding it on the plate. The project specified that the program must makeit possible to change every significant variable of the model, but also allow fora default case, with the plate measuring 0.6 by 0.4 metres and a spacing of 0.01metres between each point, heated to 100 degrees Kelvin 0.2 metres along theupper left corner and cooled to 0 degrees Kelvin 0.1 metres along the bottomright, the hole measuring 0.3 by 0.1 metres, the upper left corner of the holebeing situated 0.2 metres horizontally and vertically from the upper left of theplate. The default iteration error level is 0.01. Therefore, I created a scriptbased on the special case defined in the brief, and then altered it into a moregeneral format, and created a GUI to act as a control panel for data input, withthe option of using a default dataset. The development cycle of the project isgiven in the following section of this report.

3 Script Development

3.1 GUI Design

In designing the GUI itself I decided to forgo using Guide, therefore my projectis entirely programmatic, and I therefore took a great deal of information fromthe Simple Programmatic GUI guide.[1] I felt a greater degree of control over theprogram by coding everything myself, and I feared that I would not be able tocorrectly use the uicontrol code proided by Guide. In retrospect I probably onlysucceded in making work for myself, but I did gain a greater understandign andappreciation of MATLAB. Initially, I set all values except the precision errorand spacing to the default values in the code and allowed the user to change thevalue of both in the command line, using the same format as my first codingproject, the transmitted power of a pulley. All other values were defined in thecode in the appropriate point. The first two lines of code were therefore:

dx=input(’Input the desired spacing of points on the plate, in metres:’);

e=input(’Input the desired precision of the iterations:’);

I then coded a simple control panel GUI that allowed the user to change thevalues of the spacing and precision through sliders, that worked between thevalues of 0 and 0.1 metres in steps of 0.01 metres. I then changed the code to ageneral case and added sliders for all variables, at this point a user had to closethe control panel after inputting the data, which created a filled contour plot.I then learned how to code edit text boxes from the GUI Building guide.[2]

2

Figure 1: GUI Control Panel (sliders)

After doing so I replaced the sliders with edit boxes and added the ”Plot”button, which automatically closes the control panel and activates the resultsfigures. I attempted to add axes to the control panel but the data would notplot on the axes I defined in the figure.

3

Figure 2: GUI Control Panel (sliders)

3.2 Correcting Temperature Values

The main body of the program must define a matrix to contain the data cor-responding to the temperatures across the plate, and then put that matrixthrogh an iterative process to find the corrected values. Defining the matrixand special areas such as the plates and hole was fairly easy, since the codingtechnique necessary to do so was given in the project specification. The mainproblem was how to correct the temperatures in the matrix. I eventually de-vised a method that is inelegant from a coding standpoint, but nonethelesseffective, when I looked at it as a matrix function and not a coding prob-lem. I realised it was necessary to add the columns to the left and rightto each individual column, and to do the same with the rows. To explainin more detail, it would be best to use an example; a simple 4 × 4 matrix.

M1 =

1 2 3 14 0 5 16 7 9 11 1 1 1

Let us consider only the value of point (2,2) on the matrixl which is 0.

According to the theory of the model, the corrected value will be equal to:

(2 + 4 + 5 + 7) ÷ 4 = 4.5 (1)

We must carry out a series of matrix operators. First, define a matrix thatcontains the third column and any columns to the right of it, with the last twocolumns being composed of zeroes, this has the effect of shifting the matrix twospaces to the right.

M2 =

3 1 0 05 1 0 09 1 0 01 1 0 0

Then we add this new matrix to the original, and remove the last two

columns.

4

M3 =

4 39 115 82 2

This has the effect of adding adjacent columns together, the first column of

this matrix is the sum of the first and third columns of the original, and thesecond column of this new matrix is the sum of the second and fourth columnsof the original. Then we repeat the process but with rows, taking the lowertworows and moving them to the top of an empty 4 × 4 matrix, adding it to theoriginal and then removing the lower two rows, we find a new matrix where eachrow is the sum of the row above and below it.

M4 =

(7 9 12 25 1 6 2

)These matrices do not have the same dimensions, but by removing the first

and last rows from the M3, and the first and last columns of M4, we find two2 × 2 matrices that add to form:

M5 =

(18 1316 14

)Divide each value by four, and you have a matrix that gives the corrected

values of the central four values in the matrix, if this matrix is used to replacethe centre of the original matrix, this has the effect of replacting every valuethat is not part of the border of the matrix with it’s corrected value.

M6 =

1 2 3 14 4.5 3.25 16 4 3.5 11 1 1 1

The other values can be verified by inspection. This method is particularly

appropriate because it does not affect the values on the border of the matrix,some of which are constants (the plates). In my program this method is usedto correct the values of the temperatures, they are then placed back into thetemperature matrix and the temperature of the walls and hole are altered ac-cording to the demands of the problem. For example, a 1:10 scale of the defaultmodel is transformed from:

temp old =

100.0000 100.0000 100.0000 50.0000 50.0000 25.0000 25.0000100.0000 75.0000 75.0000 50.0000 50.0000 25.0000 25.0000100.0000 75.0000 34.6875 34.6875 34.6875 34.6875 25.000050.0000 50.0000 34.6875 34.6875 34.6875 34.6875 0.000050.0000 50.0000 50.0000 25.0000 25.0000 0.0000 0.0000

into:

temp new =

100.0000 100.0000 100.0000 52.4219 39.9219 33.6719 25.0000100.0000 87.5000 64.9219 52.4219 39.9219 33.6719 33.6719100.0000 64.9219 37.0508 37.0508 37.0508 37.0508 29.843852.4219 52.4219 37.0508 37.0508 37.0508 37.0508 0.000050.0000 52.4219 42.3438 32.2656 32.2656 0.0000 0.0000

The low resolution of this matrix means it is slightly misleading, for instance

the corner values and some wall values seem inconsisent with the rules laid outin the specification. It is important to note that these single points representwhat would be a large part of the array in the actual default case, but it doesserve as a moderately effective visual aid to the process.

5

3.3 Temperatures in the Hole

The toughest technical challenge in designing this GUI was averaging the tem-perature across the hole. Defining the section of the matrix that correspondsto the hole was relatively easy. I first defined it for the default case as the areabetween x arr=0.2 and 0.5, and between y arr=0.2 and 0.3, and after that wasable to convert it to the general case pretty easily. The code is designed toconvert the rows or columns of the matrix bordering the hole into vectors andtake an average of all the temperature values in these vectors, and then set thetemperature of the hole to that value. While using the default case, I noticedthat the vector ”l” which corresponds to the row immediately below the holewas an empty vector. This resulted in the temperature of the hole being set toa ”NaN” and a large area of the contour plot becoming white space, as shownin Figure 3. For some reason, the code could not read the values of the matrixbetween y arr=0.3 and 0.35. The problem seemingly fluctuated between labsessions- sometimes it would happen and sometime it would not, following theconversion to the general case the problem seems to have disappeared, but I donot know how or why.

Figure 3: Contour Plot due to NaN Error

6

4 Completed Script

The basic operation of the script is that the user uses the control panel GUIto enter the data into the script, then press the button marked ”Enter Data”,shown below in Figure , or alternatively simply press the ”Default Data” but-ton, thereby loading the default dataset. Pressing the ”Plot” button. Figureshows the control panel with the appropriate data input needed to produce thestandard contour plot as defined in the project description, this closes the con-trol panel and creates three additional Figures: a filled contour plot showing a2D representation of the data, a mesh plot showing a 3D representation and atext panel that gives the significant data of the model, as well as the numberof iterations required to complete the model. The colour axis of both graphsis fixed with a maximum of 100 Kelvin and a minimum of 0 Kelvin, allowingdifferent datasets to be easily compared and instantly recognise the differences.

A step-by-step walkthrough of the code is shown below:

function the_answer=heat_distribution(dx,e)

% define control panel

f1=figure(’Position’,[500,200,1050,600],’Name’,’Control Panel’);

hdx=uicontrol(f1,’Style’,’edit’,’Position’,[800,300,100,50],

’callback’,{@spacing_Callback});

he=uicontrol(f1,’Style’,’edit’,’Position’,[800,200,100,50],

’callback’,{@precision_Callback});

hpw=uicontrol(f1,’Style’,’edit’,’Position’,[20,200,100,50],

’callback’,{@platewidth_Callback});

hpl=uicontrol(f1,’Style’,’edit’,’Position’,[20,300,100,50],

’callback’,{@platelength_Callback});

hhw=uicontrol(f1,’Style’,’edit’,’Position’,[20,400,100,50],

’callback’,{@holewidth_Callback});

hhl=uicontrol(f1,’Style’,’edit’,’Position’,[20,500,100,50],

’callback’,{@holelength_Callback});

hhx=uicontrol(f1,’Style’,’edit’,’Position’,[400,400,100,50],

’callback’,{@holexcoordinate_Callback});

hhy=uicontrol(f1,’Style’,’edit’,’Position’,[400,500,100,50],

’callback’,{@holeycoordinate_Callback});

htemp1=uicontrol(f1,’Style’,’edit’,’Position’,[400,300,100,50],

’callback’,{@heatedtemp_Callback});

htemp2=uicontrol(f1,’Style’,’edit’,’Position’,[400,200,100,50],

’callback’,{@cooledtemp_Callback});

htemp1d=uicontrol(f1,’Style’,’edit’,’Position’,[800,400,100,50],

’callback’,{@heateddimensions_Callback});

htemp2d=uicontrol(f1,’Style’,’edit’,’Position’,[800,500,100,50],

’callback’,{@cooleddimensions_Callback});

henter=uicontrol(f1,’Style’,’pushbutton’,’String’,’Enter Data’,

’Position’,[20,100,100,50]);

hdefault=uicontrol(f1,’Style’,’pushbutton’,’String’,’Default Data’,

’Position’,[900,100,100,50],’callback’,{@defaultbutton_Callback});

hplot=uicontrol(f1,’Style’,’pushbutton’,’String’,’Plot’,

’Position’,[420,100,100,50],’callback’,{@plotbutton_Callback});

7

st1=uicontrol(f1,’Style’,’text’,’String’,’Spacing

(default 0.01 metres)’,’Position’,[900,300,100,40]);

st2=uicontrol(f1,’Style’,’text’,’String’,’Iteration Precision

(default 0.01)’,’Position’,[900,200,100,40]);

st3=uicontrol(f1,’Style’,’text’,’String’,’Plate Width (default 0.6 metres)’,

’Position’,[120,200,100,40]);

st4=uicontrol(f1,’Style’,’text’,’String’,’Plate Height (default 0.4 metres)’

,’Position’,[120,300,100,40]);

st5=uicontrol(f1,’Style’,’text’,’String’,’Hole Width (default 0.3 metres)’,

’Position’,[120,400,100,40]);

st6=uicontrol(f1,’Style’,’text’,’String’,’Hole Height (default 0.1 metres)’,

’Position’,[120,500,100,40]);

st7=uicontrol(f1,’Style’,’text’,’String’,’Hole Horizontal Position

(default 0.2 metres)’,’Position’,[500,400,100,40]);

st8=uicontrol(f1,’Style’,’text’,’String’,’Heated Temperature

(default 100 Kelvin)’,’Position’,[500,300,100,40]);

st9=uicontrol(f1,’Style’,’text’,’String’,’Cooled Temperature

(default 0 Kelvin)’,’Position’,[500,200,100,40]);

st10=uicontrol(f1,’Style’,’text’,’String’,’Heated Plate Dimensions

(default 0.2 metres)’,’Position’,[900,400,100,40]);

st11=uicontrol(f1,’Style’,’text’,’String’,’Cooled Plate Dimensions

(default 0.1 metres)’,’Position’,[900,500,100,40]);

st12=uicontrol(f1,’Style’,’text’,’String’,’Hole Vertical Position

(default 0.2 metres)’,’Position’,[500,500,100,40]);

This section began the function, created the figure of the control panel (f1)and defined the positions and sizes of the enter text boxes and attendant labels,and the push buttons that allow the data to be entered and plotted

%input non-default data

waitfor (hdx,’value’)

function spacing_Callback(hdx,event)

dx=str2double(get(hdx,’string’));

end

waitfor (hpw,’value’)

function platewidth_Callback(hpw,event)

pw=str2double(get(hpw,’string’));

end

waitfor (hpl,’value’)

function platelength_Callback(hpl,event)

pl=str2double(get(hpl,’string’));

end

waitfor (htemp1,’value’)

function heatedtemp_Callback(htemp1,event)

temp1=str2double(get(htemp1,’string’));

end

8

waitfor (htemp1d,’value’)

function heateddimensions_Callback(htemp1d,event)

temp1d=str2double(get(htemp1d,’string’));

end

waitfor (htemp2,’value’)

function cooledtemp_Callback(htemp2,event)

temp2=str2double(get(htemp2,’string’));

end

waitfor (htemp2d,’value’)

function cooleddimensions_Callback(htemp2d,event)

temp2d=str2double(get(htemp2d,’string’));

end

waitfor (hhw,’value’)

function holewidth_Callback(hhw,event)

hw=str2double(get(hhw,’string’));

end

waitfor (hhl,’value’)

function holelength_Callback(hhl,event)

hl=str2double(get(hhl,’string’));

end

waitfor (hhx,’value’)

function holexcoordinate_Callback(hhx,event)

hx=str2double(get(hhx,’string’));

end

waitfor (hhy,’value’)

function holeycoordinate_Callback(hhy,event)

hy=str2double(get(hhy,’string’));

end

waitfor (he,’value’)

function precision_Callback(he,event)

e=str2double(get(he,’string’));

end

% define default dataset

function defaultbutton_Callback(hdefault,event)

dx=0.01;

e=0.01;

pw=0.6;

pl=0.4;

hw=0.3;

hl=0.1;

hx=0.2;

hy=0.2;

9

temp1=100;

temp2=0;

temp1d=0.2;

temp2d=0.1;

end

waitfor(henter,’ButtonDownFcn’)

This section codes the behavior of the edit boxes, the ”waitfor” precedingeach nested function ensures that the code waits for the value of the editboxto change, after which the nested function extracts the value and the output ofeach edit box is assigned a handle, corresponding to a variable in the model.The control panel is designed so that each edit box is labelled appropriately andindicates which variable it corresponds to. The ”waitfor” also ensures that thelatest value is used if the values are changed, and prevents NaN being entered forany variable. It also codes the ”Default Data” button, it defines the default dataset and ensures it only activates if the ”Default Data” button is pressed. The”waitfor(henter,’ButtonDownFcn’)” ensures that the variables are not eneteredunless the ”Enter Data” button is pressed.

%produce_matrix

format short

x=0:dx:pw;

y=0:dx:pl;

nx = length(x);

ny = length(y);

[x_arr, y_arr] = meshgrid(x, y);

temp = zeros(ny,nx);

% set heated points

heated_top = find((x_arr <= temp1d) & (y_arr == 0));

heated_left = find((x_arr == 0) & (y_arr <=temp1d));

heated_points=union(heated_top,heated_left);

temp(heated_points)=temp1;

%set cooled points

cooled_bottom=find((x_arr>=pw-temp2d)&(x_arr<=pw)&(y_arr==pl));

cooled_right=find((x_arr==pw)&(y_arr>=pl-temp2d));

cooled_points=union(cooled_bottom,cooled_right);

temp(cooled_points)=temp2;

This are of code begins to construct the matrix representing temperature,creating an empty matrix of equal dimesnions to the plate and defining thelengths and temperatures of the constantly heated and cooled regions. It doesthis by using the variables pl (plate length), pw (plate width), dx (point spacing),temp1 (constant hot temperature), temp2 (constant cold temperature), temp1d(lengh of hot plate) and temp2d (length of cold plate). It is copied from orextrapolates on the code given in the project specification for this purpose. [3]

temp3=0.5*(temp1+temp2);

10

%set initial values

heat=find((x_arr>=dx)&(x_arr<=temp1d)&(y_arr>0)&(y_arr<=temp1d));

temp(heat)=temp1-0.5*temp3;

region_1a=find((x_arr>temp1d)&(x_arr<=2*temp1d)&(y_arr>0)&

(y_arr<=temp1d));

region_1b=find((x_arr>0)&(x_arr<=temp1d)&(y_arr>temp1d)&(y_arr<pl));

region_1=union(region_1a,region_1b);

temp(region_1)=temp3;

region_2a=find((x_arr<pw)&(x_arr>2*temp1d)&(y_arr>0)&

(y_arr<=temp1d));

region_2b=find((x_arr<2*temp1d)&(x_arr>temp1d)&(y_arr>temp1d)&

(y_arr<pl));

region_2=union(region_2a,region_2b);

temp(region_2)=temp2+0.5*temp3;

region_3=((x_arr<pw)&(x_arr>2*temp1d)&(y_arr<pl)&(y_arr>temp1d));

temp(region_3)=temp2+0.25*temp3;

cold=find((x_arr<pw)&(x_arr>=pw-temp2d)&(y_arr<pl)&(y_arr>=pl-temp2d));

temp(cold)=temp2+0.1*temp3;

This is the most vital part of the code, and one of the major potential weaklinks. This set of functions breaks the plate down into a series of ”regions”.Each region is assumed to have a constant and consistent intial temperaturethat is a function of temp1, temp2 and temp3, an average of the two. Theseregions are: ”heat”, the area immediatly bordere by the hot plate; ”cold”, theregion immediatly bordered by the cold plate; and regions 1, 2 and 3, whichmake up the intermediate area and become increasingly cold from top left tobottom right. This is a rough estimate and a huge simplification, and if thisassumption is incorrect, the whole code is invalidated, however, since the contourplot created from the default data closely resembles the model answer given inthe exercise, I believe it is a reasonable guess. As an example, in the defaultcase: region ”heat” is 75 degrees Kelvin, region 1 is 50, region 2 is 25, region 3is 12.5 and region ”cold” is 5.

%set walls

left_wall=find((x_arr==0)&(y_arr>temp1d)&(y_arr<pl));

temp(left_wall)=temp((x_arr==dx)&(y_arr>temp1d)&(y_arr<pl));

temp((x_arr==0)&(y_arr==pl))=temp((x_arr==0)&(y_arr==pl-dx));

right_wall=find((x_arr==pw)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp(right_wall)=temp((x_arr==pw-dx)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp((x_arr==0.6)&(y_arr==0))=temp((x_arr==pw)&(y_arr==dx));

floor=find((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl));

temp(floor)=temp((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl-dx));

roof=find((y_arr==0)&(x_arr>temp1d)&(x_arr<pw));

temp(roof)=temp((y_arr==dx)&(x_arr>temp1d)&(x_arr<pw));

% set hole

hole=find((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>=hy)&(y_arr<=hy+hl));

i=temp((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>hy)&(y_arr<=hy+dx));

it=mean(i);

j=temp((x_arr<hx)&(x_arr>=hx-dx)&(y_arr>=hy)&(y_arr<=hy+hl));

11

jt=mean(j);

k=temp((x_arr==pw-dx)&(y_arr>=hy-hl)&(y_arr<=hy));

kt=mean(k);

l=temp((x_arr>=hx)&(x_arr<=hx+hw)&(y_arr>hy+hl)&(y_arr<=hy+hl+dx));

lt=mean(l);

h=(it+jt+kt+lt)/4;

temp(hole)=h;

This section completes the initial assumptions of the plate temperatures.The first block of code define the temperatures of the walls as equal to thetemperatures of the points immediately adjacent to them in the plate proper,with the temperature of the two corners that are not part of the heating platesbeing defined as the temperature of the point immedately above (bottom left)or below (top right) them on the walls. The temperature of the hole was atechnical challenge, as previously mentioned, but has been mostly fixed, thesecond block defines the points that make up the hole, and defines the rows orcolumns bordering them. These four vectors: it, jt, kt and lt are all averaged,the averages are averaged to a single number h, which is then given as thetemperature of the hole. Figure 4 is an example of what a contour plot usingthis data would look like (default data).

Figure 4: Initial Temperature Contour Plot

12

% corrected

R=zeros(1,nx);

S=zeros(ny,1);

M=temp(:,3:nx);

N=[M S S];

T1=temp+N;

T1=T1(:,1:nx-2);

T1=T1(2:ny-1,:);

O=temp(3:ny,:);

P=[O;R;R];

T2=temp+P;

T2=T2(1:ny-2,:);

T2=T2(:,2:nx-1);

T=(T1+T2)/4;

centre=find((x_arr>0)&(x_arr<pw)&(y_arr>0)&(y_arr<pl));

temp(centre)=T;

temp_old=temp;

left_wall=find((x_arr==0)&(y_arr>temp1d)&(y_arr<pl));

temp_old(left_wall)=temp((x_arr==dx)&(y_arr>temp1d)&(y_arr<pl));

temp_old((x_arr==0)&(y_arr==pl))=temp((x_arr==0)&(y_arr==pl-dx));

right_wall=find((x_arr==pw)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp_old(right_wall)=temp((x_arr==pw-dx)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp_old((x_arr==0.6)&(y_arr==0))=temp((x_arr==pw)&(y_arr==dx));

floor=find((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl));

temp_old(floor)=temp((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl-dx));

roof=find((y_arr==0)&(x_arr>temp1d)&(x_arr<pw));

temp_old(roof)=temp((y_arr==dx)&(x_arr>temp1d)&(x_arr<pw));

hole=find((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>=hy)&(y_arr<=hy+hl));

i=temp_old((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>hy)&(y_arr<=hy+dx));

it=mean(i);

j=temp_old((x_arr<hx)&(x_arr>=hx-dx)&(y_arr>=hy)&(y_arr<=hy+hl));

jt=mean(j);

k=temp_old((x_arr==pw-dx)&(y_arr>=hy-hl)&(y_arr<=hy));

kt=mean(k);

l=temp_old((x_arr>=hx)&(x_arr<=hx+hw)&(y_arr>hy+hl)&(y_arr<=hy+hl+dx));

lt=mean(l);

h=(it+jt+kt+lt)/4;

temp_old(hole)=h;

%define temp_new

temp_new=zeroes(ny,nx);

heated_top = find((x_arr <= temp1d) & (y_arr == 0));

heated_left = find((x_arr == 0) & (y_arr <=temp1d));

heated_points=union(heated_top,heated_left);

temp_new(heated_points)=temp1;

cooled_bottom=find((x_arr>=pw-temp2d)&(x_arr<=pw)&(y_arr==pl));

cooled_right=find((x_arr==pw)&(y_arr>=pl-temp2d));

cooled_points=union(cooled_bottom,cooled_right);

temp_new(cooled_points)=temp2;

13

n=temp1-temp2;

ite=0;

This section of the code averages the temperatures in the ”center” of theplate, namely every area that is not the walls, the process by which the tem-peratures were averaged has been explored in detail in the previous section.After averaging the center, the handle of the matrix is changed to ”temp old”in preparation for the interative process, and then the walls and hole have theirtemperatures changed to accomodate their specific properties. It also definesa new matrix, ”temp new”, also in preparation for the iterative process, andalso provides it with the heated and cooled plates. It defines n, the number ofcontour lines in the final contour plot as the difference between the two tem-peratures, and gives the initial value of ”ite” as zero. ”ite” is the number ofiterations before the difference at each iteration is less than the precision level.

while abs(temp_old(2,2)-((temp_old(2,1)+temp_old(2,3)+

temp_old(3,2)+temp_old(1,2))/4))>e;

ite=ite+1;

R=zeros(1,nx);

S=zeros(ny,1);

M=temp_old(:,3:nx);

N=[M S S];

T1=temp_old+N;

T1=T1(:,1:nx-2);

T1=T1(2:ny-1,:);

O=temp_old(3:ny,:);

P=[O;R;R];

T2=temp_old+P;

T2=T2(1:ny-2,:);

T2=T2(:,2:nx-1);

T=(T1+T2)/4;

centre=find((x_arr>0)&(x_arr<pw)&(y_arr>0)&(y_arr<pl));

temp_new(centre)=T;

heated_top = find((x_arr <= temp1d) & (y_arr == 0));

heated_left = find((x_arr == 0) & (y_arr <=temp1d));

heated_points=union(heated_top,heated_left);

temp_new(heated_points)=temp1;

cooled_bottom=find((x_arr>=pw-temp2d)&(x_arr<=pw)&(y_arr==pl));

cooled_right=find((x_arr==pw)&(y_arr>=pl-temp2d));

cooled_points=union(cooled_bottom,cooled_right);

temp_new(cooled_points)=temp2;

left_wall=find((x_arr==0)&(y_arr>temp1d)&(y_arr<pl));

temp_new(left_wall)=temp((x_arr==dx)&(y_arr>temp1d)&(y_arr<pl));

temp_new((x_arr==0)&(y_arr==pl))=temp((x_arr==0)&(y_arr==pl-dx));

right_wall=find((x_arr==pw)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp_new(right_wall)=temp((x_arr==pw-dx)&(y_arr<pl-temp2d)&(y_arr>=dx));

temp_new((x_arr==0.6)&(y_arr==0))=temp((x_arr==pw)&(y_arr==dx));

floor=find((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl));

14

temp_new(floor)=temp((x_arr>0)&(x_arr<pw-temp2d)&(y_arr==pl-dx));

roof=find((y_arr==0)&(x_arr>temp1d)&(x_arr<pw));

temp_new(roof)=temp((y_arr==dx)&(x_arr>temp1d)&(x_arr<pw));

hole=find((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>=hy)&(y_arr<=hl+hy));

i=temp_new((x_arr>=hx)&(x_arr<=hw+hx)&(y_arr>hy)&(y_arr<=hy+dx));

it=mean(i);

j=temp_new((x_arr<hx)&(x_arr>=hx-dx)&(y_arr>=hy)&(y_arr<=hy+hl));

jt=mean(j);

k=temp_new((x_arr==pw-dx)&(y_arr>=hy-hl)&(y_arr<=hy));

kt=mean(k);

l=temp_new((x_arr>=hx)&(x_arr<=hx+hw)&(y_arr>hy+hl)&(y_arr<=hy+hl+dx));

lt=mean(l);

h=(it+jt+kt+lt)/4;

temp_new(hole)=h;

temp_old=temp_new;

end

A=transpose(temp_new);

Z=rot90(A);

This ”while” loop, based on one of the exercises done early in the courseto find the squre root of a number. Since it would be impossible to comparethe corresponding points in temp old and temp new, it instead compares theaboslute difference between the temperature of one point and the average of thefour surrounding points, which will be the corrected value of the temperatureof that point, repeating the loop until this absolute difference drops below therequired precision level. The point that was chose is point (2,2)- the pointimmediatly adjacent to the top right corner. With each iteration the value of”ite” is increased by one, allowing the program to count the iterations neededto complete the loop. The final matrix temp new is outputted from the loop, itis then transposed and rotated 90 degrees, as a contour plot of the raw data isa reflection about the horizontal axis of the desired plot as shown in Figure 5below.

15

Figure 5: Unmanipulated Temperature Contour Plot

function plotbutton_Callback(hplot,event)

close(f1)

end

f2=figure(’Position’,[0,100,1000,800],’Name’,’Contour Plot’);

ha=axes(’Units’,’Pixels’);

contourf(ha,Z,n);

caxis([0 100]);

colorbar;

axis off;

f3=figure(’Position’,[950,500,500,500],’Name’,’Important Data’);

st13=uicontrol(f3,’Style’,’text’,’String’,[’Spacing between adjacent points is ’,

num2str(dx),’ metres’],’Position’,[50 25 400 50],’FontSize’,12,’FontWeight’,’bold’);

st14=uicontrol(f3,’Style’,’text’,’String’,[’Iteration precision (error level) is ’,

num2str(e)],’Position’,[50 75 400 50],’FontSize’,12,’FontWeight’,’bold’);

st15=uicontrol(f3,’Style’,’text’,’String’,[’Plate dimensions are ’,num2str(pw),’ x ’,

num2str(pl),’ metres’],’Position’,[50 125 400 50],’FontSize’,12,’FontWeight’,’bold’);

st16=uicontrol(f3,’Style’,’text’,’String’,[’Hole dimensions are ’,num2str(hw),’ x ’,

num2str(hl),’ metres’],’Position’,[50 175 400 50],’FontSize’,12,’FontWeight’,’bold’);

st17=uicontrol(f3,’Style’,’text’,’String’,[’The top left corner of the hole is ’,

num2str(hx),’ to the right and ’,num2str(hy),’ metres below the top left corner of the plate’],’Position’,[50 225 400 50],’FontSize’,12,’FontWeight’,’bold’);

16

st18=uicontrol(f3,’Style’,’text’,’String’,[’The temperatures range from ’,num2str(temp2),’ to ’,

num2str(temp1),’ degrees Kelvin’],’Position’,[50 275 400 50],’FontSize’,12,’FontWeight’,’bold’);

st19=uicontrol(f3,’Style’,’text’,’String’,[’The heated plate measures ’,num2str(temp1d),

’ metres to a side’],’Position’,[50 325 400 50],’FontSize’,12,’FontWeight’,’bold’);

st20=uicontrol(f3,’Style’,’text’,’String’,[’The cooled plate measures ’,num2str(temp2d),,’ metres to a side’],’Position’,[50 375 400 50],’FontSize’,12,’FontWeight’,’bold’);

st21=uicontrol(f3,’Style’,’text’,’String’,[’The process is completed in ’,num2str(ite),

’ iterations’],’Position’,[50 425 400 50],’FontSize’,12,’FontWeight’,’bold’);

f4=figure(’Position’,[1400,100,500,500],’Name’,’Mesh Plot’);

graphwidth=(pw/dx)+1;

graphlength=(pl/dx)+1;

mesh(Z);

axis([1 graphwidth 1 graphlength]);

caxis([0 100]);

colorbar;

end

Finally, pressing the ”Plot” button on the control panel closes it, creatingthree new Figures: a filled contour plot, a text window detailing the variablesof the model and a mesh plot showing a 3D representation of the data. Theaxes of the meshplot are defined as the number of points that make up the x ory axis, calculated as the plate dimenosions divided by the spacing and with anadditional , as to prevent unwanted white space. The process is now complete.

Figure 6: Results of Program

Although I feel the code has been completed to a competent standard, thereare changes and additions that I would have made, and would like to make atsome point in the future, if I had more time and greater programming skill,those are laid out in the next two sections.

17

5 Discussion

I believe that my code was completed to a satisfactory standard and fulfils theproject specification, allowing a user to model the distribution of temperatureacross a plate, using either a default case or values of their own choosing forthe important variables. However, I recognise that it could still be improved.The scope is limited and does not allow for quick changes of the data. Itshould also be noted that if the spacing between the points on the plate issmall in comparison to the dimensions of the plate, then the code takes a greatdeal of time to complete and may possibly suffer an NaN error, but this israre. I could not find a way to cause the graph to output to an axes on thecontrol panel, meaning it is necessary to close the control panel and restartthe program if you wish to compare two separate data sets. The method ofcorrecting the temperature values is effective and relatively quick if the spacingand iteration precision of the data is not very small in comparison to the othervalues, but is not the most elegant solution from a coding standpoint, and relieson brute-forcing the mathematics rather than correcting each value one by one,such as in a Kernel program. The charts are clear, aesthetically pleasing andinformative, and use a broad range of the capabilities of MATLAB, displayingthe data graphically and verbally. In short, the finished project is competentlymade, and a useful tool, but it is not perfect and there is still room for furtherdevelopments.

6 Future Developments

I would like to add some animation to the GUI, to improve user friendliness andaesthetic appeal to the user, ideally I would like to show how the contour linesmove and change as the model becomes increasingly close to reality. A smallchange that would prevent NaN errors would be to cause the GUI to reject anyvalues of spacing lower than the values for the shortest plate dimension by afactor of 100. Another possible addition to the project that I did not have timeto develop, as the idea only occured shortly before the deadline, would be asingle scatter graph to show how the temperature varies along a diagonal lineacross the plate from the upper left to bottom right corners. Finally, I wouldhave liked to have included the graphs on the control panel, which change withthe data, allowing quick and easy comparison of myriad data sets, but ultimatelythis was a coding puzzle that I could not solve.

[1] [2] [3]

18

Bibliography

[1] MATLAB Help. About the simple programmatic gui, 2012.

[2] MATLAB Help. Gui building/component selection/gui controls and indica-tors/uicontrol, 2012.

[3] Dr Andrew Lawrie. Computer-based modelling: Matlab projects, 2014.

19