Looking for a function to norm a Polar Plot

Hi everybody,

I’m looking for a solution to normalize a polar plot diagram to 1.
Therefore I need to find the maximum value over 0 up to 2Pi with respect to f and devide the function through this value.

here’s the code which doesn’t work properly so far.
The function “FindMaximum” maybe isn’t the right one?!!

r = 1
sp = 343.4
f = 7000

PolarPlot[Abs[
Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] +
Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp]
/
FindMaximum[
Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] +
f*2*Pi*0.032/sp] +
Cos[
f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] +
f*2*Pi*0.030/sp], {phi, 0, Pi}]

]
, {phi, 0, 2 Pi}, PlotRange -> All]

Thank you for help :slight_smile:

Looking forward for your solutions :slight_smile:

Hi,

Since you need to calculate the max value only once, you can take out FindMaximum out of PolarPlot. In addition, FindMaximum returns the location where the maximum occurs as well, so you need to take the first element for division. Try:

r = 1
sp = 343.4
f = 7000
max = FindMaximum[
Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] +
Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp], phi]
 
PolarPlot[Abs[
 Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] +
 Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp] / max[[1]]
], {phi, 0, 2 Pi}, PlotRange -> All]

Youngjoo Chung

Hi Youngjoo,
thank you for the quick reply. But the outcome of the plot is always at 1.5 maximum, which can’t be the case if the the max value is devided by itsself. It should be 1…
Why isn’t that the case?

Greetings
Peter

Try adding parentheses so that the expression plotted has the form Abs[ (x + y) / max ] instead of Abs[ x + y / max ].

PolarPlot[Abs[(Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] + 
        Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp])/max[[1]]], 
        {phi, 0, 2 Pi}, PlotRange -> All]

Oh, I am sorry, I made a mistake. Ilian is right. I meant to put max[[1]] outside Abs[…].

In retrospect, since the same expression is used twice, it would be better to define a function, and a cleaner code would be

r = 1
sp = 343.4
f = 7000
rho[phi_] := Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] + 
   Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp];
max = FindMaximum[rho[phi], phi]
PolarPlot[Abs[rho[phi]/max[[1]]], {phi, 0, 2 Pi}, PlotRange -> All]

Youngjoo Chung

Hi Ilian, hi Youngjoo and thx for your replys,

it looks fine so far, but for example at f=10000 the maximum on the x-axes reaches approximately 1.4 because of

FindMaximum::lstol: The line search decreased the step size to within the tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient increase in the function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>


Since I’d like to go up to 20kHz this would mix up the results…
How could I reach higher precision???
Any ideas? I think you have them also :wink:

Try giving FindMaximum a different starting point, for example

max = FindMaximum[rho[phi], {phi, 0.5}]

If that does not help in all cases, it might be a good idea to use only exact numbers in the input (32/1000 instead of 0.032 etc.) and/or increase the precision (by adding an option such as WorkingPrecision → 100).

Hey Ilian, thank you for your thoughts!!!
so I typed in all values in “exact numbers” which was a good idea :wink: and included the precision option.
So I works better but still not in every case which is still a problem since I wanted to generate a list and do further comparisions…
just for the plots I can deal with this problem now but not for generating a list from 2000-20kHz…

Hi,

The Norm function in the Cos[…] generates Abs[…] since Norm assumes that its argument is complex. This causes the FindMaximum::lstol messages to appear. You can remove Abs since everything is real. Then the rho function can be changed to:

rho[phi_] := Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0.0, 0}] + f*2*Pi*0.032/sp] + 
    Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {0, -0.063}] + f*2*Pi*0.030/sp] /. Abs[x_] -> x;

I observed that the maximum always occurs at phi slightly less than zero. Try setting the starting point to zero:

max = FindMaximum[rho[phi], {phi, 0}]

The phi value found is negative, but this is ok. You can add 2 Pi if you want. With these changes, I got the table of max values as follows with no warning messages:

In[63]:= Table[FindMaximum[rho[phi], {phi, 0}], {f, 2000, 20000, 2000}]

Out[63]= {{1.99783, {phi -> -0.0283289}}, {1.99134, {phi -> -0.0283289}}, {1.98054, {phi -> -0.0283289}}, {1.96549, {phi -> -0.0283289}}, {1.94625, {phi -> -0.0283289}}, {1.92291, {phi -> -0.496271}}, {1.89557, {phi -> -0.0283289}}, {1.86434, {phi -> -0.0283289}}, {1.82937, {phi -> -0.0283289}}, {1.79079, {phi -> -0.0283289}}}

Youngjoo Chung

On second thought, it is probably safer to use NMaximize which is less likely to return a local instead of a global maximum. For example, here is the same list of maximum values as above (I did not modify the original rho):

Table[NMaximize[{Abs[rho[phi]], 0 <= phi <= 2 Pi}, phi], {f, 2000, 20000, 2000}]

(* 
Out[4]= 
 {1.99783, {phi -> 6.25486}}
 {1.99134, {phi -> 6.25486}}
 {1.98054, {phi -> 4.28723}}
 {1.96549, {phi -> 6.25486}}
 {1.94625, {phi -> 6.25486}}
 {1.92291, {phi -> 6.25486}}
 {1.89557, {phi -> 2.26335}}
 {1.86434, {phi -> 6.25486}}
 {1.82937, {phi -> 0.281135}}
 {1.79079, {phi -> 5.31289}} 
*)

