1. Write the statements required to write VBA function
code to compute the integral shown in the equations to
the right. Use the following header for your function:
Function myInt (a as Double, b as Double, _
n as Double) as Variant
Because the type of the function is Variant, it can return
either a numerical value or a string message
“Undefined” if the integral is undefined.
Function myInt(a As Double, b As Double, _
n As Double) As Variant
If n <> -1 Then
myInt = (b ^ (n + 1) _
- a ^ (n + 1)) / (n + 1)
ElseIf a * b > 0 Then
myInt = Log(b / a)
Else
myInt = "Undefined"
End If
End Function
b n 1 a n 1
n 1
n
1
b
b
n
a x dx ln a n 1 and ab 0
undefined if
n 1 and ab 0
2. The spreadsheet shown below the equations will be used to call your VBA function. Write the
Excel statements necessary to determine the integral, using the function above, in cells B4:E4.
Write the formula =myInt(B1, B2, B3) in cell B4 and copy the formulas to cells C4:E4.
3. What values would you get in cells F1:F3 if you entered the formula =myInt(C1, D1, E1) in cell F1
and copied that formula to cells F2 and F3?
For cell F1 the formula myInt(C1, D1, E1) gives a = 1, b = 2, and n = 1 so that the integral is
[2^(1+1) – 1^(1+1)]/(1+1) = (4 – 1) / 2 = 1.5
For cell F2 the copied formula becomes myInt(C2, D2, E2) which gives a = 2, b = -2, and n = 1
so that the integral is [2^(2+1) – (-2)^(1+1)]/(1+1) = (4 – 4) / 2 = 0
For cell F3 the copied formula becomes myInt(C3, D3, E3) which gives a = -1, b = -1, and n = 4
so that the integral is [(-1)^(2+1) – (-1)^(1+1)]/(4+1) = (1 – 1) / 5 = 0
Numerical Analysis of Engineering Systems - Answers