I would like to get 2D plot using output from the other function.

FindInstance[0.5*x^2 + Cos[y + P] == 1, {x, y}, Reals]

By changing values of P manually it gives instances for the expression. Now I would like to draw a plot on {x,P} or {y,P} plane considering P as variable.

I tried following approach for {x,P} case,

Plot[x /.FindInstance[0.5*x^2 + Cos[y + P] == 1, {x, y}, Reals], {P, 0, 100}]

but it does not replace P in FindInstance instantly.

Is there some easy way to satisfy this problem?

Thanks for any help.

P.S. I don't insist on FindInstance if there is other suitable function for my case. As it could give result when called separately and crash in combination with other functions.

  • $\begingroup$ f[p_] := x /. FindInstance[x^2 + Cos[y + p] == 1., {x, y}, Reals, 1];Plot[f[p], {p, 0, 10}].However, it is very slow. $\endgroup$ – Amita Aug 31 '15 at 14:21
  • $\begingroup$ Thank you. Here the other trouble appears. By changing the instant expression e.g f[p_] := x /. FindInstance[-0.5*(x - 1)^2 + Cos[y + p] == 1., {x, y}, Reals, 1]; Plot[f[p], {p, 0, 10}] it does not give any result. $\endgroup$ – Fibonacci Aug 31 '15 at 14:39
  • $\begingroup$ So you need to re-consider your problem, because x=1,y=-p is always a solution. And when plotting the image of {x, p}, y can take a fixed value. $\endgroup$ – Amita Aug 31 '15 at 14:49
  • $\begingroup$ I mentioned trivial function for simplifying, the actual function is quite different. However in some cases if FindInstance[] was called separately it gives result, if it used in other function it crashed. $\endgroup$ – Fibonacci Aug 31 '15 at 14:57
  • $\begingroup$ y, p are real, Cos[y + p] <= 1, so -0.5*(x - 1)^2 >= 0, this is impossible unless x == 1. $\endgroup$ – Amita Aug 31 '15 at 15:04

This works much faster:

f[P_?NumericQ] := x /. FindMinimum[(0.5*x^2 + Cos[y + P] - 1)^2, {x, y}][[2]]
Plot[f[P], {P, 0, 100}]


It is possible to run it even faster if you will use a successive plotting algorithm (not the algorithm Plot uses) and provide optimal starting values for FindMinimum on each new step on the base on the optimal values found on the previous step. The following two threads contain solutions which can help you to implement this approach:

How to implement the sample-point process like the built-ins of Mathematica?

How to obtain adaptive sampling as in Plot function?

| improve this answer | |
  • $\begingroup$ Alexy's answer also works on your second problem using FindMinimum[(0.5*(x - 1)^2 + Cos[y + P] - 1)^2. I don't know why FindInstance has problems but FindMinimum appears to be more robust for your particular problems. $\endgroup$ – Jack LaVigne Aug 31 '15 at 17:10
  • $\begingroup$ By using FindMinimum it's significant availability of the unique solution. Meanwhile FindInstance gives instances of the same solution in general but cause problems e.g. i.imgur.com/4AuEoch.png . Anyway, Thank you for the interesting suggestion as the solution of the problem. $\endgroup$ – Fibonacci Aug 31 '15 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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