Function returning invalid outputs (NaN, INF, etc.)
Show older comments
For a project, I need to write a function that takes two inputs (TA, TB) that determines the temperature at each individual heat tile on the heat plate. The heat tiles are a 3x3 matrix (T1 T2 T3;T4 T5 T6;T7 T8 T9). Each tiles temp is the average of every adjacent tile so for example (T2=(T1 + T3 + T5)/3). TA and TB are the only two known quantities where TA is adjacent only to T1 and TB is adjacent only to T9. The function needs to get outputted into a 3x3 matrix.
My problem is that with the codes I've put in, I am getting a returned matrix of (NaN NaN NaN; NaN NaN NaN; NaN -Inf 33.333) when inputting (100,100) for (TA,TB). To my understanding inputting the same temperatures for TA,TB should result in all numbers in the outputted matrix to be equal, so in this case 100. Where am I going wrong?
Here is my code:
%% Equations for all Temperature Tiles
%T1=(TA + T2 + T4)/3 --> 3T1 - T2 - T4 = TA
%T2=(T1 + T3 + T5)/3 -->
%T3=(T2 + T6)/2
%T4=(T1 + T5 + T7)/3
%T5=(T2 + T4 + T6 + T8)/4
%T6=(T3 + T5 + T9)/3
%T7=(T4 + T8)/2
%T8=(T5 + T7 + T9)/3
%T9=(TB + T6 + T8)/3 --> 3T9 - T6 - T8 = TB
%% Function for Heat Plate
function plate = heatPlate(TA, TB)
%This function returns the temperature of each tile
%on a heat plate given the temperatures of two
%outside heating sources (TA,TB)
A=zeros(9,9); %create 9x9 matrix
A(1,1)=3;
A(1,[2 4])=-1
A(2,[1 3 5])=1/3;
A(3,[2 6])=1/2;
A(4,[1 5 7])=1/3;
A(5,[2 4 6 8])=1/4;
A(6,[3 5 9])=1/3;
A(7,[4 8])=1/2;
A(8,[5 7 9])=1/3;
A(9,[6 8])=-1;
A(9,9)=3; %update matrix in multiple ways
B=[TA 0 0 0 0 0 0 0 TB]'; %create 9x1 matrix
soln=A\B; %perform calculation
plate = [soln(1:3)';soln(4:6)';soln(7:9)']; %convert output to 3x3 matrix
end
Accepted Answer
More Answers (0)
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!