Find root of implicit function in Mathematica

2019-02-05 10:31发布

Find root of implicit function in Mathematica

I have an implicit function, for example:

f(x,y) = x^3 + x*y + y^2 - 36

I want to find the root, ie solutions to the equation f(x,y) = 0

Drawing the solution is easy:

ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

however I would like to have the data that is in the plot and not only the visual plot. So how do I find the data of the plot?

3条回答
甜甜的少女心
2楼-- · 2019-02-05 10:45

I would encourage you to explore the documentation on equation solving and particularly the Solve and NSolve functions.

EDIT

p = ContourPlot[x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]
p //InputForm

Copy and paste the bit you need.

Alternatively, and in my view a better solution to your actual problem.

sol = Solve[x^3 + x*y + y^2 - 36 == 0,{x}][[1]] 

You might need some options to get the right solution.

Table[{x/. sol,y},{y, -3 Pi, 3 Pi, 0.02}]
查看更多
该账号已被封号
3楼-- · 2019-02-05 10:48

I'm not sure if I am interpreting your second question properly, but assuming you require a list of (x,y) points from the generated ContourPlot, one way of doing this might be the following:

plot = ContourPlot[
  x^3 + x*y + y^2 - 36 == 0, {x, -2 Pi, 2 Pi}, {y, -3 Pi, 3 Pi}]

To obtain a list of points

points = Cases[Normal@plot, x_Line :> First@x, Infinity];

'Take a look' with ListPlot

ListPlot[points, PlotRange -> {{-2 Pi, 2 Pi}, {-3 Pi, 3 Pi}}]

giving

enter image description here

Edit

Nasser has correctly pointed out that this question has been addressed before. Here is one link to essentially the same question and this answer by Szabolcs is relevant.

As regards the answer given above, this method is probably more direct:

points2 = Cases[plot, x_GraphicsComplex :> First@x, Infinity]

Finally, I should acknowledge " LunchTime Playground. Fun with Mathematica: How to extract points from a plot", see here, which gives both methods suggested above (and which I now use routinely).

Edit 2

This method is an improvement on method 1 above, as the points are obtained as a list of {x,y} values (list-of-lists) without any extraneous { }.

Cases[Normal@plot, Line[{x__}] :> x, Infinity]

An article by Paul Abbott in the Mathematica Journal Vol 7, No 2, pp 108-112, 1998, Finding Roots in an Interval, gives a lot of useful information and is available here

He points out the the following also work

Cases[Normal@plot, _Line, -1][[1, 1]]

and(!)

plot[[1, 1]]

I have made reference in the comments to the question by FreshApple where a (slight variant) of the following method may be found:

InputForm[plot][[1, 1, 1]]

The following evaluates to True

plot[[1, 1]] == Cases[Normal@plot, Line[{x__}] :> x, Infinity] == 
 InputForm[plot][[1, 1, 1]] == Cases[Normal@plot, _Line, -1][[1, 1]]

Edit 3

Just or fun ...

ListPlot@ContourPlot[x^2 + y^2 == 1, {x, -1, 1}, {y, -1, 1}][[1, 1]]

gives

enter image description here

查看更多
欢心
4楼-- · 2019-02-05 10:52

A $.02 contribution on Verbeia's answer:

Remember to check both x(y) and y(x) solutions as one of them could be cleaner than the other.

In your example:

enter image description here

查看更多
登录 后发表回答