Message Boards Message Boards

How can I set a condition for when FindRoot cannot find a solution?

Posted 4 years ago

Hello All, I have the following equation which I'd like to solve for Mt using FindRoot:

calcMt =
  {((At P) Sqrt[\[Gamma]/RSpec] Mt)/(Sqrt[
        T] (1 + (\[Gamma] - 1)/2 Mt^2)^((\[Gamma] + 1)/(
        2 (\[Gamma] - 1)))) - mst}

All the variables are fixed apart from mst. I'd like to find the value of mst that sets this expression to zero. For mst = 0.3, I have:

params1 = {At -> 0.00088394, P -> 399218.9, \[Gamma] -> 1.31723, 
   RSpec -> 287.221, T -> 1385.1, mst -> 0.3};
calcMt1 =
  {((At P) Sqrt[\[Gamma]/RSpec] Mt)/(Sqrt[
        T] (1 + (\[Gamma] - 1)/2 Mt^2)^((\[Gamma] + 1)/(
        2 (\[Gamma] - 1)))) - mst} /. params1;
res1 = {Mt} /. FindRoot[calcMt1, {Mt, 0.9}];
Echo[Mt1 = res1[[1]], "Mt1 = "];
(*Check*)
calcMt1 /. params1 /. Mt -> Mt1

The value of Mt returned is 0.556699 and with this value, the expression calcMt1 is 1.11022*10^-16. I now set mst to 0.4:

params2 = {At -> 0.00088394, P -> 399218.9, \[Gamma] -> 1.31723, 
   RSpec -> 287.221, T -> 1385.1, mst -> 0.4};
calcMt2 =
  {((At P) Sqrt[\[Gamma]/RSpec] Mt)/(Sqrt[
        T] (1 + (\[Gamma] - 1)/2 Mt^2)^((\[Gamma] + 1)/(
        2 (\[Gamma] - 1)))) - mst} /. params2;
res2 = {Mt} /. FindRoot[calcMt2, {Mt, 0.9}];
Echo[Mt2 = res2[[1]], "Mt2 = "];
calcMt2 /. params2 /. Mt -> Mt2

The value of Mt returned is now 1. but an error message is returned:

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

This is because there are no roots to the expresssion calcMt2 with mst = 0.4. As described in the Mathematica guidelines, when FindRoot cannot find a solution to within the prescribed accuracy, it returns the most recent approximation to a solution that it found. However, the value of calcMt2 for Mt = 1. is -0.0249476 so Mt = 1. is not a root.

I'm looking for the value of mst (which must be between 0.3 and 0.4) for which both the conditions Mt = 1. and calcMt = 0 are satisfied. Is there a way to recognise when FindRoot cannot find a solution to the prescribed accuracy? I could then use this as a condition to converge on the solution of mst. I've tried using NSolve to solve calcMt but it just hangs. I've attached a Notebook containing the code.

Any help would be greatly appreciated.

Many thanks,

Archie

Attachments:
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract