Ask Your Question
0

implicit plot 3d for a triangle

asked 2022-03-12 09:53:13 +0100

ErWinz gravatar image

hi this code is the result of a long work and long interactions with members of this forum (end of the message) i could draw the "domain" fonction here by manual instructions, since this domain is x>0, y>0, x+y<4 but i would like to modify only the code between the two #** so i have to explicit this domain as a dom(x,y) function i have tried :

def dom(x,y):
    if ((x*y*(x+y)-4==0) and (x>0)):
        return 0
    else
        return 1

but it did not work now as you see below i have tried:

dom(x,y)=(x*y*(x+y-4))^2+(floor(x/4))^2

which returns 0 only on the triangle ; my aim is to draw the triangle but i draws nothing, without returning any error

how could i proceed ?

 # code à personnaliser ---------------------
 #**********   
    f(x,y)=x*log(1+y^2)
    lenom="fonction Ah : ****"
    leratio=(1,1,1/32)
    lacouleur="blue"
    (xmin,xmax)=(-8,8)
    (ymin,ymax)=(-6,6)
    (zmin,zmax)=(-140,50)
    def laregion(x,y,z):#domaine de definition
        return True
    domdom=True # True = afficher un domaine de contrainte
    dom(x,y)=(x*y*(x+y-4))^2+(floor(x/4))^2

lacouleurdom="cyan"
(zmindom,zmaxdom)=(zmin,zmax) #idem


#**********    
# -------------------------------------------

# code général, ne pas toucher --------------

# calculs généraux
show(html("<h4>"+lenom+"</h4>"))    
from sage.manifolds.operators import *
E.<x,y> = EuclideanSpace()
F = E.scalar_field(f)
H=f(x,y).hessian()
show(html("<h5>paramètres généraux</h5>"))
T=table([["f",f],["grad f=",grad(F)[:]],["H=",H]],frame=True,align='center')
show(T)

# calcul des points critiques
Cr= solve([grad(f)[0]==0,grad(f)[1]==0],[x,y],solution_dict=True)
liste=[]
for critique in Cr:
    x_et_y_reels=(x(critique).imag()==0 and y(critique).imag()==0)
    x_et_y_avec_parametre=(len(x(critique).variables())>0 or len(y(critique).variables())>0)
    if(x_et_y_reels or x_et_y_avec_parametre): 
        liste.append(["(","x","=",x(critique),";","y","=",y(critique),")",H(critique)])        
show(html("<h5>points critiques</h5>"))
if (len(liste)!=0):
    show(table(liste))
else :
    show("pas de points critiques")

# graphique
h(x,y,z)=f(x,y)-z
lasurface=implicit_plot3d(h, (x, xmin,xmax), (y, ymin, ymax), (z, zmin, zmax),
    aspect_ratio=leratio,color=lacouleur,adaptive=True,mesh=True,
    region=laregion)
from sage.plot.plot3d.shapes import Text
lalegende=Text(str(f)).translate(xmin,ymin,zmin)
#lalegende=Text(pretty_print(str(f))).translate(xmin,ymin,zmin)
ledomaine=implicit_plot3d(dom, (x, xmin,xmax), (y, ymin, ymax), (z, zmindom, zmaxdom),
    aspect_ratio=leratio,color=lacouleurdom,adaptive=True,mesh=True,
    region=laregion)
#lignes de niveau :
import numpy as np
pas=(zmax-zmin)/20
leslignes=contour_plot(f, (x,xmin, xmax), (y,ymin, ymax),contours=np.arange(zmin,zmax,pas))

# enfin, les show
if domdom:
    show(lasurface+ledomaine+lalegende)
else :
    show(lasurface+lalegende)
show(leslignes)
# -------------------------------------------
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2022-03-12 20:33:06 +0100

Emmanuel Charpentier gravatar image

updated 2022-03-12 21:01:07 +0100

Unless I'm misunderstanding you, what's wrong with :

def dom(x,y): return 1 if x>0 and y>0 and x+y<4 else 0
region_plot(dom, (-4,4), (-4, 4))

(See Sagecell)

(Note that, since you are working on two variables, you should plot in 2D : There's nothing to specify a third dimension...).

EDIT : Redecyphering your code, I guessed you may mean : "plot z(x, y)=(x*y*(x+y-4))^2+(floor(x/4))^2 subject to x>0, y>0, x+y<4", in which case, what you are looking for is :

plot3d(lambda x,y:(x*y*(x+y-4))^2+(floor(x/4))^2, (0, 4), (0, 4)).add_condition(lambda x, y, z:x+y<4)

(see Sagecell).

(Note that the "condition" function is a three-argument one...).

(Note also that I'm not an haruspex : I'm a dentist...).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-03-12 09:53:13 +0100

Seen: 137 times

Last updated: Mar 12 '22