1

I have a function of 3 arguments

mu[kx_, ky_, lambda_] := -(kx^2 + ky^2) - (kx^2*(kx^2 + 5*ky^2)*lambda)/(kx^2 + ky^2)^4

I want to plot mu against lambda for a range of values for each of kx and ky - for each pair {kx,ky} there will be one straight line of mu[lambda] as shown in the attached screenshot.

I got the screenshot using Plot, but that is very slow (prohibitively slow if I want to decrease epsilon to get better resolution).

Q1 - will it be quicker if I use ListLinePlot to plot this set of straight lines? (My guess is that it would be much quicker as Mathematica will only need to calculate two points (mu[lambdaMin] and mu[lambdaMax]) for each line.)

Q2 - how do I do it with ListLinePlot?! My guess is to first Table out the values of kx,ky I want to use, but this gives me a square matrix. Then I think I need to reshape this into a 2-by-many matrix then expand this by Table-ing over lambda. But I can't work out how.

Thanks in advance, apologies for such a basic question, still fairly new to Mathematica etc.

output using Plot

kglr
  • 394,356
  • 18
  • 477
  • 896
jms547
  • 399
  • 1
  • 8

1 Answers1

3

how do I do it with ListLinePlot?

ϵ = .2;
ListLinePlot[Join @@ Table[{l, mu[x, y, l]}, {x, ϵ, 1, ϵ}, {y, ϵ, 1, ϵ}, {l, {-5, 5}}]]

enter image description here

will it be quicker if I use ListLinePlot to plot this set of straight lines?

timings = Table[{ϵ , First@RepeatedTiming@ 
  Plot[Evaluate[Join @@ Table[mu[x, y, l], {x, ϵ, 1, ϵ}, {y, ϵ, 1, ϵ}]], {l,  -5, 5}], 
  First@ RepeatedTiming@
   ListLinePlot[Join @@ Table[{l, mu[x, y, l]}, {x, ϵ,  1, ϵ}, {y, ϵ,      1, ϵ}, 
    {l, {-5, 5}}], PlotRange -> {-20, 20}]}, {ϵ , .05, .2, .05}] 

Grid[Prepend[timings, {"ϵ", "Plot", "ListLinePlot"}]] /. 
  x_Real :> NumberForm[x, {5, 3}] // TeXForm

$\begin{array}{ccc} \epsilon & \text{Plot} & \text{ListLinePlot} \\ 0.050 & 7.060 & 0.158 \\ 0.100 & 0.532 & 0.048 \\ 0.150 & 0.099 & 0.024 \\ 0.200 & 0.057 & 0.019 \\ \end{array}$

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thanks very much! Bonus question, which is off-topic but refers to your answer, how did you get markdown to write out the epsilons in code sections without using TeX?

    In my question I tried copy-pasting from Mathematica but it came out as \[Epsilon]. I'd like to use special characters like Greek letters in future questions.

    Thanks again!

    – jms547 Aug 15 '18 at 08:24
  • 1
  • 2
    @jms547 An alternative is to use the MMA SE userscript. It adds a few useful buttons that can replace those sequences with the correct characters and more – Lukas Lang Aug 15 '18 at 08:29