Message Boards Message Boards

GROUPS:

Get same numerical and analytical integral's results?

Posted 7 months ago
765 Views
|
4 Replies
|
2 Total Likes
|

Good morning-Afternoon.

I was recently programing an integral and decided to check how it would work compared to a numerical aproximation. I used Simpson and trapezoid rules. However, my results are several orders of magnitude different. I have attached my code.

Can someone please give me some hindsight on what I am doing wrong?

Thank you very much.

4 Replies

There are a number of ways to improve on this. Most important error is to not multiply trapezoid heights by step length. The value below is pretty good for an approximation.

step*(Total[Map[gausiana, N[PuntosEnsayo[[2 ;; -2]]]]] + 
   1/2 (gausiana[PuntosEnsayo[[1]]] + gausiana[PuntosEnsayo[[-1]]]))

(* Out[192]= 0.0000718038661611 *)

I see. Thank you bery much for the information. I am afraid I am not fluent enough in mathematica to propperly understand your proposed aproximation, but I will try to comprehend it.

Jaime,

your approach is basically correct. But you give the number of a point to the gaussian and not the x-value, which is in fact

PuntosEnsayo[[i]]

and not i. i is the number of your x-value. And you start with i =0, which is problematic for Tables.

Try in your code

For[i = 1, i < numero, i++, 
  valorNumerico = 
   valorNumerico + 
    step ((1/2)*(gausiana[PuntosEnsayo[[i]]] + 
         gausiana[PuntosEnsayo[[i + 1]]]))];
For[i = 1, i < numero, i++, 
  valorNumericoSimpson = 
   valorNumericoSimpson + 
    step (1/6)*(gausiana[PuntosEnsayo[[i]]] + 
       gausiana[PuntosEnsayo[[i + 1]]] + 
       4*gausiana[PuntosEnsayoMedios[[i]]])];
valorNumerico
valorNumericoSimpson

Daniel's approach is Mathematica-like (avoid Do's, For's and so on if possible) and elegant.

He applies gausiana to each x-value, meaning each value in PuntosEnsayo. This is done with the Map-command. Then he adds all these values ( Total ) and finally multiplies with your step-width. You can achieve this as well with

step*Total[gausiana /@ PuntosEnsayo]

neglecting the contributions at the ends of the interval

Yes, this is exactly what I was looking for. Thank you very much for your help.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract