Plotting implicit functions

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!

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.

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):

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
3 Likes

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!

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

@em55, could you please format your code as recommended on the PSA at top of Julia discourse pages, by enclosing it within backticks?
As is, it does not run for me.