Download presentation

Presentation is loading. Please wait.

Published byKian Evett Modified over 3 years ago

1
Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark jdclark@utexas.edu

2
Objectives Intro to MatLab (if needed) Briefly discuss linear equations Solving systems of non-linear equations – Using solve function Example 1 – Using loops to solve for multiple time steps Example 2 – Importing data from Excel Example 3

3
Linear Equations in MATLAB Solve system of equations: 3x +2y –z = 10 -x +3y +2z = 5 x -y -z = -1

4
Linear Equations in MATLAB

5
3 ways to solve in MATLAB: – Inv – A^-1 – Left division

6
Linear Equations in MATLAB For all three, first define matrices: A= [3 2 -1; -1 3 2; 1 -1 -1]; B= [10; 5; -1]; Can be solved by any of the following syntaxes: X=inv(A)*B X=A^-1*B X=A\B NOT X=A/B or X=B\A Each gives equivalent answer Left division is more computationally efficient: less time to compute for large matrices

7
Non-Linear Equations in MATLAB Distinguish between symbolic variables (ones you will solve for) and variables with numeric values (i.e. , F 12, etc.) Define symbolic variables: – Can create multiple symbolic variables at once with syms command – Syntax: syms x y z a b c or syms T1 T2 T3

8
Non-Linear Equations in MATLAB Say we have defined symbolic variable x and want to solve an equation for x. With symbolic variables defined, create equation using syntax: E1=x-3 Notice your equation must be in the form f(x)=0 If it is not, rearrange it so it is

9
Non-Linear Equations in MATLAB We can now use solve function to find x: If we just write solve (E1) we will get ans = 3 If we want to redefine x as the solution, we can input x= solve(E1)

10
Non-Linear Equations in MATLAB For systems of equations, process is similar. – Define variables using syms – Write multiple homogeneous equations e.g. E1= x + y + z +2 E2= 3*x -2*y + z E3= -x + y -4*z +6 – Then use [x,y,z]=solve(E1, E2, E3) – Variables must be listed in alphabetical order on left side because right side outputs answers in alphabetical order of symbolic variables

11
Non-Linear Equations in MATLAB Sometimes you will be working with both symbolic and numerical variables. This is ok.

12
Non-Linear Equations in MATLAB Example 1: Find both T 2 and Q for the steady state conduction problem, given k=4W/mK, wall thickness is 0.2m, T 1 =30, T 3 =40, and the area of the wall is 4m 2 T1T1 T2T2 T3T3 Q

13
Non-Linear Equations in MATLAB Example 1 Code: k=4; %W/mK A=4; %m^2 L=0.1; %m T1=30; %degrees C T3=40; syms Q T2; E1= k*A/L*(T3-T2)-Q; E2= k*A/L*(T2-T1)-Q; [Q, T2]=solve(E1, E2) Output: Q = 800 T2 = 35

14
Non-Linear Equations in MATLAB Example 2: Using a loop to solve for multiple time steps Now suppose the temperature of the outside wall changes every hour, in discrete jumps (steady state assumption can be used for each hour) We need an efficient means of calculating T 2 and Q for each hour Will use a loop

15
Non-Linear Equations in MATLAB Say we are given this data for the outside temperature, T 3 : and we want to find T 2 and Q at each time TimeT3 12 midnight20 1 2 3 421 524 627 728 830 933 1035 1139 noon41

16
Non-Linear Equations in MATLAB Example 2 1 st create an array with the temperatures: T3=[20 20 20 20 21 24 27 28 30 33 35 39 41] Then loop through each value of T3 to calculate a corresponding value of Q and T2, and store these values in a Q and T2 array

