Re: Mathematica Numerical Integration and For Loop
- To: mathgroup at smc.vnet.net
- Subject: [mg81978] Re: Mathematica Numerical Integration and For Loop
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 8 Oct 2007 00:05:50 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <fea9tf$k99$1@smc.vnet.net>
General wrote: > I have a function which I have numerically integrated in Mathematica. I would like to be able to change a parameter of that function and re-evaluate that integral and then record the answer. > > Since this needs to be done for vary small changes of that independent variable it would be impossible for me to sit down and do it one by one. > > I am thinking of using a for loop but I cannot understand how to use it in my case. > > I would appreciate anyones help/ > > Here is the form of the function that I am integrating > > NIntegrate[ > ref[\[lambda], \[Theta], \[Phi], \[Pi]/5, (2 \[Pi])/5], {\[Theta], > 0, \[Pi]}, {\[Phi], 0, 2 \[Pi]}] > > I would like to be able to change the first variable within my ref function. This is lambda. So I want lambda to take different values within a range of 0 to 10 with increments of 0.01 and then store the output of the numerical integration. I would then like to plot lambda against the values obtained from the numerical integration. Forget about the for-loop construction and use *Table* instead. Below, I have cooked up a fully working example (though not very interesting from the mathematical point of view). (* First, we define a function with a check on each variable to ensure that f will be called by *NIntegrate* only if its arguments are numeric. *) f[x_?NumericQ, y_?NumericQ, p_?NumericQ] := Exp[-x + y] Sin[p] (* Then, we construct a list of points. Each point is a pair of values that hold the value of p and the result of the numerical integration. *) data = Table[{p, NIntegrate[f[x, y, p], {x, 0, \[Infinity]}, {y, 0, 1}]}, {p, 1, 10, 1/10}]; (* Finally, we plot this list of pairs with *ListPlot*. *) ListPlot[data] Regards, -- Jean-Marc