0

I am trying to implement Simpson's Rule using the math.net numerics library. The method that I would like to use takes four arguments, a Func (function pointer), intervalBegin, intervalEnd and partitionNumbers. Currently I am testing the method using Math.Sin, but can someone help me to understand how this Func should be implemented?

var test = MathNet.Numerics.Integration.SimpsonRule.IntegrateComposite(Math.Sin, 1, 4, 20);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Doro
  • 671
  • 1
  • 16
  • 34

1 Answers1

3

Additional material: Pic taken directly from math is fun. You can have absolutely any mathematical function like the one char below represents.

enter image description here

Integration calculates area between the line that function draws and X axis.

The reason I say function you pass into Simpsons integration doesn't matter is because ANY function can be used as long as it fits "1 number in, 1 number out" format.

End of additional material.

Simpson's formula is an integration which means it needs a mathematical function to calculate with. Below is the rough formula of simpson's integration (using numbers you passed as arguments and a random function) if you are interested, if not skip this part .

intervalBegin = 1; 
intervalEnd = 4;
partitionNumbers = 20;
f(x) = 3x^2;

deltaX = (intervalEnd - intervalBegin) / partitionNumbers;
SimpsonsIntegration = deltaX/3 * (f(intervalBegin) + 4*f(intervalBegin + deltaX*1)+ 2*f(intervalBegin + deltaX*2)+ 4*f(intervalBegin + deltaX*3)+ 2*f(intervalBegin + deltaX*4).....+4*f(intervalBegin + deltaX*19) +f(intervalEnd);

The function in Simpsons integration is ANY function that takes 1 numeric argument and returns one. (it might be type specific like float or double)

public double anyFunction(double number){ 
   double result = [calculations];
   return calculations;

}

Your call could look like this:

MathNet.Numerics.Integration.SimpsonRule.IntegrateComposite(anyFunction, 1, 4, 20);
Zero
  • 1,562
  • 1
  • 13
  • 29
  • I did not understand yet what the Func should do. Can you please be more specific? – Doro Oct 17 '14 at 06:53
  • Func is supposed to be any mathematical function. It can be function can be "x^2" it can be "Math.Pi*r^2". I'll edit my post in a sec, it might give you some context. – Zero Oct 17 '14 at 06:58
  • That's means I can use Math.Sin as well as Math.Pi? – Doro Oct 17 '14 at 07:00
  • Math.Pi will not work, because I believe it's a simple variable or a property, not a method. You can use your own function: MathNet.Numerics.Integration.SimpsonRule.IntegrateComposite(anyFunction, 1, 4, 20); check out some edits – Zero Oct 17 '14 at 07:27
  • Any hint how to create anyFunction? – Doro Oct 17 '14 at 07:31
  • second block of code in my answer. The [calculations] part is completely up to you. It can be calculation of area it can be something much more complex. – Zero Oct 17 '14 at 07:34
  • Right now I just need something simple, later when I know more I could think on something more complex. Can I ask a silly question? how to calculate each block under the line? – Doro Oct 17 '14 at 07:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63205/discussion-between-zero-and-doro). – Zero Oct 17 '14 at 07:40
  • I am sorry, I am at work and some security policy do not allow me to access the chat. What abut by email? – Doro Oct 17 '14 at 07:42
  • I see, I was saying if you want a simple function just replace [calculations] with square area: "number*number" (without quotation marks). Everything should work. – Zero Oct 17 '14 at 07:58
  • I did and it seems to work. I just got an update on the requirements; the intervals are not equals. So I guess that Simpson’s Rule would not fit. Do you some other approach except of Riemann Sum? – Doro Oct 17 '14 at 13:27