17
Non-Linear Equations in MATLAB Example 2 code: k=4; %W/mK A=4; %m^2 L=0.1; %m T1=30; %degrees C T3=[20 20 20 20 21 24 27 28 30 33 35 39 41]; %input temperature array syms Q T2; % define symbolic variables for i=1:13 % tells the program to loop through 13 iterations % (corresponding to 13 hours for which we have data) E1= k*A/L*(T3(i)-T2)-Q; %notice an individual value of the data is called for T3 E2= k*A/L*(T2-T1)-Q; %by using the index "i" in T3(i) [Qarray(i), T2array(i)]=solve(E1, E2) %we save our data in a separate array, % which is appended automatically each iteration end T2array=double(T2array) %when doing symbolic math, answer is often returned in fractional form. %to convert it to decimal form, use the function "double", as in double precision

18
Non-Linear Equations in MATLAB Example 2: Qarray= -800 -720 -480 -240 -160 0 240 400 720 880 Output: T2array= 25.0000 25.5000 27.0000 28.5000 29.0000 30.0000 31.5000 32.5000 34.5000 35.5000

19
Non-Linear Equations in MATLAB Example 3: Importing Data from Excel Sometimes we have a lot of data and cannot input it by hand, e.g. TMY data There are a few ways to import data from Excel – Cut and paste – Use import wizard by selecting File > Import Data or Use function uiimport When using the wizard, make sure the file is saved in your working directory

20
Non-Linear Equations in MATLAB Example 3 Say we now are not given T 3, but we have an outdoor convection coefficient, h, which changes with time, and an outdoor air temperature. T 1 =30 T2T2 T3T3 Q T out We want to find all T values and Q using the import wizard

21
Non-Linear Equations in MATLAB Example 3: Importing Data from Excel Here is the data we have, in a worksheet by itself: If we just use the wizard, a 13x3 matrix is created, with the name of the Excel file (I called mine tdata) Time (h)Tout (C)h (W/m^2-K) 0191 1 1.1 2190.9 3191 4 0.9 5191.1 6201.5 7221.7 8271.3 9291 10321.2 11381 12421.5

22
Non-Linear Equations in MATLAB Example 3: Importing Data from Excel We can make separate arrays if we like for each variable by using commands such as >> Tout=tdata (:,2) %which says the array “Tout” is the 2 nd column of “tdata” Tout =19 19 20 22 27 29 32 38 42

23
Non-Linear Equations in MATLAB Example 3: Importing Data from Excel Once we identify the h values in a similar way, we can solve our problem Code on next slide

24
Non-Linear Equations in MATLAB Example 3 Code k=4; %W/mK A=4; %m^2 L=0.1; %m T1=30; %degrees C syms Q T2 T3; for i=1:13 E1= k*A/L*(T3-T2)-Q; E2= k*A/L*(T2-T1)-Q; %we now have 3 eqns + 3 unknowns E3= h(i)*A*(Tout(i)-T3)-Q; %notice indices on h and Tout [Qarray(i), T2array(i), T3array(i)]=solve(E1, E2, E3) end Qarray=double(Qarray)' T2array=double(T2array)' T3array=double(T3array)'

25
Non-Linear Equations in MATLAB Example 3 Output Qarray = -41.9048 -45.8768 -37.8947 -41.9048 -37.8947 -45.8768 -55.8140 -50.1382 -14.6479 -3.8095 9.0566 30.4762 66.9767 T2array = 29.7381 29.7133 29.7632 29.7381 29.7632 29.7133 29.6512 29.6866 29.9085 29.9762 30.0566 30.1905 30.4186 T3array = 29.4762 29.4265 29.5263 29.4762 29.5263 29.4265 29.3023 29.3733 29.8169 29.9524 30.1132 30.3810 30.8372

26
Conclusion Should be everything you need to finish homework Additional help (2 equation model form the class) on the course website, handouts section Please contact me with questions about Matlab or about B.E.S. jdclark@utexas.edu Questions now????

Similar presentations

Presentation is loading. Please wait....

OK

Using Matrices to Solve a 3-Variable System

Using Matrices to Solve a 3-Variable System

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google