Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 122 / CS123 Engineering Computation Lab Lab 3 Bruce Char Department of Computer Science Drexel University Summer 2011 ©By the author. All rights reserved.

Similar presentations


Presentation on theme: "CS 122 / CS123 Engineering Computation Lab Lab 3 Bruce Char Department of Computer Science Drexel University Summer 2011 ©By the author. All rights reserved."— Presentation transcript:

1 CS 122 / CS123 Engineering Computation Lab Lab 3 Bruce Char Department of Computer Science Drexel University Summer 2011 ©By the author. All rights reserved. Permission is given to CS122 Summer 2011 staff and students to use and reproduce these notes for their own use.

2 10/22/09 Review of Lab 2 Cycle – cs122 Major Lab 2 concepts to remember – Use of user defined functions to create plots of boxes – Introduction to Maple’s table and for loop features

3 10/22/09 Review of Lab 2 Cycle – cs122 drawBoxB function syntax: – drawBoxB:=(width, height, xlo, ylo,c)  display([line([xlo,ylo],[xlo+width,ylo],color=c), line([xlo+width,ylo],[xlo+width,ylo+height],color=c), line([xlo,ylo],[xlo,ylo+height],color=c), line([xlo,ylo+height],[xlo+width,ylo+height],color=c)], axes=none, scaling=constrained); – Will be using in Lab 4

4 10/22/09 Review of Lab 2 Cycle – cs122 Maple table and for loop usage in the chemical reaction problem – Atab:=table(); – indexTab:=table(); – for i from 1 to 50 do indexTab[i] := i; # note use of for loop counter I Atab[i] := ………..; Etc. – end do; – # remember to convert tables to lists before using in plot functions – # since these functions expect a list type parameter

5 10/22/09 Review of Lab 2 Cycle – cs123 Maple procedures – Procname := proc(input parameters) local variables only used within proc Body of proc Return “result to be returned from proc” – end proc; – Procname(input parameters) # invoke the procedure GUI user interface creation – Use Maple’s Components pallette to drag sliders, text objects, buttons and plot areas onto work sheet – Then must configure and program components as appropriate

6 Administrative Notes Proficiency exam scheduled for Monday, 8/22. Overview and logistics to be communicated shortly Will also present details in lab 4 Special Accommodations If you are eligible for extended time for the proficiency exam, please provide a copy of the form to your instructor Wednesday CS122 sessions will be held in Korman 110 from now on.

7 Quiz 3 Activities Quiz 3 will be released on Tuesday (8/9) at 9 AM – Deadline: Friday (8/12) at 5 PM Pre-lab quizlet for Lab 4 – Released on Thursday (8/11) at 9 AM through Monday (8/15) at 3 PM

8 10/22/09 Lab 3 Overview – cs122 Based on materials from Chapter 14 and 15 readings – Chapter 14 – More on repetition and looping “while” loop – conditional looping + relational operators Combining “for” and “while” loops Dealing with runaway (infinite) “while” loops – Chapter 14 – Conditional execution Choosing alternatives – if.. then.. else.. elif constructs

9 10/22/09 Lab 3 Overview – cs122 Lab 3 outline – Part 1 – Analysis of Blammo trajectory options 1.1 - Applying a “for” loop to calculate Blammo’s distance for a variety of firing angles – starter script provided 1.2 – Finding the smallest firing angle for Blammo to achieve a particular flight distance 1.3 – Find all firing angles that enables Blammo to hit target 1.4 – Plot a movie (animation) of all trajectories for different firing angles

10 Lab 3 Overview – cs123 Based on materials from Chapters 19 and 20 Chapter 19 – Calculus and Optimization Develop an objective function for the minimum surface area of a can and find optimal dimensions (radius and height) using Maple’s Optimization feature Chapter 20 – more Maple support for mathematical modeling Solving integration problems – finding the surface area of an irregularly shaped object Piecewise expressions – irregularly shaped curve consisting of a series of “piecewise” expressions Curve fitting using Splines – generating spline curves of various degrees to connect a series of data points

11 Lab 3 Overview – cs123 Lab 3 outline Part 0 – practice with Maple mechanics for Optimization, piecewise expressions, integration and differentiation, Spline curve fitting Part 1 – Find the minimum dimensions of a can that holds a specified volume of liquid using Maple’s Optimization function Create an expression for the can’s surface area (objective function) to hold a constant volume of liquid Use the Maple Optimization function to find the minimum dimensions (radius and height) for the surface area Part 2 – finding the surface area of an irregularly shaped machine part Define top of object in terms of a series of piecewise expressions Bottom = a single expression Plot top, bottom expressions  shape of object Integrate (top – bottom)  area between curves

12 Lab 3 Overview – cs123 Lab 3 outline Part 3 – Spline curve fitting to analyze the power of a baseball bat swing Given a table of times (seconds) versus power (hp), create and plot spline curves of degrees 1 through 4 Answer 4 questions using the results of the spline plots Notes: B. total energy transferred is the integral of the power curve C. point of maximum power increase – take derivative of power curve and then find maximum (optimization)

13 Lab 3 Overview – cs123 New lab description format for Lab 3 – Basic lab description document in form of a shell, directing you to starter scripts for each of 4 parts (Part 0, 1, 2 and 3) – Each starter script consists of text directions for completing that Part – Maple statements and results to be generated at various points within starter scripts Mix of text and math modes

