6

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Geo
  • 461
  • 3
  • 12

2 Answers2

6

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.)

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
3

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
xyz
  • 605
  • 4
  • 38
  • 117
  • 2
    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]] – Mr.Wizard Oct 19 '14 at 02:29
  • @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! – xyz Oct 19 '14 at 02:32
  • 2
    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. – Mr.Wizard Oct 19 '14 at 02:50
  • that's interesting. I will try using this for plotting the results for different initial conditions on the same graph. thanks – Geo Oct 20 '14 at 12:43
  • @Geo,see here,my answer is at last. – xyz Oct 20 '14 at 13:15