MATLAB Answers

Function returning invalid outputs (NaN, INF, etc.)

23 views (last 30 days)
Daniel Gollin
Daniel Gollin on 8 Jul 2020 at 14:58
Answered: Steven Lord on 8 Jul 2020 at 16:14
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

  0 Comments

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 8 Jul 2020 at 16:14
Your coefficient matrix A is not of full rank. It looks like you've translated from your equations to the rows of the matrix incorrectly. For example, your equation for T2 is T2=(T1 + T3 + T5)/3 but the way you've set up row 2 of A is:
A(2,[1 3 5])=1/3;
If we use Symbolic Math Toolbox to check that the equations the A and B matrices represent match your equations:
>> syms T [9 1]
>> A*T == B
3*T1 - T2 - T4 == 100
T1/3 + T3/3 + T5/3 == 0
T2/2 + T6/2 == 0
T1/3 + T5/3 + T7/3 == 0
T2/4 + T4/4 + T6/4 + T8/4 == 0
T3/3 + T5/3 + T9/3 == 0
T4/2 + T8/2 == 0
T5/3 + T7/3 + T9/3 == 0
3*T9 - T8 - T6 == 100
Nowhere does T2 appear in the second equation, or T3 in the third, or ... T8 in the eighth. When I add the appropriate coefficients along the diagonal of A it is now full rank and A\B returns an answer with no nonfinite entries.

  0 Comments

Sign in to comment.

More Answers (0)