# 2D plot using output from the other function

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.

• 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. – Amita Aug 31 '15 at 14:21
• 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. – Fibonacci Aug 31 '15 at 14:39
• 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. – Amita Aug 31 '15 at 14:49
• 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. – Fibonacci Aug 31 '15 at 14:57
• y, p are real, Cos[y + p] <= 1, so -0.5*(x - 1)^2 >= 0, this is impossible unless x == 1. – 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}][]
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?

• 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. – Jack LaVigne Aug 31 '15 at 17:10
• 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. – Fibonacci Aug 31 '15 at 18:06