# Fitting data in polar coordinates

I have some data in polar coordinates:

datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar1 = Rescale[datar[[All, 2]]];
datar2 = Rescale[datar[[All, 3]]];
plot2 = ListPolarPlot[{datar1, datar2}]


I want to fit the two curves to the equation: $$r=A\cos^{2}\left(t+\phi\right)$$, where $$t$$ is the angle and $$A$$ and $$\phi$$ are constants to be determined for each curve. How can I find those constants and overlay the fitting to the data?

## 2 Answers

Addition: 95% confidence bands for the mean added.

(* Convert from degrees to radians *)
datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar[[All, 1]] = 2 π datar[[All, 1]]/360;

(* Fit curves *)
nlm1 = NonlinearModelFit[datar[[All, {1, 2}]], a Cos[t + ϕ]^2, {a, ϕ}, t];
nlm2 = NonlinearModelFit[datar[[All, {1, 3}]], a Cos[t + ϕ]^2, {a, ϕ}, t];

(* Plot results *)
mpb1 = Table[Flatten[{t, nlm1["MeanPredictionBands"]}], {t, 0, 2 π, π/50}];
mpb2 = Table[Flatten[{t, nlm2["MeanPredictionBands"]}], {t, 0, 2 π, π/50}];
Show[ListPolarPlot[{datar[[All, {1, 2}]], datar[[All, {1, 3}]]}, PlotStyle -> PointSize[0.02]],
PolarPlot[{nlm1[t], nlm2[t]}, {t, 0, 2 π}],
ListPolarPlot[{mpb1[[All, {1, 2}]], mpb1[[All, {1, 3}]],
mpb2[[All, {1, 2}]], mpb2[[All, {1, 3}]]},
PlotStyle -> {{Blue, Dotted}, {Blue, Dotted}, {Orange, Dotted}, {Orange, Dotted}}, Joined -> True]]


• If I wanted to set the zero degrees on top, should I make 2 π (datar[[All, 1]]-90)/360 ? Nov 15, 2019 at 17:47
• And keep the counterclockwise direction? Does that mean your data was taken with zero degrees meaning 90 degrees? In other words, what measurement system characterized the angles you have? Or do you just want to now rotate the data?
– JimB
Nov 15, 2019 at 18:06
• I just want to rotate the the result plot so that the zero is on top and it increases clockwise. In my data, zero degrees is still the initial point. Nov 15, 2019 at 18:13
• Also, I might need to rescale the plot, making it bigger to combine with another plot. Nov 15, 2019 at 18:15
• Both of those requests constitute a new question.
– JimB
Nov 15, 2019 at 18:17

An alternative to Jim's proposal is to directly use the definition for least-squares fitting:

datar = Import["https://pastebin.com/raw/CM7Rj6jC", "Table"];
datar[[All, 1]] = datar[[All, 1]] °;

d1 = Drop[datar, None, {3}]; d2 = Drop[datar, None, {2}];

{a1, φ1} = NArgMin[Norm[Function[{θ, r}, a Cos[θ + φ]^2 - r] @@@ d1], {a, φ}]
{0.530572, -0.584595}

{a2, φ2} = NArgMin[Norm[Function[{θ, r}, a Cos[θ + φ]^2 - r] @@@ d2], {a, φ}]
{0.472955, 0.969005}

{Show[PolarPlot[a1 Cos[θ + φ1]^2, {θ, 0, 2 π}, PolarAxes -> True],
ListPolarPlot[d1, PlotStyle -> ColorData[97, 4]]],
Show[PolarPlot[a2 Cos[θ + φ2]^2, {θ, 0, 2 π}, PolarAxes -> True],
ListPolarPlot[d2, PlotStyle -> ColorData[97, 4]]]} // GraphicsRow