How plot heart curve in scilab?

2019-09-15 15:39发布

问题:

I have been looking in the forum and within the graphical functions of the program help, how to graph the following function:

x2=[-2:0.02:2]';x1=[-1:0.01:1]';
function val = Heart(x1, x2) 
    val=(1.2*x2-sqrt(abs(x1)))^2+x1^2-1;
endfunction 

And I have not found anything to guide me. I try to reproduce this maple plot: Heart curve

回答1:

Since your defining a 3D surface, you can use contour and contour2d: as said by luispauloml, you can pass the function directly as a parameter.

x1=[-1:0.01:1]
x2=[-2:0.02:2]
function val = Heart(x1, x2) 
    val=(1.2*x2-sqrt(abs(x1))).^2+x1.^2-1; // switched ^ to .^ to handle vectors
endfunction;

figure()
xlabel('x1')
ylabel('x2')

contour2d(x1,x2,Heart,[0 0]); 

a=gca()
hline=a.children.children(1)
hline.foreground=color('red')
hline.thickness=2



回答2:

an other solution is to solve the equation (1.2*x2-sqrt(abs(x1)))^2+x1^2-1 = c for x2 The solution is quite easy and one can found 2 branches x2=(5/6)(sqrt(abs(x1))+sqrt(c+1-x1^2)); and x2=(5/6)(sqrt(abs(x1))-sqrt(c+1-x1^2));

c=0;
x1=linspace(-1,1,200);
x1r=x1($:-1:1);
x2=(5/6)*[ (sqrt(abs(x1))+sqrt(c+1-x1^2)) (sqrt(abs(x1r))-sqrt(c+1-    x1r^2))];
clf;plot([x1 x1r],x2);