0% found this document useful (0 votes)
66 views27 pages

Lecture 12 Spreadsheets Pt2

The document discusses using spreadsheets for engineering applications, including using the Solver tool to solve simultaneous equations and perform linear and non-linear regression analysis. It also covers creating macros and programming in Visual Basic for Applications (VBA) to automate spreadsheet tasks. Examples are provided for solving circuit analysis problems with Solver and performing non-linear regression to model thermal response data.

Uploaded by

Mani Sekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views27 pages

Lecture 12 Spreadsheets Pt2

The document discusses using spreadsheets for engineering applications, including using the Solver tool to solve simultaneous equations and perform linear and non-linear regression analysis. It also covers creating macros and programming in Visual Basic for Applications (VBA) to automate spreadsheet tasks. Examples are provided for solving circuit analysis problems with Solver and performing non-linear regression to model thermal response data.

Uploaded by

Mani Sekar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Lecture 12: Spreadsheets

for Engineering
Applications - part 2

BJ Furman
14NOV2011
The Plan for Today
 Solver
 Review
 Non-linear regression analysis
 Aerospace engineering computation using
a spreadsheet
 Macros and VBA programming
Learning Objectives
 Use Solver to solve mathematical
equations
 Apply Solver to perform regression
(linear and non-linear)
 Explain the utility of Visual Basic for
Applications (VBA)
 Create a VBA macro to automate tasks
Excel Solver
 Add-in tool comprised of optimization
algorithms
 Goal: minimize or maximize an objective
function subject to constraints by changing a
set of parameters that the objective function
depends upon
 Can be used to find roots, solve
simultaneous equations, solve problems
requiring iteration, etc.
Activating Solver - Excel 2003

Make sure Solver Add-in is checked!


Activating Solver - Excel 2007

Office Button
Solving Simultaneous Equations
i2
i1
 Circuit analysis R2

(EE 98) +V

R3
R1
i3
 Find the currents

i1  i2  i3 A
Kirchhoff’s Current Law at A i1  i2  i3  0
i3 R1  V 0i1  0i2  i3 R1  V
Kirchhoff’s Voltage Law
i2 R2  i2 R3  V across R1 and R3 0i1  R2  R3 i2  0i3  V

Equations in matrix form

1 1  1  i1   0  Could solve by matrix inversion:


0 0 R1  i2   V  [ i ] = [A]-1[V], but let’s use the
     Solver instead.
0 R2  R3  0  i3  V 
Steps for Solving Simultaneous Equations
With Solver
 Steps
1. Define constants and variables. Put guesses in for the
variables.
2. Express m equations in n variables (and constants) as:
fi (xj)= 0, where i =1 to m, and j = 1 to n
3. Form the equation: y = ∑ fi2

4. Use the Solver, and change the xj values to drive y (the


Target Cell) to be zero
Example: Excel_pt2.xls
Circuit Analysis Example with Solver
1. Define constants and variables.
Add guesses for variables
Note: named ranges
2. Express equations in the
variables and constants
3. Form the equations: y = ∑ fi2
4. Solver, and change the
variables to drive y (the Target
Cell) to 0

On to Regression Analysis 
Adding Names to Ranges
2003: Insert / Name / Create / Left column
Highlight names and values 2007: Formulas / Defined Names tab / Create
from Selection / Left column

Result

Back
Regression Analysis
 Coined by biologist Francis Galton (1822-1911)
 Searching for a mathematical law describing the
tendency of offspring’s characteristics to revert
(regress) back to the average of ancestors its
ancestors
 Statistical method to investigate the relationship
between dependent and independent variables
 Fit a mathematical model to a set of data
 Ex. “best-fit” straight line (trendline) through data points from
a phenomena that is thought to be of the form: y=mx + b
 Find the coefficients of the model equation that
minimizes the sum-of-squared error (SSE) between
the actual dependent variable values and those
predicted by the model
Linear Regression
Least Squares Curve Fit
 Linear
regression 14

12 y = 1.1693x
 Where the 2
R = 0.9538
coefficients of 10

the model are 8

linear Y 6

Examples 2 error
y = mx + b 0
y = a + b log(x) 0 2 4 6 8 10 12
y = a + bX + cX2 X
Non-linear Regression
Temperature Response
 Non-linear
