Message Boards Message Boards

6 Replies
6 Total Likes
View groups...
Share this post:

How to Plot a parametric function

Posted 11 years ago
I have an integral that can only be solved numerically. I wish to plot the integral's value as a function of a parametric variable.

Currently, I have to execute the integral for each parametric value, which is time consuming and not too flexible. How can I automatically generate the numerical results and then plot them?
POSTED BY: Luther Nayhm
6 Replies
NIntegrate's timings of the examples given by Michael Rogers can be improved using appropriate options.

In[827]:= (f[a_?NumericQ] := NIntegrate[x Sin[a x], {x, 0, 10}, PrecisionGoal -> 2, Method -> {Automatic, "SymbolicProcessing" -> False}]; Plot[f[a], {a, 0, 10}];) // AbsoluteTiming[/size]
Out[827]= {2.411418, Null}

In[828]:= (f2 = ParametricNDSolveValue[{D[y[x], x] == x Sin[a x], y[0] == 0}, y, {x, 0, 10}, a];Plot[f2[a][10.], {a, 0, 10}];) // AbsoluteTiming
Out[828]= {2.251283, Null}

In order to speed-up the calls to NIntegrate I have skipped the symbolic preprocessing and decreased the default precision goal to 2. (NDSolve does not look into the global error, NIntergate does.)

Obviously, tuning the computations performance with NInterate's options is a more universal approach than using NDSolve.
POSTED BY: Anton Antonov
 (f[a_?NumericQ] := NIntegrate[x Sin[a x], {x, 0, 10}];
   Plot[f[a], {a, 0, 10}];) // AbsoluteTiming
 Out[66]= {10.193100, Null}
 (f2 = ParametricNDSolveValue[{D[y[x], x] == x Sin[a x], y[0] == 0}, y, {x, 0, 10}, a];
   Plot[f2[a][10.], {a, 0, 10}];) // AbsoluteTiming

Out[70]= {1.969538, Null}

(fgood = NDSolveValue[{D[y[x, a], x] == x Sin[a x], y[0, a] == 0}, y, {x, 0, 10}, {a, 0, 10}, MaxStepSize -> Pi/20.];
  Plot[fgood[10., a], {a, 0, 10}];) // AbsoluteTiming

Out[67]= {0.058061, Null}

(* Does not produce an accurate graph *)
(fbad = NDSolveValue[{D[y[x, a], x] == x Sin[a x], y[0, a] == 0},
    y, {x, 0, 10}, {a, 0, 10}];
  Plot[fbad[10., a], {a, 0, 10}];) // AbsoluteTiming

Out[68]= {0.045235, Null}

Since an integral is the solution to a differential equation, one can consider using  NDSolve  or  ParametricNDSolve.  NDSolve is over 100 times faster for this sort of use (e.g. plotting).  This can be very important for certain uses, such as inside  Manipulate.  However  NDSolve needed help on Sean Clarke's example.
POSTED BY: Michael Rogers
Posted 11 years ago
I will look into your suggestions. The integrals evaluate very fast. I did run into another issue, which I may post in a new thread.

I was actually plotting several functions. The results looked unusual. It was suggested that I was looking at some rounding errors.

I checked it via changing the accuracy goal for the individual integrals, and above a goal of 6, the integrals did not compute. However, for an accuracy goal of Infinity, the did compute and gave the expected results. 

I do not understand why this occurred. Any guesses?
POSTED BY: Luther Nayhm
Posted 11 years ago
?NumericQ is the neatest thing I have seen! It really works well. I need to adjust the range of the ordinate in the plots, but other than that, it would have saved me weeks...months...of laborious calculations in Mathematica.

You da man!!
POSTED BY: Luther Nayhm
The first way to do this is to write a function which calculates the value of the integral for a given parameter called a. Let's say our integral is 
NIntegrate[ x Sin[ a x],{x,0,10}]
We can make a function, f, that takes a and calculates the integral:
f[a_?NumericQ] := NIntegrate[ x Sin[ a x],{x,0,10}]
Please see this article on why ?NumericQ was used in the definition of f. The function f can be plotted using Plot. 

As an additional note, you can often reformulate your problem so that instead of solving an integral, you are solving a corresponding differential equation. Once you've done this, you can use ParametricNDSolve
POSTED BY: Sean Clarke
Posted 11 years ago
Cool. Good pointers to look into. Mathematica is a process of steps backwards and then several steps forward.

I will check out your suggestions and feed back.
POSTED BY: Luther Nayhm
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract