Plot a function (x,y) in polar coordinates?

Hi, I’ve this function f(x,y) such that (x,y) are the coordinates of an ellipse, that is (x/a)^2 + (y/b)^2 = 1…so like the function yields an output for all coordinates of the ellipse…I want the plot in polar coordinates, like I want to have all the outputs laid down on a circular plot with degrees shown all around and the values of f(x,y)…i’ve absolutley no idea how to do this so please help me out from the basics, as i’m new to mathematica…

Hi Muhammad,

I am no quite sure whether this is what you want to achieve, but one way is to define the following two functions;

cartesianToPolarEqn[eqn_] := 
 Module[{}, eqn /. {Rule @@@ Transpose@{{x, y}, FromPolarCoordinates[{r, \[Phi]}]}}]

and

implicitPolarPlot[eqn_, options : OptionsPattern[]] := Module[{},pl = ContourPlot[eqn, {r, 0, 8 Pi}, {\[Phi], 0, 2 Pi}, 
PlotPoints -> 100]; g[r_, th_] := {r Cos[th], r Sin[th]};pl[[1, 1]] = g @@@ pl[[1, 1]]; Show[pl, options]]

If you then want to convert your implicit equation into polar coordinates you can do:

cartesianToPolarEqn[(x/a)^2 + (y/b)^2 == 1 ]

which gives:

You can then generate a plot of this using:

implicitPolarPlot[(r^2 Cos[\[Phi]]^2)/a^2 + (r^2 Sin[\[Phi]]^2)/b^2 ==1 /. {a -> 1, b -> 2}]

This rescales the axes so that everything looks circular again, but the function also takes additional arguments such that

implicitPolarPlot[(r^2 Cos[\[Phi]]^2)/a^2 + (r^2 Sin[\[Phi]]^2)/b^2 ==1 /. {a -> 1, b -> 2}, PlotRange -> {{-2, 2}, {-2, 2}}]

Note that in both cases I needed to specify the parameters “a” and “b”.

Cheers,

Marco

great answer, as always!

The transformation rules can also be obtained like this:

rules = Thread[{x, y} -> CoordinateTransform[{"Polar" -> "Cartesian", 2}, {r, \[Phi]}]]

which might be easier to grasp…

Another new way of doing the entire thing, including the polar axes is:

equation=(x/a)^2+(y/b)^2==1;
equation=equation/.{a->2,b->3}

rules=Thread[{x,y}->CoordinateTransform[{"Polar"->"Cartesian",2},{r,\[Phi]}]];
equation=equation/.rules;

plot=ContourPlot[Evaluate[equation],{r,0,6},{\[Phi],-\[Pi]+10^-8,\[Pi]},PlotPoints->100];
lines=Cases[Normal[plot],Line[x_]:>Line[FromPolarCoordinates[x]],\[Infinity]];
max=Max[Cases[lines,(x:{_?NumericQ,_?NumericQ}):>Norm[x],\[Infinity]]];
PolarPlot[1.2max,{\[Phi],0,2\[Pi]},Epilog->lines,PlotStyle->None,PlotRange->5,PolarAxes->Automatic,PolarTicks->{"Degrees",Automatic}]

giving:

of course one does not have to go back and forth polar coordinates if one has the equation in cartesian coordinates:

equation=(x/a)^2+(y/b)^2==1;
equation=equation/.{a->2,b->3}

plot=ContourPlot[Evaluate[equation],{x,-6,6},{y,-6,6},PlotPoints->100];
lines=Cases[Normal[plot],Line[_],\[Infinity]];
max=Max[Cases[lines,(x:{_?NumericQ,_?NumericQ}):>Norm[x],\[Infinity]]];
PolarPlot[1.2max,{\[Phi],0,2\[Pi]},Epilog->lines,PlotStyle->None,PlotRange->5,PolarAxes->Automatic,PolarTicks->{"Degrees",Automatic}]

giving the same plot…

Or even, yet simpler:

equation=(x/a)^2+(y/b)^2==1;
equation=equation/.{a->2,b->3}

plot=ContourPlot[Evaluate[equation],{x,-6,6},{y,-6,6},PlotPoints->100,Axes->False,Frame->False];
plot2=PolarPlot[3,{\[Phi],0,2\[Pi]},PlotStyle->None,PlotRange->5,PolarAxes->Automatic,PolarTicks->{"Degrees",Automatic}];
Show[{plot2,plot}]

Hi Sander,

that looks nice. Adding polar axes is very useful and I over read it in the question. I also fudged the function a bit, e.g. there are no plot ranges built into it. I also was not quite sure what the main purpose of the OP was/is; do we just need the plot so that we can technically plot everything in cartesian coordinates and then plot the axes, or do we need to do it actually for polar coordinates? In your next two posts you have used the more direct way without transforming forward and backward again.

It by the way quite nice to add a plot theme to your plot:

equation = (x/a)^2 + (y/b)^2 == 1;
equation = equation /. {a -> 2, b -> 3};
rules = Thread[{x, y} -> CoordinateTransform[{"Polar" -> "Cartesian", 2}, {r, \[Phi]}]];
equation = equation /. rules;
plot = ContourPlot[Evaluate[equation], {r, 0, 6}, {\[Phi], -\[Pi] + 10^-8, \[Pi]}, PlotPoints -> 100]
lines = Cases[Normal[plot], Line[x_] :> Line[FromPolarCoordinates[x]], \[Infinity]];
max = Max[Cases[lines, (x : {_?NumericQ, _?NumericQ}) :> Norm[x], \[Infinity]]];
PolarPlot[1.2 max, {\[Phi], 0, 2 \[Pi]}, Epilog -> {Red, Thick, lines}, PlotStyle -> None, PlotRange -> 5, 
PolarAxes -> Automatic, PolarTicks -> {"Degrees", Automatic}, PlotTheme -> "Business"]

Cheers,

Marco