2

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.

Fibonacci
  • 43
  • 4
  • 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. – Ukiyo-E 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. – Ukiyo-E 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. – Ukiyo-E Aug 31 '15 at 15:04
  • Fibonacci, I suspect that the "crash" of FindInstance was just the function giving you some errors. When I try to run your code, FindInstance reports that "The methods available to FindInstance are insufficient to find the requested instances or prove they do not exist. " That is valuable feedback: the function is telling you that it won't be able to extract a solution for you. It may be a limitation of the function, but it may also be an error in your code, which you might want to re-evaluate. Can you share what errors you experience, and what functions you are actually working with? – MarcoB Aug 31 '15 at 15:21
  • Amita, yes, for this selection of initial function the plot on {x,P} would be x==1 line. But I want to get this result with Mathematica command and not analytically. – Fibonacci Aug 31 '15 at 15:22
  • MarcoB, thank you. At this step it's useful me to understand why FindInstance[-0.5*(x - 1)^2 + Cos[y + p] == 1., {x, y}, Reals, 1] for any p provides the answer and f[p_] := x /. FindInstance[-0.5*(x - 1)^2 + Cos[y + p] == 1., {x, y}, Reals, 1]; Plot[f[p], {p, 0, 10}] does not. Is that because of program limitations? – Fibonacci Aug 31 '15 at 15:36

1 Answers1

3

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}]

plot

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?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 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. http://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