SlideShare a Scribd company logo
1 of 20
Download to read offline
Computer Based Modelling Project Report
Robert Tanner, University of Bristol
May 2, 2014
Contents
1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 Script Development . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1 GUI Design . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.2 Correcting Temperature Values . . . . . . . . . . . . . . . 4
3.3 Temperatures in the Hole . . . . . . . . . . . . . . . . . . 6
4 Completed Script . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
5 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 Future Developments . . . . . . . . . . . . . . . . . . . . . . . . . 18
1 Summary
Designing this GUI was extremely challenging, and one of the most difficult
projects I did this year. The project s[ecification was to design a GUI that
would model the distribution of temperature across a plate in accordance with
Laplace’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 immediatly
adjacent to it. The data used could either be a dataset specified by the user
or a default set. The code grew from the a program that used the default set
and 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 any
value and produce a series of charts to model this data. Designing a method to
correct the temperatures to the mean of the surrounding values in accordance
with Laplace’s equation was a diffcult puzzle that I eventually solved through a
series of complex matrix operations, in a method that produced corrected tem-
peratures quickly and efficiently, but only through a systematic series of lengthy
matrix operations. I also encountered a significant challenge in attempting to
code the behavior of the hole filled with conducting liquid in the centre of the
plate 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. Both
these problems were resolved however, and I was able to program a GUI that
allows the user to input important variables, whether of their own choosing or
a default case, and produce a set of clear and informative graphs that modelled
the temperature distribution. Ultimately this resulted in a code that was an ef-
fective tool for the purpose it was designed, but could potentially receive further
development to increase user friendliness, aesthetics or allow the comparison of
several data sets in quick succession.
1
2 Introduction
The purpose of this project was to write a MATLAB script that would model
the 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 the
bottom right, and had a hole containing a conductive liquid that ensured all
points in the hole are equal to the average of the temperatures surrounding the
hole. 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 the
plate, and from that carry out an interative process to calculate each corrected
temperature, repeating the process until the error of the iteration drops below
a set level. The corrected temperature of each point is the average of the points
surrounding it on the plate. The project specified that the program must make
it possible to change every significant variable of the model, but also allow for
a default case, with the plate measuring 0.6 by 0.4 metres and a spacing of 0.01
metres between each point, heated to 100 degrees Kelvin 0.2 metres along the
upper left corner and cooled to 0 degrees Kelvin 0.1 metres along the bottom
right, the hole measuring 0.3 by 0.1 metres, the upper left corner of the hole
being situated 0.2 metres horizontally and vertically from the upper left of the
plate. The default iteration error level is 0.01. Therefore, I created a script
based on the special case defined in the brief, and then altered it into a more
general format, and created a GUI to act as a control panel for data input, with
the option of using a default dataset. The development cycle of the project is
given 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 project
is entirely programmatic, and I therefore took a great deal of information from
the Simple Programmatic GUI guide.[1] I felt a greater degree of control over the
program by coding everything myself, and I feared that I would not be able to
correctly use the uicontrol code proided by Guide. In retrospect I probably only
succeded in making work for myself, but I did gain a greater understandign and
appreciation of MATLAB. Initially, I set all values except the precision error
and spacing to the default values in the code and allowed the user to change the
value of both in the command line, using the same format as my first coding
project, the transmitted power of a pulley. All other values were defined in the
code 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 the
values of the spacing and precision through sliders, that worked between the
values of 0 and 0.1 metres in steps of 0.01 metres. I then changed the code to a
general case and added sliders for all variables, at this point a user had to close
the 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 results
figures. I attempted to add axes to the control panel but the data would not
plot 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 matrix
throgh an iterative process to find the corrected values. Defining the matrix
and special areas such as the plates and hole was fairly easy, since the coding
technique necessary to do so was given in the project specification. The main
problem was how to correct the temperatures in the matrix. I eventually de-
vised a method that is inelegant from a coding standpoint, but nonetheless
effective, 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 right
to each individual column, and to do the same with the rows. To explain
in more detail, it would be best to use an example; a simple 4 × 4 matrix.
M1 =




1 2 3 1
4 0 5 1
6 7 9 1
1 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 that
contains the third column and any columns to the right of it, with the last two
columns being composed of zeroes, this has the effect of shifting the matrix two
spaces to the right.
M2 =




3 1 0 0
5 1 0 0
9 1 0 0
1 1 0 0




Then we add this new matrix to the original, and remove the last two
columns.
4
M3 =




4 3
9 1
15 8
2 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 the
second column of this new matrix is the sum of the second and fourth columns
of the original. Then we repeat the process but with rows, taking the lowertwo
rows and moving them to the top of an empty 4 × 4 matrix, adding it to the
original and then removing the lower two rows, we find a new matrix where each
row is the sum of the row above and below it.
M4 =
7 9 12 2
5 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 two
2 × 2 matrices that add to form:
M5 =
18 13
16 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 replace
the centre of the original matrix, this has the effect of replacting every value
that is not part of the border of the matrix with it’s corrected value.
M6 =




1 2 3 1
4 4.5 3.25 1
6 4 3.5 1
1 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 used
to correct the values of the temperatures, they are then placed back into the
temperature 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 default
model is transformed from:
temp old =






100.0000 100.0000 100.0000 50.0000 50.0000 25.0000 25.0000
100.0000 75.0000 75.0000 50.0000 50.0000 25.0000 25.0000
100.0000 75.0000 34.6875 34.6875 34.6875 34.6875 25.0000
50.0000 50.0000 34.6875 34.6875 34.6875 34.6875 0.0000
50.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.0000
100.0000 87.5000 64.9219 52.4219 39.9219 33.6719 33.6719
100.0000 64.9219 37.0508 37.0508 37.0508 37.0508 29.8438
52.4219 52.4219 37.0508 37.0508 37.0508 37.0508 0.0000
50.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 out
in the specification. It is important to note that these single points represent
what would be a large part of the array in the actual default case, but it does
serve 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 corresponds
to the hole was relatively easy. I first defined it for the default case as the area
between x arr=0.2 and 0.5, and between y arr=0.2 and 0.3, and after that was
able to convert it to the general case pretty easily. The code is designed to
convert the rows or columns of the matrix bordering the hole into vectors and
take an average of all the temperature values in these vectors, and then set the
temperature of the hole to that value. While using the default case, I noticed
that the vector ”l” which corresponds to the row immediately below the hole
was an empty vector. This resulted in the temperature of the hole being set to
a ”NaN” and a large area of the contour plot becoming white space, as shown
in Figure 3. For some reason, the code could not read the values of the matrix
between y arr=0.3 and 0.35. The problem seemingly fluctuated between lab
sessions- sometimes it would happen and sometime it would not, following the
conversion to the general case the problem seems to have disappeared, but I do
not 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 GUI
to 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. Figure
shows the control panel with the appropriate data input needed to produce the
standard contour plot as defined in the project description, this closes the con-
trol panel and creates three additional Figures: a filled contour plot showing a
2D representation of the data, a mesh plot showing a 3D representation and a
text panel that gives the significant data of the model, as well as the number
of iterations required to complete the model. The colour axis of both graphs
is fixed with a maximum of 100 Kelvin and a minimum of 0 Kelvin, allowing
different 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” preceding
each nested function ensures that the code waits for the value of the editbox
to change, after which the nested function extracts the value and the output of
each 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 and
indicates which variable it corresponds to. The ”waitfor” also ensures that the
latest value is used if the values are changed, and prevents NaN being entered for
any variable. It also codes the ”Default Data” button, it defines the default data
set and ensures it only activates if the ”Default Data” button is pressed. The
”waitfor(henter,’ButtonDownFcn’)” ensures that the variables are not enetered
unless 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 the
lengths and temperatures of the constantly heated and cooled regions. It does
this 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 or
extrapolates 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 weak
links. This set of functions breaks the plate down into a series of ”regions”.
Each region is assumed to have a constant and consistent intial temperature
that is a function of temp1, temp2 and temp3, an average of the two. These
regions are: ”heat”, the area immediatly bordere by the hot plate; ”cold”, the
region immediatly bordered by the cold plate; and regions 1, 2 and 3, which
make up the intermediate area and become increasingly cold from top left to
bottom right. This is a rough estimate and a huge simplification, and if this
assumption is incorrect, the whole code is invalidated, however, since the contour
plot created from the default data closely resembles the model answer given in
the exercise, I believe it is a reasonable guess. As an example, in the default
case: region ”heat” is 75 degrees Kelvin, region 1 is 50, region 2 is 25, region 3
is 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 the
temperatures 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 plates
being 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 a
technical challenge, as previously mentioned, but has been mostly fixed, the
second block defines the points that make up the hole, and defines the rows or
columns 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 the
temperature of the hole. Figure 4 is an example of what a contour plot using
this 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 the
plate, 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 their
temperatures changed to accomodate their specific properties. It also defines
a new matrix, ”temp new”, also in preparation for the iterative process, and
also provides it with the heated and cooled plates. It defines n, the number of
contour 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 of
iterations 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 course
to find the squre root of a number. Since it would be impossible to compare
the corresponding points in temp old and temp new, it instead compares the
aboslute difference between the temperature of one point and the average of the
four surrounding points, which will be the corrected value of the temperature
of that point, repeating the loop until this absolute difference drops below the
required precision level. The point that was chose is point (2,2)- the point
immediatly adjacent to the top right corner. With each iteration the value of
”ite” is increased by one, allowing the program to count the iterations needed
to complete the loop. The final matrix temp new is outputted from the loop, it
is then transposed and rotated 90 degrees, as a contour plot of the raw data is
a reflection about the horizontal axis of the desired plot as shown in Figure 5
below.
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 pla
16
st18=uicontrol(f3,’Style’,’text’,’String’,[’The temperatures range from ’,num2str(temp2),’
num2str(temp1),’ degrees Kelvin’],’Position’,[50 275 400 50],’FontSize’,12,’FontWeight’,’b
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),,’
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, creating
three new Figures: a filled contour plot, a text window detailing the variables
of the model and a mesh plot showing a 3D representation of the data. The
axes of the meshplot are defined as the number of points that make up the x or
y axis, calculated as the plate dimenosions divided by the spacing and with an
additional , 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, there
are changes and additions that I would have made, and would like to make at
some 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 the
project specification, allowing a user to model the distribution of temperature
across a plate, using either a default case or values of their own choosing for
the 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. It
should also be noted that if the spacing between the points on the plate is
small in comparison to the dimensions of the plate, then the code takes a great
deal of time to complete and may possibly suffer an NaN error, but this is
rare. I could not find a way to cause the graph to output to an axes on the
control panel, meaning it is necessary to close the control panel and restart
the program if you wish to compare two separate data sets. The method of
correcting the temperature values is effective and relatively quick if the spacing
and iteration precision of the data is not very small in comparison to the other
values, but is not the most elegant solution from a coding standpoint, and relies
on brute-forcing the mathematics rather than correcting each value one by one,
such as in a Kernel program. The charts are clear, aesthetically pleasing and
informative, and use a broad range of the capabilities of MATLAB, displaying
the data graphically and verbally. In short, the finished project is competently
made, and a useful tool, but it is not perfect and there is still room for further
developments.
6 Future Developments
I would like to add some animation to the GUI, to improve user friendliness and
aesthetic appeal to the user, ideally I would like to show how the contour lines
move and change as the model becomes increasingly close to reality. A small
change that would prevent NaN errors would be to cause the GUI to reject any
values of spacing lower than the values for the shortest plate dimension by a
factor of 100. Another possible addition to the project that I did not have time
to develop, as the idea only occured shortly before the deadline, would be a
single scatter graph to show how the temperature varies along a diagonal line
across the plate from the upper left to bottom right corners. Finally, I would
have liked to have included the graphs on the control panel, which change with
the data, allowing quick and easy comparison of myriad data sets, but ultimately
this 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

More Related Content

Similar to Robert_Tanner_Temperature_Distribution

Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0PMILebanonChapter
 
Proyecto parcial ii_grupo2.docx
Proyecto parcial ii_grupo2.docxProyecto parcial ii_grupo2.docx
Proyecto parcial ii_grupo2.docxLuisCuevaFlores
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Application of thermal error in machine tools based on Dynamic Bayesian Network
Application of thermal error in machine tools based on Dynamic Bayesian NetworkApplication of thermal error in machine tools based on Dynamic Bayesian Network
Application of thermal error in machine tools based on Dynamic Bayesian NetworkIJRES Journal
 
micro proj4.V2odt
micro proj4.V2odtmicro proj4.V2odt
micro proj4.V2odtBruno Diaz
 
Smith waterman algorithm parallelization
Smith waterman algorithm parallelizationSmith waterman algorithm parallelization
Smith waterman algorithm parallelizationMário Almeida
 
Curve fitting
Curve fittingCurve fitting
Curve fittingdusan4rs
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionFranck Pachot
 
Algorithm for computational problematic sit
Algorithm for computational problematic sitAlgorithm for computational problematic sit
Algorithm for computational problematic sitSaurabh846965
 
Linear programming models - U2.pptx
Linear programming models - U2.pptxLinear programming models - U2.pptx
Linear programming models - U2.pptxMariaBurgos55
 

Similar to Robert_Tanner_Temperature_Distribution (20)

Book
BookBook
Book
 
User rpl tut
User rpl tutUser rpl tut
User rpl tut
 
Project Report
Project ReportProject Report
Project Report
 
Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0Monte Carlo Simulation for project estimates v1.0
Monte Carlo Simulation for project estimates v1.0
 
Proyecto parcial ii_grupo2.docx
Proyecto parcial ii_grupo2.docxProyecto parcial ii_grupo2.docx
Proyecto parcial ii_grupo2.docx
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Application of thermal error in machine tools based on Dynamic Bayesian Network
Application of thermal error in machine tools based on Dynamic Bayesian NetworkApplication of thermal error in machine tools based on Dynamic Bayesian Network
Application of thermal error in machine tools based on Dynamic Bayesian Network
 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
 
GIS AUTOMATION
GIS AUTOMATIONGIS AUTOMATION
GIS AUTOMATION
 
Lecture 1 2
Lecture 1 2Lecture 1 2
Lecture 1 2
 
micro proj4.V2odt
micro proj4.V2odtmicro proj4.V2odt
micro proj4.V2odt
 
Mathcad
MathcadMathcad
Mathcad
 
Smith waterman algorithm parallelization
Smith waterman algorithm parallelizationSmith waterman algorithm parallelization
Smith waterman algorithm parallelization
 
Final Report
Final ReportFinal Report
Final Report
 
Curve fitting
Curve fittingCurve fitting
Curve fitting
 
Lecture 7.pptx
Lecture 7.pptxLecture 7.pptx
Lecture 7.pptx
 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
 
Algorithm for computational problematic sit
Algorithm for computational problematic sitAlgorithm for computational problematic sit
Algorithm for computational problematic sit
 
Linear programming models - U2.pptx
Linear programming models - U2.pptxLinear programming models - U2.pptx
Linear programming models - U2.pptx
 
Excel Training
Excel TrainingExcel Training
Excel Training
 

More from Robert Tanner (20)

Viva Voce
Viva VoceViva Voce
Viva Voce
 
r_tanner_mp
r_tanner_mpr_tanner_mp
r_tanner_mp
 
transcript_2015-6_rt13074
transcript_2015-6_rt13074transcript_2015-6_rt13074
transcript_2015-6_rt13074
 
Robert Tanner FEA CW2 (final)
Robert Tanner FEA CW2 (final)Robert Tanner FEA CW2 (final)
Robert Tanner FEA CW2 (final)
 
Excel+Matlab flow chart
Excel+Matlab flow chartExcel+Matlab flow chart
Excel+Matlab flow chart
 
transcript_2014-5_rt13074
transcript_2014-5_rt13074transcript_2014-5_rt13074
transcript_2014-5_rt13074
 
scholarship
scholarshipscholarship
scholarship
 
Alevel5
Alevel5Alevel5
Alevel5
 
Alevel1
Alevel1Alevel1
Alevel1
 
Alevel4
Alevel4Alevel4
Alevel4
 
Alevel3
Alevel3Alevel3
Alevel3
 
Alevel2
Alevel2Alevel2
Alevel2
 
yave
yaveyave
yave
 
robots
robotsrobots
robots
 
gcse2
gcse2gcse2
gcse2
 
leader
leaderleader
leader
 
gcse1
gcse1gcse1
gcse1
 
Lumen
LumenLumen
Lumen
 
yini
yiniyini
yini
 
YINI summary
YINI summaryYINI summary
YINI summary
 

Robert_Tanner_Temperature_Distribution

  • 1. Computer Based Modelling Project Report Robert Tanner, University of Bristol May 2, 2014
  • 2. Contents 1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 Script Development . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3.1 GUI Design . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3.2 Correcting Temperature Values . . . . . . . . . . . . . . . 4 3.3 Temperatures in the Hole . . . . . . . . . . . . . . . . . . 6 4 Completed Script . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 5 Discussion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 6 Future Developments . . . . . . . . . . . . . . . . . . . . . . . . . 18 1 Summary Designing this GUI was extremely challenging, and one of the most difficult projects I did this year. The project s[ecification was to design a GUI that would model the distribution of temperature across a plate in accordance with Laplace’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 immediatly adjacent to it. The data used could either be a dataset specified by the user or a default set. The code grew from the a program that used the default set and 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 any value and produce a series of charts to model this data. Designing a method to correct the temperatures to the mean of the surrounding values in accordance with Laplace’s equation was a diffcult puzzle that I eventually solved through a series of complex matrix operations, in a method that produced corrected tem- peratures quickly and efficiently, but only through a systematic series of lengthy matrix operations. I also encountered a significant challenge in attempting to code the behavior of the hole filled with conducting liquid in the centre of the plate 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. Both these problems were resolved however, and I was able to program a GUI that allows the user to input important variables, whether of their own choosing or a default case, and produce a set of clear and informative graphs that modelled the temperature distribution. Ultimately this resulted in a code that was an ef- fective tool for the purpose it was designed, but could potentially receive further development to increase user friendliness, aesthetics or allow the comparison of several data sets in quick succession. 1
  • 3. 2 Introduction The purpose of this project was to write a MATLAB script that would model the 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 the bottom right, and had a hole containing a conductive liquid that ensured all points in the hole are equal to the average of the temperatures surrounding the hole. 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 the plate, and from that carry out an interative process to calculate each corrected temperature, repeating the process until the error of the iteration drops below a set level. The corrected temperature of each point is the average of the points surrounding it on the plate. The project specified that the program must make it possible to change every significant variable of the model, but also allow for a default case, with the plate measuring 0.6 by 0.4 metres and a spacing of 0.01 metres between each point, heated to 100 degrees Kelvin 0.2 metres along the upper left corner and cooled to 0 degrees Kelvin 0.1 metres along the bottom right, the hole measuring 0.3 by 0.1 metres, the upper left corner of the hole being situated 0.2 metres horizontally and vertically from the upper left of the plate. The default iteration error level is 0.01. Therefore, I created a script based on the special case defined in the brief, and then altered it into a more general format, and created a GUI to act as a control panel for data input, with the option of using a default dataset. The development cycle of the project is given 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 project is entirely programmatic, and I therefore took a great deal of information from the Simple Programmatic GUI guide.[1] I felt a greater degree of control over the program by coding everything myself, and I feared that I would not be able to correctly use the uicontrol code proided by Guide. In retrospect I probably only succeded in making work for myself, but I did gain a greater understandign and appreciation of MATLAB. Initially, I set all values except the precision error and spacing to the default values in the code and allowed the user to change the value of both in the command line, using the same format as my first coding project, the transmitted power of a pulley. All other values were defined in the code 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 the values of the spacing and precision through sliders, that worked between the values of 0 and 0.1 metres in steps of 0.01 metres. I then changed the code to a general case and added sliders for all variables, at this point a user had to close the 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
  • 4. 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 results figures. I attempted to add axes to the control panel but the data would not plot on the axes I defined in the figure. 3
  • 5. 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 matrix throgh an iterative process to find the corrected values. Defining the matrix and special areas such as the plates and hole was fairly easy, since the coding technique necessary to do so was given in the project specification. The main problem was how to correct the temperatures in the matrix. I eventually de- vised a method that is inelegant from a coding standpoint, but nonetheless effective, 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 right to each individual column, and to do the same with the rows. To explain in more detail, it would be best to use an example; a simple 4 × 4 matrix. M1 =     1 2 3 1 4 0 5 1 6 7 9 1 1 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 that contains the third column and any columns to the right of it, with the last two columns being composed of zeroes, this has the effect of shifting the matrix two spaces to the right. M2 =     3 1 0 0 5 1 0 0 9 1 0 0 1 1 0 0     Then we add this new matrix to the original, and remove the last two columns. 4
  • 6. M3 =     4 3 9 1 15 8 2 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 the second column of this new matrix is the sum of the second and fourth columns of the original. Then we repeat the process but with rows, taking the lowertwo rows and moving them to the top of an empty 4 × 4 matrix, adding it to the original and then removing the lower two rows, we find a new matrix where each row is the sum of the row above and below it. M4 = 7 9 12 2 5 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 two 2 × 2 matrices that add to form: M5 = 18 13 16 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 replace the centre of the original matrix, this has the effect of replacting every value that is not part of the border of the matrix with it’s corrected value. M6 =     1 2 3 1 4 4.5 3.25 1 6 4 3.5 1 1 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 used to correct the values of the temperatures, they are then placed back into the temperature 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 default model is transformed from: temp old =       100.0000 100.0000 100.0000 50.0000 50.0000 25.0000 25.0000 100.0000 75.0000 75.0000 50.0000 50.0000 25.0000 25.0000 100.0000 75.0000 34.6875 34.6875 34.6875 34.6875 25.0000 50.0000 50.0000 34.6875 34.6875 34.6875 34.6875 0.0000 50.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.0000 100.0000 87.5000 64.9219 52.4219 39.9219 33.6719 33.6719 100.0000 64.9219 37.0508 37.0508 37.0508 37.0508 29.8438 52.4219 52.4219 37.0508 37.0508 37.0508 37.0508 0.0000 50.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 out in the specification. It is important to note that these single points represent what would be a large part of the array in the actual default case, but it does serve as a moderately effective visual aid to the process. 5
  • 7. 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 corresponds to the hole was relatively easy. I first defined it for the default case as the area between x arr=0.2 and 0.5, and between y arr=0.2 and 0.3, and after that was able to convert it to the general case pretty easily. The code is designed to convert the rows or columns of the matrix bordering the hole into vectors and take an average of all the temperature values in these vectors, and then set the temperature of the hole to that value. While using the default case, I noticed that the vector ”l” which corresponds to the row immediately below the hole was an empty vector. This resulted in the temperature of the hole being set to a ”NaN” and a large area of the contour plot becoming white space, as shown in Figure 3. For some reason, the code could not read the values of the matrix between y arr=0.3 and 0.35. The problem seemingly fluctuated between lab sessions- sometimes it would happen and sometime it would not, following the conversion to the general case the problem seems to have disappeared, but I do not know how or why. Figure 3: Contour Plot due to NaN Error 6
  • 8. 4 Completed Script The basic operation of the script is that the user uses the control panel GUI to 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. Figure shows the control panel with the appropriate data input needed to produce the standard contour plot as defined in the project description, this closes the con- trol panel and creates three additional Figures: a filled contour plot showing a 2D representation of the data, a mesh plot showing a 3D representation and a text panel that gives the significant data of the model, as well as the number of iterations required to complete the model. The colour axis of both graphs is fixed with a maximum of 100 Kelvin and a minimum of 0 Kelvin, allowing different 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
  • 9. 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
  • 10. 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
  • 11. 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” preceding each nested function ensures that the code waits for the value of the editbox to change, after which the nested function extracts the value and the output of each 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 and indicates which variable it corresponds to. The ”waitfor” also ensures that the latest value is used if the values are changed, and prevents NaN being entered for any variable. It also codes the ”Default Data” button, it defines the default data set and ensures it only activates if the ”Default Data” button is pressed. The ”waitfor(henter,’ButtonDownFcn’)” ensures that the variables are not enetered unless 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 the lengths and temperatures of the constantly heated and cooled regions. It does this 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 or extrapolates on the code given in the project specification for this purpose. [3] temp3=0.5*(temp1+temp2); 10
  • 12. %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 weak links. This set of functions breaks the plate down into a series of ”regions”. Each region is assumed to have a constant and consistent intial temperature that is a function of temp1, temp2 and temp3, an average of the two. These regions are: ”heat”, the area immediatly bordere by the hot plate; ”cold”, the region immediatly bordered by the cold plate; and regions 1, 2 and 3, which make up the intermediate area and become increasingly cold from top left to bottom right. This is a rough estimate and a huge simplification, and if this assumption is incorrect, the whole code is invalidated, however, since the contour plot created from the default data closely resembles the model answer given in the exercise, I believe it is a reasonable guess. As an example, in the default case: region ”heat” is 75 degrees Kelvin, region 1 is 50, region 2 is 25, region 3 is 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
  • 13. 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 the temperatures 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 plates being 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 a technical challenge, as previously mentioned, but has been mostly fixed, the second block defines the points that make up the hole, and defines the rows or columns 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 the temperature of the hole. Figure 4 is an example of what a contour plot using this data would look like (default data). Figure 4: Initial Temperature Contour Plot 12
  • 14. % 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
  • 15. n=temp1-temp2; ite=0; This section of the code averages the temperatures in the ”center” of the plate, 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 their temperatures changed to accomodate their specific properties. It also defines a new matrix, ”temp new”, also in preparation for the iterative process, and also provides it with the heated and cooled plates. It defines n, the number of contour 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 of iterations 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
  • 16. 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 course to find the squre root of a number. Since it would be impossible to compare the corresponding points in temp old and temp new, it instead compares the aboslute difference between the temperature of one point and the average of the four surrounding points, which will be the corrected value of the temperature of that point, repeating the loop until this absolute difference drops below the required precision level. The point that was chose is point (2,2)- the point immediatly adjacent to the top right corner. With each iteration the value of ”ite” is increased by one, allowing the program to count the iterations needed to complete the loop. The final matrix temp new is outputted from the loop, it is then transposed and rotated 90 degrees, as a contour plot of the raw data is a reflection about the horizontal axis of the desired plot as shown in Figure 5 below. 15
  • 17. 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 pla 16
  • 18. st18=uicontrol(f3,’Style’,’text’,’String’,[’The temperatures range from ’,num2str(temp2),’ num2str(temp1),’ degrees Kelvin’],’Position’,[50 275 400 50],’FontSize’,12,’FontWeight’,’b 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),,’ 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, creating three new Figures: a filled contour plot, a text window detailing the variables of the model and a mesh plot showing a 3D representation of the data. The axes of the meshplot are defined as the number of points that make up the x or y axis, calculated as the plate dimenosions divided by the spacing and with an additional , 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, there are changes and additions that I would have made, and would like to make at some point in the future, if I had more time and greater programming skill, those are laid out in the next two sections. 17
  • 19. 5 Discussion I believe that my code was completed to a satisfactory standard and fulfils the project specification, allowing a user to model the distribution of temperature across a plate, using either a default case or values of their own choosing for the 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. It should also be noted that if the spacing between the points on the plate is small in comparison to the dimensions of the plate, then the code takes a great deal of time to complete and may possibly suffer an NaN error, but this is rare. I could not find a way to cause the graph to output to an axes on the control panel, meaning it is necessary to close the control panel and restart the program if you wish to compare two separate data sets. The method of correcting the temperature values is effective and relatively quick if the spacing and iteration precision of the data is not very small in comparison to the other values, but is not the most elegant solution from a coding standpoint, and relies on brute-forcing the mathematics rather than correcting each value one by one, such as in a Kernel program. The charts are clear, aesthetically pleasing and informative, and use a broad range of the capabilities of MATLAB, displaying the data graphically and verbally. In short, the finished project is competently made, and a useful tool, but it is not perfect and there is still room for further developments. 6 Future Developments I would like to add some animation to the GUI, to improve user friendliness and aesthetic appeal to the user, ideally I would like to show how the contour lines move and change as the model becomes increasingly close to reality. A small change that would prevent NaN errors would be to cause the GUI to reject any values of spacing lower than the values for the shortest plate dimension by a factor of 100. Another possible addition to the project that I did not have time to develop, as the idea only occured shortly before the deadline, would be a single scatter graph to show how the temperature varies along a diagonal line across the plate from the upper left to bottom right corners. Finally, I would have liked to have included the graphs on the control panel, which change with the data, allowing quick and easy comparison of myriad data sets, but ultimately this was a coding puzzle that I could not solve. [1] [2] [3] 18
  • 20. 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