1
$\begingroup$

I have a function that reads:

f[P_?NumericQ, 
Ob_?NumericQ] := -Sqrt[P/Ob^2 + Sqrt[P^2 + Ob^2 w^2]/Ob^2] Tan[
 Sqrt[-(P/Ob^2) + Sqrt[P^2 + Ob^2 w^2]/Ob^2]] + 
 Sqrt[-(P/Ob^2) + Sqrt[P^2 + Ob^2 w^2]/Ob^2]
 Tanh[Sqrt[P/Ob^2 + Sqrt[P^2 + Ob^2 w^2]/Ob^2]]

For each pair of numerical values of P and Ob, there are infinitely many solutions for w (denoted as w0, w1, w2, w3, ...).

I want to write a concise code that does the following:

  1. Loops over values of Ob from 0 to 1.
  2. For each value of Ob, finds a critical value of P (Pcr) that makes the ratio of w2/w1 approximately equal to 3 (with reasonable tolerance).
  3. Stores or outputs these critical values of P for each Ob. I tried but couldn't come up with a successful one. Thanks for any help in advance.

Below is one of my trials:

     Remove["Global`*"];
     tolerance = 0.05;
     results = {};

     For[ObValue = 0.01, ObValue <= 1, ObValue += 0.01, 
    For[P = 0.01, P <= 1, P += 0.01,(*Add a loop for P values*)
    roots = NSolve[f[P, ObValue] == 0 && 0 < w < 20, w, Reals];
    If[Length[roots] > 1, w1 = w /. roots[[1]];
    w2 = w /. roots[[2]];
    Pcr = FindRoot[Abs[w2/w1 - 3] <= tolerance, {P, .1}];
    If[NumberQ[P /. Pcr], AppendTo[results, {ObValue, P /. Pcr}]; 
  Break[];] (*Exit P loop if critical value found*)];];];

results

$\endgroup$
5
  • $\begingroup$ What have you tried so far? Once you pinpoint a specific problem, we can more easily help you. Otherwise you are just for somebody to do your work for you. $\endgroup$
    – MarcoB
    Commented Jan 10 at 1:26
  • $\begingroup$ Thanks @MarcoB, I just added one of my trials. $\endgroup$
    – qahtah
    Commented Jan 10 at 3:05
  • $\begingroup$ P is never assigned a numerical value in your loop, so NSolve cannot work with it. You should assign it a value just like you do with ObValue. $\endgroup$
    – MarcoB
    Commented Jan 10 at 15:14
  • $\begingroup$ Thanks @MarcoB. I apologize for the error in my previous message. I've now corrected the code snippet. However, I'm encountering the same issue: the modified code isn't producing any output. $\endgroup$
    – qahtah
    Commented Jan 10 at 17:09
  • $\begingroup$ You really need to give some thought to what you are doing here. With the new modification, Abs[w2/w1-3] is just a number and does not depend on P, so you cannot "solve for P" any more. Your problem is quite complex, so you may want to break it down into smaller pieces, or try asking on the math forum for advice instead. $\endgroup$
    – MarcoB
    Commented Jan 10 at 17:54

1 Answer 1

3
$\begingroup$

With your function f[ ] as above, you can see the output of the function for a specific value. For example

f[0.1,0.2]

gives a formula with 2 terms, each with a square root and a tanh.

NSolve doesn't work on this

NSolve[f[0.1,0.2]==0,w] 

it just sits there. So let's try FindRoot:

FindRoot[f[0.1, 0.2] == 0, {w, 1}]

this returns

{w -> 3.43538}

almost immediately. You can do this for many starting values of w in a number of ways. For example:

Table[FindRoot[f[0.1, 0.2] == 0, {w, w0}], {w0, 1, 50}]

spits out 50 different terms, some of them redundant (which you would likely want to remove). Then you can iterate over different values of P and Ob.

If you only want the value near 3.4, you can do:

FindRoot[f[0.1, 0.2] == 0, {w, Pi}]

To iterate this over Ob, you might do:

Table[FindRoot[f[0.1, Ob] == 0, {w, Pi}], {Ob, 0.1, 2, 0.1}]

which gives a variety of answers, one for each Ob value.

$\endgroup$
1
  • $\begingroup$ Thanks @bill for your great answer, Now I want the value of P that gives me the ratio of the second frequency to the first w2/w1 is close to 3. That is what I call Pcr. $\endgroup$
    – qahtah
    Commented Jan 11 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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