Lab 2

17
Lab 1 VBA PROGRAMMING IN EXCEL 2007 Dr. Majid Mohammadian [email protected]

Transcript of Lab 2

Page 1: Lab 2

Lab 1VBA PROGRAMMING IN EXCEL 2007

Dr. Majid [email protected]

Page 2: Lab 2

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.

Page 3: Lab 2

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

Page 4: Lab 2

OPENING VBA EDITOR IN EXCEL 2007Two ways to access the excel VBA

programming environment:Alt F11Using the Developer tab Visual Basic

Page 5: Lab 2

THE PROGRAMING ENVIRONMENT

Project ManagerEditor

Properties

Page 6: Lab 2

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

Page 7: Lab 2

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 )

Page 8: Lab 2

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

Page 9: Lab 2

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

Page 10: Lab 2

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

Page 11: Lab 2

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

Page 12: Lab 2

Function v2_parachute

Function v2_parachute(v1, dt, g, cd, m)v2_parachute = v1 + (g - cd / m * v1) * dtEnd Function

Page 13: Lab 2
Page 14: Lab 2

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

Page 15: Lab 2

VBA CODE FOR MACHINE EPSILON

Function machineeps()Dim epsi As Doubleepsi = 1DoIf 1 + epsi <= 1 Then Exit Do epsi = epsi / 2Loopmachineeps = 2 * epsiEnd Function

Page 16: Lab 2

Single precisionFunction machineeps_s()Dim epsi As Singleepsi = 1DoIf 1 + epsi <= 1 Then Exit Do epsi = epsi / 2Loopmachineeps_s = 2 * epsiEnd Function

Page 17: Lab 2

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