Lab 2
-
Upload
goragorago -
Category
Documents
-
view
156 -
download
6
Transcript of Lab 2
Lab 1VBA PROGRAMMING IN EXCEL 2007
Dr. Majid [email protected]
Adding the Developer Tab Click the Microsoft Office Button: and then click Excel Options.Click Popular, and then select the Show
Developer tab in the Ribbon check box.
MACRO SECURITYExcel has several levels of Macro Security that
can be selected using Developer Macro Security
Select Enable all You can also create a self-signing
certificate
OPENING VBA EDITOR IN EXCEL 2007Two ways to access the excel VBA
programming environment:Alt F11Using the Developer tab Visual Basic
THE PROGRAMING ENVIRONMENT
Project ManagerEditor
Properties
ADDING A NEW MODULESelect your spreadsheet in the project
explorerUse the main menu or the contextual menu
(mouse right button) to insert the new module
A simple function: my_sumOpen VBA using alt+f11Type the following:
Function my_sum(a, b) my_sum = a + bEnd Function
Return to excel using Alt+qUse your functionSave your file as Macro-Enabled (*.xlsm )
Single and double precisionAdd the following functions
Function test_s()Dim x As Singlex = 1.23456789012345test_s = xEnd Function
Function test_d()Dim x As Doublex = 1.23456789012345test_d = xEnd Function
Analytical solution:
Numerical (finite difference) solution:
We are going to implement both solutions in Excel
( ) 1 c m tgmv t ec
1 1( ) ( ) ( ) ( )
orNew value Old value slope step size
i i i i icv t v t g v t t tm
THE PARACHUTIST PROBLEM
ANALYTICAL SOLUTIONTo calculate analytical solution using
Excel, let us first set up a simple spreadsheet. As shown below, the first step involves entering labels and numbers into the spreadsheet cells
To attach names to parameters values, select cells Formula tab Define Name
Create the time column: D8=0D9=D8+dt, Drag down
Type the following formula in F8=g*m/cd*(1-exp(-cd/m*D8))Drag down
Numerical: G8=0: (Type in G9) =G8+(g-cd/m*G8)*dt
Function v2_parachute
Function v2_parachute(v1, dt, g, cd, m)v2_parachute = v1 + (g - cd / m * v1) * dtEnd Function
MACHINE EPSILONMachine, or computer epsilon (eps) is the smallest
floating point number that can be added to a floating point 1 that yields a result different from 1.
It is determined by the following type of algorithmepsi=1DOIF(epsi+1 < 1 ) EXITepsi = epsi / 2
END DOepsi = epsi * 2print epsi
END
VBA CODE FOR MACHINE EPSILON
Function machineeps()Dim epsi As Doubleepsi = 1DoIf 1 + epsi <= 1 Then Exit Do epsi = epsi / 2Loopmachineeps = 2 * epsiEnd Function
Single precisionFunction machineeps_s()Dim epsi As Singleepsi = 1DoIf 1 + epsi <= 1 Then Exit Do epsi = epsi / 2Loopmachineeps_s = 2 * epsiEnd Function
Other TasksTask 6: Create a table where x are the values from -3.1415 to 3.1415, and the values of y are y= cos(x) without using VB. Plot it. Add a title “y= cos (x)”, the axis titles x and y, and the legend. Now add another column with values of y=sin(x). Try to add the new series of values to the plot without plot the graph again (Hint: See “Adding a new series of values” in Plotting ). Note: Give your function in VB a special name e.g. my_cos (why?).
Task 7: Define the function y= cos(x) +sin(x) using VB, and plot it from 0 to 3.1415.
Task 8 (To be submitted with assignment 2): Write a VBA code for the falling parachute problem assuming that the drag force formula is now FD is now FD=cV2. Submit a print of all your VBA procedures and a table of v versus t using the following data: c =0.125kg/s and