Message Boards Message Boards

Manipulate not working with Findroot in cdf

Posted 10 years ago

I am trying to create a web presentable cdf file. It is supposed to find the root of a nonlinear equation that contains a variable parameter 9slider).

func = 1. + a1*x + 2 x^2 - 1/x; Manipulate[ Evaluate[x /. FindRoot[{func == 0} /. {a1 -> a}, {x, 1}]], {a, 10, 20}]

The problem works fine within Mathematica and computes the numerical value of the root. However the cdf file shows the equation in the output instead of a numerical root (see image below).

screenshot

Can someone suggest what I might do differently to make it work correctly?

The cdf file is attached.

Attachments:
POSTED BY: Kaushik Mallick
9 Replies
Posted 10 years ago

OK, now I understand. I have used your suggestion for Initialization :> to define the function and root finding and that solved my issue. Thank you again.

POSTED BY: Kaushik Mallick

I tried your suggestedcode but I still get an error:

That is not an error. As I mentioned, it is normal warning of FindRoot :

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.

The above is separate issue from your other error. The fix I did was to to remove your other error. The above warning remains and it is something you have to look into depending on your problem. I added the guess to help you is resolving this warning easier. You can always remove the slider for the guess.

POSTED BY: Nasser M. Abbasi
Posted 10 years ago

I really appreciate your continued help. Yes you are correct in stating that I want to find the root of 1 + a1p + a2p^2 - z == 0 when p has different values from the slider (after multiplying it by 100). I tried your suggestedcode but I still get an error:

enter image description here

Moreover, a slider of 'guess' for the cdf file will not be an attractive option to the end user.

I decided to modify my function definition to remove the requirement of multiplying slider value of p by 100. I am struggling to understand why my revised and much simplified code for root finding still gives an error:

Remove["Global`*"];
A1 = .3265;
A2 = -1.07;
Manipulate[
 Module[{func, z},
  func[z_] := 1. + A1*p1 + A2*p1^2 - z;
  Evaluate[z /. FindRoot[{func[z] == 0}, {z, 1}]]
  ],
 {{p1, 10, P }, 10, 40, Appearance -> "Labeled"}
 ]

enter image description here

POSTED BY: Kaushik Mallick

It looks like you want to find the root of 1 + a1*p + a2*p^2 - z == 0 when p has different values from the slider (after multiplying it by 100 ?) The first error you had due to how you over used /. The second issue is FindRoot itself, says

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. >>

For the above, you have to change the guess value or look into the suggestions given above.

Manipulate[
 2 + func[p*100, guess],
 {{p, 10, "P"}, 10, 20, 1, Appearance -> "Labeled", ImageSize -> Small},
 {{guess, .5, "guess"}, -1, 1, 0.01, Appearance -> "Labeled", ImageSize -> Small},
 Initialization :>
  (
   func[p_, guess_] := Module[{a1 = .3265, a2 = -1.07, z},
     z /. FindRoot[1 + a1*p + a2*p^2 - z == 0, {z, guess}]]
   )
 ]
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

In my attempt to solve the bigger problem, I am still having some issues with Manipulate and FindRoot. I have used your suggestions above.I think I am close but I need some helping hand.

Here is another simplified version of my problem definition:

Remove["Global`*"];
A1 = .3265;
A2 = -1.07;
Manipulate[
 Module[{func, z},
  func[z_, zp_] := 1. + A1*zp + A2*zp^2 - z;
  Evaluate[(2. + z0) /.

    {z0 -> 
      Evaluate[
       z /. FindRoot[{func[z, zp] == 0} /. {zp -> p1*100}, {z, 1}]]
     }]
  ],
 {{p1, 10, "P"}, 10, 20, 1, Appearance -> "Labeled", 
  ImageSize -> Small}
 ]

My objective is to solve a nonlinear equation 'func' that has a variable zp that needs to be manipulated by slider. The root of this equation 'func = 0' then gets used in the target variable '(2. + z0)' that I need to output within the Manipulate.

I am getting some errors related to the FindRoot as shown below. I would really appreciate some suggestion to modify the code to resolve these errors.

enter image description here

POSTED BY: Kaushik Mallick
Posted 10 years ago

Thanks for your help and suggestion again. I completely understand your approach. However my trial case was an attempt to debug a more complex problem, where I needed to have the control for variable 'a1' . Modifying my problem definition by moving the function definition within Manipulate like this:

Manipulate[func = 1. + a1*x + 2 x^2 - 1/x; 
 Evaluate[x /. FindRoot[{func == 0} /. {a1 -> a}, {x, 1}]], {a, 10, 
  20}]

stopped the error. Thanks a lot again.

POSTED BY: Kaushik Mallick

You are making things too complicated.

Manipulate[
 eq = 1 + a*x + 2 x^2 - 1/x;
 x /. FindRoot[eq == 0, {x, 1}],
 {{a, 10, "a"}, 0, 20, .1, Appearance -> "Labeled", ImageSize -> Small}
 ]

or you can put everything inside Module to limit scope of symbols used inside the function

Manipulate[
 Module[{eq, x},
  eq = 1 + a*x + 2 x^2 - 1/x;
  x /. FindRoot[eq == 0, {x, 1}]
  ],
 {{a, 10, "a"}, 0, 20, .1, Appearance -> "Labeled", ImageSize -> Small}
 ]
POSTED BY: Nasser M. Abbasi
Posted 10 years ago

Thank you so much for your direction. Your suggestion seemed to resolve the issue I was having. But now I am getting the following error within the Mathematica nb file:

FindRoot::nlnum: The function value {2. +1. a} is not a list of numbers with dimensions {1} at {x} = {1.}. >>

ReplaceAll::reps: {FindRoot[{func==0}/. {a1->a},{x,1}]} is neither a list of replacement rules nor a 
valid dispatch table, and so cannot be used for replacing. >>

What could be causing it?

POSTED BY: Kaushik Mallick

Add the option

SaveDefinitions->True

To your Manipulate finction. Alternatively you can add your function definition ti an Initialization option of your Manipulate.

POSTED BY: David Reiss
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