How to Integrate a Series

elliott10

New Member
Joined
Jan 16, 2020
Messages
23
Office Version
  1. 365
Platform
  1. MacOS
hi, I am new to coding/programming. How do I write a macro to integrate a series of n data points using an unknown function y=f(x)?
criteria includes the following:

Show a button to run your macro
Assume there are ten x/y pairs in the spreadsheet, starting with the first x value in cell A1, and
the first y value in cell B1, and the remaining data points organized vertically downward, with all
x-values in column A and y values in column B.
The x-values are equidistant (h= 1,2,....,etc I'm guessing)
All x and y values are equal or larger than zero. There are no empty spreadsheet cells or
irregular entries (letters and such) among the data set.
To approximate the integral, calculate and sum the areas of rectangles of area A=yi * deltaX
where yi is the right hand boundary of each interval bounded by two x-values (see posted
pictures of algorithm). deltaX is the distance between two adjacent x-values.
Output the value of the integral via a message box after the integration

I started with the following:

Function Unknown(x)

y = f(x)

End Function

(new module)

Option Explicit
Sub Eurler()
'This sets the integration limit from 1 to 10

xi = 0
xf = 10
h = 0.1

'This is the number of caluclations it will run

a = (xf - xi) / h





End Sub
 
VBA Code:
Sub eulerint()
  Dim xold, yold, ynew, slope, step As Single, k As Integer

  Sheets("Euler").Activate

  xold = 0
  yold = 2
  h = 1
  'k is a counter for the output row
  k = 1

  While xold < 11

    'calculate the slope at the current x-value
    slope = xold ^ 3

    'calculate the new y-value by adding the rise to the old y value
    ynew = yold + slope * h
 
    'output to Column B aka Column 2
    Cells(k, 2) = ynew
    Range("A" & k) = xold + h

    'determine New Values before executing the loop again
    k = k + 1
    yold = ynew
    xold = k + 1
  Wend
End Sub
Book1
AB
112
2429
3593
46218
57434
68777
791289
8102018
9113018
Euler

Run-time error 9 ubscript out of range when I copied and pasted into a new excel document
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top