650 likes | 796 Views
Engineering Problem Solving with Computers. MEPP Summer Residency August 2008. Outline for Session. About me Course overview Cases Excel programming and Optimization Matlab Primer. Intro. Jake Blanchard, Professor, Engineering Physics PhD in Nuclear Engineering, UCLA, 1988
E N D
Engineering Problem Solving with Computers MEPP Summer Residency August 2008
Outline for Session • About me • Course overview • Cases • Excel programming and Optimization • Matlab Primer
Intro • Jake Blanchard, Professor, Engineering Physics • PhD in Nuclear Engineering, UCLA, 1988 • Research: fusion technology, solid mechanics, nuclear batteries for MEMS, laser effects in metals • Born and raised in Southern California • married with two kids (11 and 9 yrs)
My Research Interests • Fusion Technology • Fission Reactor Fuels and Structures • Nuclear Microbatteries
Goal of EPSC • Students who have completed this course should have a broad understanding of: • Solution techniques for several equation types • Level of difficulty for various problem types • Appropriate tools for solving various problems • Advantages and disadvantages of using computers to solve engineering problems
Goal of This Session • I just want to get you started with the tools we use in my course • My theory is that some exposure now will benefit you in the Spring • We will repeat everything we do today when the course starts, though in more depth • Therefore, don’t worry if you don’t catch on to everything right away
Approach Used in EPSC • Problem-based using case studies • Assigned and student-identified projects • No exams
Textbooks • We will use a text I put together from two full texts: one Excel-based and one Matlab-based • You won’t all need this and no problems will be assigned from it • Buy it from Rose • Many other background books are available if you need additional support
Numerical Topics • Linear and nonlinear equations • Quadrature • Ordinary differential equations • initial value • boundary value • Optimization • Data Analysis • Databases
Prerequisites and Resources • Math skills: linear algebra (matrices and vectors) and ordinary differential equations (first and second order) • Computer skills: some programming is useful, but not necessary
Used These Before? • FORTRAN • C • C++ • Pascal • Basic • Java • Others? • EES • MathCAD • Matlab • Excel/Quattro • Mathematica • Maple • Others?
Tools • Microsoft Excel (97, 2000, 2003, etc.) • Matlab from Mathworks • there is a student version for ~ $100 • Any version beyond Matlab 4 will work fine, though a couple things got easier with version 6 and a few commands have changed • You can run it as “tethered” software from CAE if you are interested (www.cae.wisc.edu/tethered)
Cases • Broad coverage of engineering disciplines and topics • The more students can see relevance of cases, the better
Case Titles and Topics • Macros in Excel • Databases • Material Properties (choose your own for lesson) • Quadrature • Automobile suspension • Roots • natural frequencies of satellite boom • Linear Systems • fluid flow network
Case Titles and Topics • Initial Value • PSII target heating • Boundary Value • chip cooling • Optimization (Linear Programming/Constraints) • manufacturing
Case Titles and Topics • Optimization (Monte Carlo) • maximize profit with uncertain inputs • Curve Fits • radioactive decay
Hands-On Session • Optimization Problem in Excel (using the Solver) • Introduction to Matlab
First Download Files http://www.cae.wisc.edu/~blanchar/MEPP.html
Optimization of Water Distribution • Consider two bottling plants: Mexico City and Tepic • Consider three customers: one in Monterrey, one in Mazatlan, and the other in Acapulco • Question is: which plants should ship to which cities?
Best Guess • Use pencil and paper to make best guess of optimum distribution • Goal is to meet demand with minimum cost • Selling price is the same in each city
Sheet Setup What is total cost for your guess at the optimal distribution?
Using the Solver • Tools/Add-Ins…Solver • Tools/Solver • Take a look at this link for further information: http://econltsn.ilrt.bris.ac.uk/cheer/ch9_3/ch9_3p07.htm
Your Task • Set up the Solver, with all appropriate constraints, to optimize this problem
Next Scenario • What if the production cost in Tepic drops to 0.45 $/gal?
Next Scenario • What is optimum if demand increases in each city by 50%? Put Tepic back at 0.6 $/gal on the production cost.
Adding a “Run” Button • Excel has a built-in macro language called Visual Basic for Applications (VBA) • It can be used to create user interfaces • We can use it to add a button for running the Solver
Using Macros • Macros are written in a Basic-like language called Visual Basic for Applications • Excel comes with a separate macro editor • To create or edit a macro, go to Tools/Macro/Visual Basic Editor • To add a new module go to Insert/Module
The Macro Sub runsolver() SolverSolve End Sub
Creating the Button • Go to View/Toolbars/Forms • From this Toolbar, click on the button (row 2, column 2) and then trace out a button on a spreadsheet • Assign the “runsolver” macro to the button • Now click the button • Note that you may have to add a “Reference” to the Solver under Tools in the VBA Editor
User-Defined Functions • We can also create user-defined functions in VBA • These can be called from cells, just like any built-in function
Creating a Function • Suppose we want to create an Excel function that takes a temperature in Celsius and converts to Fahrenheit • We would type the following in a module: Function ctof(temp) ctof = 9 / 5 * temp + 32 End Function
Using the function • Then you can go to the spreadsheet and type =ctof(100) • Or, you can put the value of “100” into cell A1 and then type =ctof(A1) into some other cell • In fact, this function can be used just as any built-in Excel function can be used
Exercise • Copy this function and create ftoc for converting from fahrenheit to celsius
The Macro Language • Operators: +, -, *, /, ^, Mod • Comparison: =, <, >, <=, >=, <> • Logical Operators: And, Eqv, Imp, Not, Or, Xor • Intrinsic Functions: Abs, Cos, Sin, Tan, Atn (arc tangent), Exp, Log (natural), Sgn, Sqr (square root), Rnd (random number)
Flow Control If condition Then statements Else statements End If If x=0 Then f=1 Else f=sin(x)/x End If
Flow Control For counter=start To end statements Next For i=1 To 100 sum=sum+i Next
Flow Control Do Until condition statements Loop i=1 x=1 Do Until i=50 x=x*i i=i+1 Loop
Flow Control Do While condition statements Loop i=1 x=1 Do While i<50 x=x*i i=i+1 Loop
Example • Write an Excel function that calculates the factorial of some number Z • Factorial is Z*(Z-1)*(Z-2)*…*3*2*1
My solution Function fact(Z) x = 1 ans = 1 Do Until x = Z ans = ans * x x = x + 1 Loop fact = ans End Function
Another Solution Function fact(Z) ans = 1 For i = 1 To Z ans = ans * i Next fact = ans End Function
Exercise • Write an Excel function that calculates the sum of the first N cubes and test it • That is: 13+23+33+…+N3 • What is the result for N=20?
Matlab • Matlab began as a linear algebra package • It’s grown to be a general purpose equation solver • Strengths: robust routines, excellent performance • Weaknesses: more difficult user interface