# Plotting implicit functions

**URL:** <https://discourse.julialang.org/t/plotting-implicit-functions/54874>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 8, 2021, 9:38pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874 "2021-02-08T21:38:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![em55](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@em55](https://discourse.julialang.org/u/em55)\
**Post date:** [February 8, 2021, 9:38pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/1 "2021-02-08T21:38:34Z")

</div>

Hello! I have an implicit equation that fixes y as a function of x. I plotted y over a range of x using the package ImplicitEquations.jl .

I would now like to plot a third variable w over the same x range and using the y values consistent with the implicit equation. Is this possible?  
I thought of using some indicator function to make sure the first implicit equation is satisfied, but I cannot do it.

Here’s my code:

using ImplicitEquations, Plots

μ=-1.0  
σ=1.0  
L=1.0  
cᶠ=1.0  
cᵉ=1.0  
θ=0.5  
r=0.1  
a=1.0  
ρ=(μ+sqrt(μ^2+2σ^2))/(σ^2)  
τᵉ=0

#x=[-.2,.2] and y defined by implicit function f(x,y)=0  
f(x,y)=(1+τᵉ)_cᵉ-(ρ/(1+ρ)exp(a-y)+exp(-ρ(a-y))/(ρ+1)-1)_(exp(y)_(1-θ)_(1+ρ)/(ρ\*(r-μ-σ^2/2)))^(1-θ)\*((1+x)\*cᶠ/r)^θ  
p1=Plots.plot(f ⩵ 0, xlims = (-0.2, 0.2), ylims=(0, 0.6),linewidth = 2)

#How to plot w over x range [-.2,.2] and y range implied by f(x,y)=0?  
w(x, y)=((exp(y)_(1-θ)_(1-ρ)_r/(ρ_(r-μ-σ^2/2)\*(1+x)\*cᶠ))^(1-θ))

Thanks!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 8, 2021, 11:05pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/2 "2021-02-08T23:05:52Z")

</div>

Do not know the answer. However, for your specific equation `f(x,y) = 0`, it seems at first sight that one could solve explicitly for `x` as a function of `y`? The equation is quite long and I might well be overlooking something. Sorry if that is the case.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 8, 2021, 11:25pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/3 "2021-02-08T23:25:27Z")

</div>

If you want to plot `(x,y,w(x,y))` along the curve defined by `f(x,y) = c` then ImplicitEquations isn’t quite the right tool, as it doesn’t really parameterize the curve so that `w` can be evaluated along it.

However, the `MDBM` package can help (as could the `Contour` package). Here is how you might do this for two simpler functions (though you could likely make this more efficient):

```julia
using Plots, MDBM

f(x,y) = x^2 - y^2 - 2
w(x,y) = x^3 - y^3

xs = -2:1:2
ys = -2:1:2

prob = MDBM_Problem(f, [xs, ys])
solve!(prob, 4)

p = plot(; xlim=(-4,4), ylim=(-4,4), zlim=(-8,8), legend=false)
mdt=MDBM.connect(prob)
for i in 1:length(mdt)
    dt=mdt[i]
    P1=getinterpolatedsolution(prob.ncubes[dt[1]], prob)
    P2=getinterpolatedsolution(prob.ncubes[dt[2]], prob)
    x0,x1=P1[1], P2[1]
    y0,y1=P1[2], P2[2]
    z0,z1 = w(x0,y0), w(x1, y1)
    
    plot!(p, [x0,x1],[y0,y1],[z0,z1], linewidth=5)
end
p

```

---

<div class="post-metadata">

**Author:** ![em55](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@em55](https://discourse.julialang.org/u/em55)\
**Post date:** [February 8, 2021, 11:33pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/4 "2021-02-08T23:33:08Z")

</div>

Thanks for your reply! I don’t think a closed-form solution exists in this case. I did work around this and get a numerical solution for y over an x grid, but I was interested in implicit function plots more in general!

---

<div class="post-metadata">

**Author:** ![em55](https://avatars.discourse-cdn.com/v4/letter/e/3ec8ea/32.png) [@em55](https://discourse.julialang.org/u/em55)\
**Post date:** [February 8, 2021, 11:34pm UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/5 "2021-02-08T23:34:18Z")

</div>

Thanks a lot for your reply! This looks like what I need, I’ll try to implement it for my case.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [February 9, 2021, 9:39am UTC](https://discourse.julialang.org/t/plotting-implicit-functions/54874/6 "2021-02-09T09:39:03Z")

</div>

@em55, could you please format your code as recommended on the [PSA](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) at top of Julia discourse pages, by enclosing it within backticks?  
As is, it does not run for me.
