1. Consider the following integral: 3.14162 , x in radians 0 Write a VBA program
ID: 3596031 • Letter: 1
Question
1. Consider the following integral: 3.14162 , x in radians 0 Write a VBA program to evaluate this integral using Simpson's 1/3 rule. Input the lower and upper limits (3.1416 is accurate enough). Input and output from the main Sub. Put the 1/3 rule in a subprogram called by the main program. Use a function for the integrand. Note: you will need to include logic to account for the alternating 4's and 2's. Probably the easiest way is to include two for loops Begin with n 4. Compute the relative error, Ea, as the difference between the present value and the previous value. (For n 4 the previous value was zero.) Put the call to Simpson inside a for loop and continue until you get an Ea value less than 105 in magnitude. Output should include each iterated value, and the corresponding values of n and Ea.Explanation / Answer
VBA code for simpson 1/3 rule:
==============================
Module Module1
Sub Main()
Dim a As Decimal
a = 0
Dim b As Decimal
b = 3.1416
Dim x As Double
x = 10.0
eval(x, a, b)
End Sub
Sub eval(x As Double, a As Double, b As Double)
Dim y As Double
y = (b - a) / 6 * (integrand(a) + 4 * integrand((a + b / 2) + integrand(b)))
MsgBox(" is " & Chr(10) & "Value of y is " & y & Chr(10))
End Sub
Function integrand(x)
Dim y As Double
y = x * x * Math.Sin(x)
Return y
End Function
End Module
========================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.