6
$\begingroup$

I'm trying to plot two recursive functions (p and θ) on a map. So far I have:

θ0 = Pi/2;
p0 = 0;
α = 0.1;
β = 1;

θ[j_] := θ[j] = θ[j - 1] + β*p[j - 1];
p[j_] := p[j] = 
   p[j - 1] - α*Sin[θ[j - 1] + β*p[j - 1]];
θ[0] = θ0;
p[0] = p0;

How can I plot p[j] against θ[j] for 0 <= j <= 100?

$\endgroup$
6
$\begingroup$

I think you are looking for DiscretePlot:

DiscretePlot[{θ[j], p[j]}, {j, 0, 100}]

enter image description here

Or perhaps you want something like this?:

ListPlot @ Table[{θ[j], p[j]}, {j, 0, 100}]

enter image description here

Another method is to use ParametricPlot after sufficiently coercing the input, e.g.:

f[x_?NumericQ] := {θ[#], p[#]} & @ Round @ x

ParametricPlot[f[j], {j, 0, 100}]

enter image description here

(Aspect ratio may be controlled with the AspectRatio option.)

$\endgroup$
8
  • $\begingroup$ that plots p and θ by j. I need to plot p by θ (for every matching j) as it if were a phase space $\endgroup$
    – Geo
    Oct 18 '14 at 22:39
  • $\begingroup$ @Geo You mean you want p on one axis and θ on the other? $\endgroup$
    – Mr.Wizard
    Oct 18 '14 at 22:41
  • $\begingroup$ yeah. sorry my not being so clear on the question. $\endgroup$
    – Geo
    Oct 18 '14 at 22:43
  • $\begingroup$ @Geo Please see update. $\endgroup$
    – Mr.Wizard
    Oct 18 '14 at 22:43
  • $\begingroup$ ahh that's it. Thank you sir. $\endgroup$
    – Geo
    Oct 18 '14 at 22:44
3
$\begingroup$

A proposal using Functional paradigm to avoid recursive functions.

data=
 With[{α = .1, β = 1},
   NestList[
    {#[[1]] + β #[[2]], #[[2]] - α Sin[#[[1]] + β #[[2]]]} &,
    {Pi/2, 0}, 100]];

ListPlot[data]

enter image description here

$\endgroup$
5
  • 2
    $\begingroup$ Although this doesn't directly answer the question it does show a good technique; NestList is often faster and simpler than recursion in Mathematica. By the way this can also be written: With[{α = .1, β = 1}, NestList[{# + β #2, #2 - α Sin[# + β #2]} & @@ # &, {Pi/2, 0}, 100]] $\endgroup$
    – Mr.Wizard
    Oct 19 '14 at 2:29
  • $\begingroup$ @Mr.Wizard,+1, your solution is indeed I need! I am alway writing the code: With[{α = .1, β = 1}, NestList[{# + β #2, #2 - α Sin[# + β #2]} & , [Pi/2, 0], 100]] before. Thanks sir! $\endgroup$
    – xyz
    Oct 19 '14 at 2:32
  • 2
    $\begingroup$ You're welcome. Another method you might like: With[{α = .1, β = 1}, NestList[# /. {x_, y_} :> {x + β y, y - α Sin[x + β y]} &, {Pi/2, 0}, 100]] as described here: (8399). This can be more readable than an excessive number of #* parameters. $\endgroup$
    – Mr.Wizard
    Oct 19 '14 at 2:50
  • $\begingroup$ that's interesting. I will try using this for plotting the results for different initial conditions on the same graph. thanks $\endgroup$
    – Geo
    Oct 20 '14 at 12:43
  • $\begingroup$ @Geo,see [here]( mathematica.stackexchange.com/questions/61050/…),my answer is at last. $\endgroup$
    – xyz
    Oct 20 '14 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.