{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Tema21 - 21.22 Here is a VBA code to implement the...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 10
21.22 Here is a VBA code to implement the multi-segment trapezoidal rule for equally-spaced segments: Option Explicit Sub TestTrapm() Dim n As Integer, i As Integer, ind As Integer Dim label As String Dim a As Single, b As Single, h As Single Dim x(100) As Single, f(100) As Single 'Enter data and integration parameters ind = InputBox("Functional (1) or Tabulated (2) data?") a = InputBox("Lower bound = ") b = InputBox("Upper bound = ") n = InputBox("Number of segments = ") h = (b - a) / n If ind = 1 Then 'generate data from function x(0) = a f(0) = fx(a) For i = 1 To n x(i) = x(i - 1) + h f(i) = fx(x(i)) Next i Else 'user input table of data x(0) = a label = "f(" & x(0) & ") = " f(i) = Val(InputBox(label)) For i = 1 To n x(i) = x(i - 1) + h label = "f(" & x(i) & ") = " f(i) = InputBox(label) Next i End If 'invoke function to determine and display integral MsgBox "The integral is " & Trapm(h, n, f()) End Sub Function Trapm(h, n, f) Dim i As Integer Dim sum As Single sum = f(0) For i = 1 To n - 1 sum = sum + 2 * f(i) Next i sum = sum + f(n) Trapm = h * sum / 2 End Function Function fx(x) fx = 0.2 + 25 * x - 200 * x ^ 2 + 675 * x ^ 3 - 900 * x ^ 4 + 400 * x ^ 5 End Function 21.23 Here is a VBA code to implement the multi-segment Simpson’s 1/3 rule algorithm from Fig. 21.13 c : Option Explicit Sub TestSimpm()
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dim n As Integer, i As Integer Dim label As String Dim a As Single, b As Single, h As Single Dim x(100) As Single, f(100) As Single 'Enter data and integration parameters a = InputBox("Lower bound = ") b = InputBox("Upper bound = ")
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

Tema21 - 21.22 Here is a VBA code to implement the...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online