Hi,

I noticed that rho(phi) can become negative, and at certain frequencies, the max of rho can have smaller magnitude than the min. I did some analytical calculation and I found that if max is between 0 and 1 we need to divide with the minimum (<0) rather than the maximum since in this case Abs(minimum) > Abs(maximum). For example, at f = 3494, the max is 1.95243*10^-6 while the min is -2.

A quick fix is

max = Max[Abs[FindMaximum[rho[phi], {phi, 0}][[1]]], Abs[FindMinimum[rho[phi], {phi, 0}][[1]]]]
PolarPlot[Abs[rho[phi]/max], {phi, 0, 2 Pi}, PlotRange -> All]

Please note that rho(phi) is divided with max rather than max[[1]]. Or we can try

max = FindMaximum[Abs[rho[phi]], {phi, 0}][[1]]
PolarPlot[Abs[rho[phi]/max], {phi, 0, 2 Pi}, PlotRange -> All]

and use NMaximize instead as Ilian suggested. Some experimentation may be necessary, though, since FindMaximum may not like Abs[…] as seen before.

Youngjoo Chung

Right, so we just need to maximize Abs[rho[ phi ]]. I corrected my last post.

Hey you are brilliant!

so this would be the result of the discussion?

r = 1
sp = 3434/10
f = 6000

rho[phi_] := 
  1 Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-207/1000, 0}] + 
      f*2*Pi*0/sp] +
   1 Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-161/1000, 0}] + 
      f*2*Pi*(046/1000)/sp];
max = Max[Abs[FindMaximum[rho[phi], {phi, 0}][[1]]], 
  Abs[FindMinimum[rho[phi], {phi, 0}][[1]]]]
PolarPlot[Abs[rho[phi]/max], {phi, 0, 2 Pi}, PlotRange -> All, 
 WorkingPrecision -> 200]

there is still the pricision warning but the 1 thing is solved :slight_smile:

Hi,

I made some changes to your code, and the following works fine. NMaximize, as suggested by Ilian, gives no warnings. Seems to work ok without the WorkingPrecision option, though.

r = 1
sp = 3434/10;
f = 6000;

rho[phi_] := Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {-207/1000, 0}] + f*2*Pi*0/sp] + 
    Cos[f*2*Pi/sp*Norm[{r*Cos[phi], r*Sin[phi]} - {-161/1000, 0}] + f*2*Pi*(046/1000)/sp] /. Abs[x_] -> x;
max = NMaximize[{Abs[rho[phi]], 0 <= phi <= 2 \[Pi]}, phi][[1]]
PolarPlot[Abs[rho[phi]/max], {phi, 0, 2 Pi}, PlotRange -> All, WorkingPrecision -> 200]

Youngjoo Chung

in this case it does’t work :frowning:

r = 1
sp = 3434/10
f = 3000
rho[phi_] :=
  Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {0, 0}] + 
     f*2*Pi*(32/1000)/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-(63/1000), 0}] + 
     f*2*Pi*(30/1000)/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {(63/1000), 0}] + 
     f*2*Pi*(30/1000)/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-(127/1000), 0}] +
      f*2*Pi*(24/1000)/sp] +
   Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {(127/1000), 0}] + 
     f*2*Pi*(24/1000)/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-(191/1000), 0}] +
      f*2*Pi*(14/1000)/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {(191/1000), 0}] + 
     f*2*Pi*(14/1000)/sp] +
   Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {-(255/1000), 0}] + 
     f*2*Pi*0/sp] +
    Cos[f*2*Pi/sp* Norm[{r*Cos[phi], r*Sin[phi]} - {(255/1000), 0}] + 
     f*2*Pi*0/sp];
max = Max[Abs[FindMaximum[rho[phi], {phi, Pi}][[1]]], 
  Abs[FindMinimum[rho[phi], {phi, Pi}][[1]]]]
PolarPlot[Abs[rho[phi]/max], {phi, 0, 2 Pi}, PlotRange -> All, 
 WorkingPrecision -> 300]

for what is

. Abs[x_] -> x;

standing for?

In the last example, try

max = NMaxValue[Abs[rho[phi]], phi]

it works :slight_smile:
I was a little bit confused after all the posts :slight_smile:
but still wondering about “/ . Abs → x;”

“/ . Abs → x” is to replace all occurrences of the form Abs(x) to x in the function definition of rho(phi). A safer version would be “/ . Abs → Sqrt[x^2]”, but in the present case, it is ok since we have only Abs(x_)^2. This is from Norm(…)

In[11]:= Norm[{r*Cos[phi], r*Sin[phi]} - {-207/1000, 0}]

Out[11]= Sqrt[Abs[207/1000 + r Cos[phi]]^2 + Abs[r Sin[phi]]^2]

This can make some functions like FindMaximum misbehave. Removing Abs makes it better.

In[12]:= Norm[{r*Cos[phi], r*Sin[phi]} - {-207/1000, 0}] /. Abs[x_] -> x

Out[12]= Sqrt[(207/1000 + r Cos[phi])^2 + r^2 Sin[phi]^2]

Youngjoo Chung