14 10/22/09 Lab 3 Maple Concepts: Discussion – cs122 Conditional looping with “while” loops – while (condition) do loop body – end do; – The loop body will continue to get executed as long as the condition = true – The condition expression usually utilizes 1 or more relational operator ( >= <> = ) – Sometimes, a variable in the condition expression must be initialized so that it will have a value for the 1 st condition test – Faulty program logic can result in an endless (infinite) loop Use “stop (red)” hand on Maple icon list If this does not stop the execution, use “Force quit” from the Maple menu

15 10/22/09 Lab 3 Maple Concepts: Discussion “while” loops - continued – “while” and “for” loops can be used in combine when you want to potentially loop over a series of values, but also want to stop when a certain condition is met. – ex.  for angle from 0 to 90 while (cos >= sin) do This loop has the potential to loop 91 times But will stop looping once cos < sin. In some iteration (angle = 45), the cos and sin will become equal. Before, the cos is > sin and after, the cos < sin. Therefore, this loop will execute 46 times, for angles of 0 through 45

16 10/22/09 Lab 3 Maple Concepts: Discussion - cs122 Choosing among alternative actions – “if” statement – if (condition) then Code to execute if condition is true – end if; – if (condition) then Code to execute if condition is true – else Code to execute if condition is false – end if; – Many examples in chapter readings and lab

17 10/22/09 Lab 3 Maple Concepts: Discussion - cs122 Animation / Movie basics An animation is actually a series of “snapshot” plots which are displayed one by one to give the appearance of a continuous “movie” – “insequence=true” in the display function designates the plots as an animated sequence – When executed, the initial plot in the sequence appears on the screen. By “right-clicking” on this plot and selecting Animate  Play The animation will run (see demo for example animation) Note the overall syntax and order of operations in the demo example!

18 10/22/09 Lab 3 Maple Concepts: Part 0 Exercises – cs122 Part 0 will provide practice for some of the Maple features needed for this lab – The following concepts are illustrated Example 1 – produce table of sine and cosines functions for angles from 0 to 90 degrees Example 2 – script to find and report the angle between 0 and 90 degrees for which the sine and cosine are equal Example 3 – produce table of sines and cosines for angles between 0 to 90 where cosine is greater than sine Example 4 – Create a movie of sine and cosine values for 0 through 90 degrees

19 Lab 3 Maple Concepts: Discussion and Demo – cs123 Part 1 – Maple’s Optimization feature Creating the Objective function (surface area of a can) Surface area = lateral area + top and bottom SA = 2*pi*r*h + 2*pi*rsquared Since the surface area needs to be a function of a single variable (eg. radius=r), we need to find an function relating h (height) to r and substitute. Since the volume is constant at 1000: 1000 = pi*rsquared*h  h = 1000 / (pi*rsquared) Substitute this equation for h into the SA equation above to obtain the objective function SA(r).

20 Lab 3 Maple Concepts: Discussion and Demo – cs123 Part 1 – Maple’s Optimization feature - continued Now use this objective expression SA(r) to find the minimum surface area over a range of radii that holds a volume = 1000 minRslt:=Optimization[ Minimize](objexpression,r=1..10) You will obtain 2 results minRslt[1]  minimum surface area minRslt[2]  radius that produces this minimum SA Substitute minRslt[2] into the equation for h to obtain the associated height Note Optimize[Minimize](objexp,x=a..b) → produces set of approximate results Minimize(objexp,a=a..b, location) → produces set of exact results

21 Lab 3 Maple Concepts: Discussion and Demo – cs123 Part 2 – Area between 2 curves Piecewise expressions Use clickable interface as opposed to textual I/F To add more than 2 piecewise expressions Ctrl-Shift-R (PC) (Command=Shift-R for Mac) Be sure to highlight each area and type over it (not as “free-form” as you would like) – see demo “otherwise” condition – can use to denote all other values not defined in piecewise expressions Be sure to assign the piecewise expression a name → top := “piecewise expression” Integration Indefinite integral  int(f(x),x)  generates function in terms of x Definite integral  int(f(x),x=a..b)  numeric result Differentiation → result := diff(somefunction, x);

22 Lab 3 Maple Concepts: Discussion and Demo – cs123 Problem 3 – analysis of batter’s swing Plot_structure:=CurveFitting[Spline](x values, y values, name for independent variable, degree=degree of Spline fit) Or, can use with(CurveFitting); Spline( ); Ex. CurveFitting[Spline](T,p,t,degree=3) Will produce a Spline curve fit using cubic piecewise expressions Some Spline details Curve passes through all points (note that linear least squares curve fit produces a “best straight line estimate” for all points, and may possibly not pass through any point) A piecewise expression will be generated to connect each pair of adjacent points

23 Lab 3 – Maple Part0 Exercises – cs123 1. Maple optimization logistics 2. Piecewise expression and integration 3. More on integration and differentiation 4. Curve fitting with Splines


Download ppt "CS 122 / CS123 Engineering Computation Lab Lab 3 Bruce Char Department of Computer Science Drexel University Summer 2011 ©By the author. All rights reserved."

Similar presentations


Ads by Google