Message Boards Message Boards

0
|
4743 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Plotting multiple functions on the same axes using Evaluate@Map

Posted 9 years ago

Hello all,

I am trying to understand what I am getting wrong in trying to plot a function using a few different parameters with Map. Example code is below, I am trying to plot the numerical integral of my function over the same x range, with a different extra parameter, I have a working method (right.png) for Plot[] using Evaluated->True, but this is not portable to e.g LogLinearPlot[] so I am trying to use Evaluate@Map instead, and this is where things go wrong.

f[x_, k_, a_] := Sqrt[1 + a Sin[k x]]/(
 1 + (k Cos[k x])/(1 + a Sin[k x]))
colors = (("DefaultPlotStyle" /. (Method /. 
        Charting`ResolvePlotTheme["Default", ListLinePlot])) /. 
    Directive[x_, __] :> x);
Plot[Evaluate@
  Map[NIntegrate[f[x, 2 \[Pi]/l, #], {x, 0, l}]/l &, {0.05, 0.1, 
    0.2}], {l, 10*^-6, 10*^-3}, PlotStyle -> colors, Frame -> True, 
 FrameLabel -> {"\!\(\*SubscriptBox[\(\[Lambda]\), \(ri\)]\)", 
   "<\!\(\*SubscriptBox[\(\[Beta]\), \(ph\)]\)>", 
   "Variation of average wake velocity with ripple wavelength"}, 
 PlotLegends -> LineLegend[colors, {"a=0.05", "a=0.1", "a=0.02"}]]

Obviously I am not understanding fully how evaluation works because I am not getting the results I expect (wrong.png), I just get three flat lines, as if the first data point has been mapped to all the x values in the domain. Can someone help me out here please?

Matthew

P.S This is what I am aiming at with (working) code for Plot[]:

f[x_, k_, a_] := Sqrt[1 + a Sin[k x]]/(
 1 + (k Cos[k x])/(1 + a Sin[k x]))
colors = (("DefaultPlotStyle" /. (Method /. 
        Charting`ResolvePlotTheme["Default", ListLinePlot])) /. 
    Directive[x_, __] :> x);
Plot[Map[NIntegrate[f[x, 2 \[Pi]/l, #], {x, 0, l}]/l &, {0.05, 0.1, 
   0.2}], {l, 10*^-6, 10*^-3}, PlotStyle -> colors, Frame -> True, 
 Evaluated -> True, 
 FrameLabel -> {"\!\(\*SubscriptBox[\(\[Lambda]\), \(ri\)]\)", 
   "<\!\(\*SubscriptBox[\(\[Beta]\), \(ph\)]\)>", 
   "Variation of average wake velocity with ripple wavelength"}, 
 PlotLegends -> LineLegend[colors, {"a=0.05", "a=0.1", "a=0.02"}]]
Attachments:
POSTED BY: Matthew Tooley
2 Replies

Actually, on a second look at this I see that on Mathematica 10.2 it does still produce the graph. This is because, although

Map[NIntegrate[f[x, 2 \[Pi]/ell, #], {x, 0, ell}]/l &, {0.05, 0.1, 0.2}]

does generate many error messages it then returns

{NIntegrate[f[x, (2 \[Pi])/ell, 0.05], {x, 0, ell}]/l, 
 NIntegrate[f[x, (2 \[Pi])/ell, 0.1], {x, 0, ell}]/l, 
 NIntegrate[f[x, (2 \[Pi])/ell, 0.2], {x, 0, ell}]/l}

Which the Plot then proceeds to properly plot.

POSTED BY: David Reiss

As with many problems the first error message gives you a hint of what is going wrong

NIntegrate::nlim: x = l is not a valid limit of integration. >>

When you perform your Evaluate the expression that is being evaluated is a number of NIntegrates with an upper limit which has no value (by the way generally it is not good to use the letter l as a variable as it is very hard to distinguish to the eye from a 1 in many fonts).

I.e., your Evaluate is doing the same as trying to compute the following (I have replaced your l by ell for readability):

Map[NIntegrate[f[x, 2 \[Pi]/ell, #], {x, 0, ell}]/l &, {0.05, 0.1, 0.2}]
POSTED BY: David Reiss
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