Alternative to a "for" loop?

z3019494 shared this question 8 years ago
Answered

Hi all


Essentially I'm trying to calculate the area beneath a curve via Simpson's Rule in GeoGebra 4.0.


i.e. A \approx \frac{h}{3} \left(y_0 + 4\sum y_\text{odd} + 2\sum y_\text{even} + y_\text{last}\right)


I can construct a sequence of function values, but am stuck as to how to loop through all these values, and pick out the correct ones to sum.


I'm also finding the Spreadsheet view of somewhat limited use -- of course, compared to Excel. I was thinking that it might be the way to go but I still haven't found a suitable command to sum values in a list.


Can any experts out there help?


Thanks


Hubert

Comments (2)

photo
1

Hi,


There are several ways with "Sequence[]".

Here is a solution in one step:


    Result = h / 3 Element[First[Y] + 4Sum[Sequence[Element[Y, N], N, 2, Length[Y] - 1, 2]] + 2Sum[Sequence[Element[Y, N], N, 3, Length[Y] - 1, 2]] + Last[Y], 1]


Another way is probably with JavaScript.


Raymond


Annex GG40

https://ggbm.at/551749

photo
1

Thank you rami! :) The code has been put to good use.


I'll certainly study it more carefully too.

© 2019 International GeoGebra Institute