Presentation on theme: "Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark"— Presentation transcript:
Using MatLab and Excel to Solve Building Energy Simulation Problems Jordan Clark firstname.lastname@example.org
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
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\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
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
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 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
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
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
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
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
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 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
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
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
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)'
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. email@example.com Questions now????