regression
120

error
 Where the 100

coefficients of
the model are 80

non-linear Temperature, °C

60

Example
40

y = Ae -t/t
+C
Which coefficient is 20

“non-linear”?
0
0 2 4 6 8 10 12
Time, Sec.
Regression Analysis Procedure
 Steps
1. Select a regression model (e.g., y=mx+b)
2. Enter the data set (dependent and independent variable values) and
initial guesses for the regression model coefficients
3. Calculate the predicted dependent values using the regression model
and the independent variable(s)
4. Calculate the 'error' values (actual-predicted)
5. Calculate the squared errors
6. Calculate the sum of the squared errors (SSE)
7. Use Solver to minimize the SSE (the Target Cell, and select 'Min') by
changing the value of the coefficients (m and b)
8. Test your result by: 1) calculating the coefficient of determination (R2), 2)
plotting your model curve to the data, 3) plotting Ypred vs. Yi, and
4) plotting the deviations (Yi - Ypred).
SSE
R2  1  where TSS is the Total Sum of Squares
TSS
N

 y  y  where y is the average of the (actual) dependent variable in the data set
2
TSS  i
1
and the yi are the values of the actual dependent variable
Non-linear Regression Example
 Thermal step response
 Expose an object at uniform temperature to a
step change in surrounding temperature
 Ex: Lobster put into a pot of boiling water
 thermocouple at the center of the lobster
 measure lobster temperature as function of time

 Use the data to determine parameters of a


first order model: Heat in  change in internal energy
Assumed Solution dT
hA(T  T )  mC
T (t )  T  (T0  T )e  t /  dt
dT mC
  T  T where  
Example: Excel_pt2.xls dt hA
Spreadsheet for Non-linear Regression

Model curve

Example: Excel_pt2.xls
Plot of Ypred vs Yi
A good fit: Ypred vs. Yi
120
• Data close to the
diagonal line,
100
Yi vs. Yi
(but will depend on
scatter in the data) 80

• No discernable
Ypred , °C

pattern in the 60

data points around


the diagonal axis 40

Yi vs. Yi
20

0
0 20 40 60 80 100 120
Measured Yi, °C
Deviation Plot
A good fit:
Deviation Plot
• Yi-Ypred small
(but will depend on 5

scatter in the data) 4

• No discernable 3

pattern in the 2

data points around 1


Yi - Ypred

the horizontal axis 0


0 2 4 6 8 10 12
-1

-2

-3

-4

-5

-6
Time, Sec.
Excel, Macros, and VBA
 Macro: a set of recorded key strokes or
program written in Visual Basic (VB) to
automate tasks
 Visual Basic for Applications (VBA)
 An implementation of VB integrated into MS
Office applications
 Enables user to write VB code to automate tasks
and much more.
Security and Working With Macros
 Be careful with macro enabled files!
 Excel 2007
 .xlsx - macro dis-abled workbooks (default)
 .xlsm - macro enabled workbooks

 You can write and work with a macro in a .xlsx file,

but you can only save the macro to a .xlsm file.


 You may need to modify Macro Settings
 Office button | Excel Options | Trust Center Settings |
Macro Settings | Disable all macros with notification
 Excel 2003
 Set security level to ‘medium’
Accessing Macros in Excel 2003
 Tools / Macro
Accessing Macros in Excel 2007
 View / Macro
Recording Macros
 Excel 2007
 View / Macros / Macros (menu) / Record
Macro
 Excel 2003
 Tools / Macro / Record New Macro
 Ex. lbf to N conversion
lbf to N Conversion Macro
VB Editor
Review
References
 Larsen, R. W. (2009). Engineering with Excel, Pearson
Prentice Hall, New Jersey. ISBN 0-13-601775-4
 Engineering with Excel companion website:
http://www.chbe.montana.edu/excel/EngExcel3.htm.
Visited 25OCT2009.
 First-Order System: Transient Response of a
Thermocouple to a Step Temperature Change. [Available
on-line].
http://www.colorado.edu/MCEN/Measlab/background1st
order.pdf
. Visted 24APR2010.

You might also like