260 likes | 437 Views
Using MatLab and Excel to Solve Building Energy Simulation Problems. Jordan Clark jdclark@utexas.edu. Objectives. Intro to MatLab (if needed) Briefly discuss linear equations Solving systems of non-linear equations Using solve function Example 1
E N D
Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark jdclark@utexas.edu
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
Linear Equations in MATLAB • Solve system of equations: 3x +2y –z = 10 -x +3y +2z = 5 x -y -z = -1
Linear Equations in MATLAB • Pose as a matrix problem Ax=b Where A = x =, b =
Linear Equations in MATLAB • 3 ways to solve in MATLAB: • Inv • A^-1 • Left division
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\BNOTX=A/B or X=B\A • Each gives equivalent answer • Left division is more computationally efficient: less time to compute for large matrices
Non-Linear Equations in MATLAB • Distinguish between symbolic variables (ones you will solve for) and variables with numeric values (i.e. e, F12, etc.) • Define symbolic variables: • Can create multiple symbolic variables at once with syms command • Syntax: syms x y z a b c or symsT1 T2 T3
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
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)
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
Non-Linear Equations in MATLAB • Sometimes you will be working with both symbolic and numerical variables. • This is ok.
Non-Linear Equations in MATLAB Example 1: Find both T2 and Q for the steady state conduction problem, given k=4W/mK, wall thickness is 0.2m, T1=30, T3=40, and the area of the wall is 4m2 Q T3 T1 T2
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
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 T2 and Q for each hour • Will use a loop
Non-Linear Equations in MATLAB • Say we are given this data for the outside temperature, T3: • and we want to find T2and Q at each time
Non-Linear Equations in MATLAB Example 2 • 1st 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
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; % definesymbolicvariables 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
Non-Linear Equations in MATLAB Example 2: Qarray= -800 -800 -800 -800 -720 -480 -240 -160 0 240 400 720 880 Output: T2array= 25.0000 25.0000 25.0000 25.0000 25.5000 27.0000 28.5000 29.0000 30.0000 31.5000 32.5000 34.5000 35.5000
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
Non-Linear Equations in MATLAB Example 3 • Say we now are not given T3, but we have an outdoor convection coefficient, h, which changes with time, and an outdoor air temperature. • We want to find all T values and Q using the import wizard Q T1=30 T3 T2 Tout
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)
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 2nd column of “tdata” Tout =19 19 19 19 19 19 20 22 27 29 32 38 42
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
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)'
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
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 Matlabor about B.E.S. jdclark@utexas.edu • Questions now????