[Scilab-users] numderivative Heart function

classic Classic list List threaded Threaded
13 messages Options
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] numderivative Heart function

Hola,
I would like to find the values of the function for which its derivative is
equal to zero.

function val = Heart(x1, x2)
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;

I've tried it with the numderivative function.
Gracias



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Salut,

1. Define: z = H(x, y).
2. Differentiate analytically to obtain the gradient g = (gx,gy) = (dH/dx, dH/dy)
3. Use same contour method as before to plot |g| = 0

PS: as derivative has singularity along x=0; need to define grid appropriately to avoid it.

Bonne chance
Rafael

-----Original Message-----
From: users [mailto:[hidden email]] On Behalf Of Hermes
Sent: Monday, October 02, 2017 9:42 AM
To: [hidden email]
Subject: [Scilab-users] numderivative Heart function

Hola,
I would like to find the values of the function for which its derivative is
equal to zero.

function val = Heart(x1, x2)
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;

I've tried it with the numderivative function.
Gracias



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hi Rafael,
This is how I have reproduced your guidelines:
*========
funcprot(0);
function val = H(x1, x2)
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;
function m=g(x1,x2)

m=[2*x1-(x1.*(1.25*x2-sqrt(abs(x1))))/abs(x1).^(3/2),2.5*(1.25*x2-sqrt(abs(x1)))];  
endfunction

function val = HH(z)
    x1=z(1);
    x2=z(2);
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;
function z=dH(x)  
z=numderivative(HH,x)
endfunction

//test
xx=[0.2 -0.5];
disp(H(xx(1),xx(2)));
disp(g(xx(1),xx(2)));
disp(dH(xx));

x = -1:0.01:1;
g1=g(x,2*x)
contour2d(x, 2*x', g, [0 0]);

replot([-1.415,-1,1.415,1.415]);


//expected values
//x1 -0.5566931 y1 1.2614695
//x2 0.5566931 y2 1.2614695
//x3 0. y3 0.8
//x4 0. y4 -0.8
//plot([x1 x2],[y1 y2],'gd');
//plot([x3 x4],[y3 y4],'rx');


========
*
but in spite of the fact that the help for abs can be read:
"absolute value, magnitude
Syntax
t = abs (x)
Arguments
x real or complex vector or matrix "
I receive the following alert:
  "Function not defined for given argument type (s), check arguments or
define function% function_abs for overloading."

should I define the function mabs = sqrt (x1 ^ 2 + x2 ^ 2);

And using only the function g in contour2d (x, 2 * x ', g, [0 0]) I also
receive the following alert:
---> in builtin contour2d (C: \ PROGRA ~ 1 \ SCILAB ~ 1.0 \ modules \
graphics \ macros \ contour2d.sci line 12)
at line 33 of executed file C: \ Users \ hermesr \ Documents \ Scilab Xcos \
FORUMs \ Heart and numderivative2.sce

g: Wrong size for output argument # 1: A Scalar expected.

Any help, correction or markings



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hello Hermes,

----- Mail original -----
>.../...
>function m=g(x1,x2)
>  m=[2*x1-(x1.*(1.25*x2-sqrt(abs(x1))))/abs(x1).^(3/2),2.5*(1.25*x2-sqrt(abs(x1)))];

.. or ./abs(x1)  // instead?

>.../...
>I receive the following alert:
>  "Function not defined for given argument type (s), check arguments or
>define function% function_abs for overloading."
>
>should I define the function mabs = sqrt (x1 ^ 2 + x2 ^ 2);
>
>And using only the function g in contour2d (x, 2 * x ', g, [0 0]) I also
>receive the following alert:
>---> in builtin contour2d (C: \ PROGRA ~ 1 \ SCILAB ~ 1.0 \ modules \
>graphics \ macros \ contour2d.sci line 12)
>at line 33 of executed file C: \ Users \ hermesr \ Documents \ Scilab Xcos \
>FORUMs \ Heart and numderivative2.sce
>
>g: Wrong size for output argument # 1: A Scalar expected.

I also get with 6.0.0+:
--> contour2d(x, 2*x', g, [0 0]);
in builtin                contour2d ( SCI\modules\graphics\macros\contour2d.sci line 12 )

g: Wrong size for output argument #1: A Scalar expected.
敦慶l: An error occured in '硥捥敆慶䙬' subroutine.


Samuel
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

In reply to this post by Hermes

Hi Hermes,

 

I do not recall your problem and it would help some explanation.

To plot the contours of a gradient with contour2d, you have to make it a scalar (otherwise look at champ, etc.).

Check out the edited code below.

Note that your gradient does not reach 0 and so a contour |g|=5 is plotted.

 

// START OF CODE
clear; clf;
funcprot(0);
function val=H(x1, x2) 
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;
function [gx, gy]=g(x1, x2)
   gx= 2*x1-(x1.*(1.25*x2-sqrt(abs(x1))))./abs(x1).^(3/2);
   gy = 2.5*(1.25*x2-sqrt(abs(x1)));   
endfunction
 
function val=HH(z) 
    x1=z(1); 
    x2=z(2); 
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction; 
 
function z=dH(x)  
    z=numderivative(HH,x) 
endfunction 
 
//test 
x0=[0.2 -0.5];
disp(H(x0(1),x0(2)));
[gx,gy]=g(x0(1),x0(2))
disp(gx,gy);
disp(dH(x0));
 
x = linspace(-1,1,1002);
y = 2*x';
xx = x.*.ones(x');
yy = y.*.ones(y');
[gx,gy] = g(xx,yy);
g1= abs(gx + %i*gy);
contour2d(x, y, g1, [5 5]);
 
replot([-1.415,-1,1.415,1.415]);
// END OF CODE
 

 

 

Regards,

Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Help me,
why the same function with different procedures to determine the critical
points; these differ in the results.
What am I not doing correctly or interpreted?
*
function val = Heart(x1, x2)
    val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1;
endfunction;


function y = heart_up(x1)
    y = 4/5 * (sqrt(abs(x1))+sqrt(1-x1.^2))
endfunction
function y = heart_down(x1)
    y = 4/5 * (sqrt(abs(x1))-sqrt(1-x1.^2))
endfunction

function y = du_heart(x1)
    y = numderivative(heart_up, x1);
endfunction
function y = dd_heart(x1)
    y = numderivative(heart_down, x1);
endfunction

x1 = fsolve(-0.6, du_heart); y1 = heart_up(x1);disp(y1,"y1",x1,"x1");
x2 = fsolve( 0.6, du_heart); y2 = heart_up(x2);disp(y2,"y2",x2,"x2");
x3 = fsolve( 0.0, du_heart); y3 = heart_up(x3);disp(y3,"y3",x3,"x3");
x4 = fsolve( 0.0, dd_heart); y4 = heart_down(x4);disp(y4,"y4",x4,"x4");

x = -1:0.01:1;
contour2d(x, 2*x, Heart, [0 0]);
plot([x1 x2],[y1 y2],'gd');
plot([x3 x4],[y3 y4],'rx');
replot([-1.415,-1,1.415,1.415]);
*




--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hi Hermes,

 

I think you may have a Heart problem:

 

The two Hearts do not seem to match.

 

Regards,

Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hello; in this way we can find the extreme or critical points of the Heart function, in Maple: Maple Heart solution function val = Heart(x1, x2) val=(1.25*x2-sqrt(abs(x1))).^2+x1.^2-1; endfunction; First function resolved with respect to Y: function y = heart_up(x1) y = 4/5 * (sqrt(abs(x1))+sqrt(1-x1.^2)) endfunction function y = heart_down(x1) y = 4/5 * (sqrt(abs(x1))-sqrt(1-x1.^2)) endfunction I would like to arrive at the same solution by numerical method, Scilab!

Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com.

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

In reply to this post by Hermes
I wish in this case that x passes as a parameter:

function val = Hxy(x,y)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; // switched .^ to ^ to handle
vectors
endfunction;

x=0.0;
yint1=fsolve(1,list(Hxy,x))//// ---> (25/16)*y^2-1
disp(yint1)
yint2=fsolve(-1,list(Hxy,x))
disp(yint2)
the expected value is 4/5 and all the time I receive 0.68034

What incorrect logic am I assuming?
Please, I would like to put an end to this ("Heart") !!!!
Gracias



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hello,

As y is your unknown, you have interverted x and y in the arguments list of Hxy function (y is the unknown and x the parameter) :

function val = Hxy(y,x)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; // switched .^ to ^ to handle vectors
endfunction;

will dot it.

S.

Le 07/12/2017 à 18:29, Hermes a écrit :
function val = Hxy(x,y)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; // switched .^ to ^ to handle
vectors
endfunction;

x=0.0;
yint1=fsolve(1,list(Hxy,x))//// ---> (25/16)*y^2-1
disp(yint1)
yint2=fsolve(-1,list(Hxy,x))
disp(yint2)


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hi,
I have interverted x and y in the arguments list of Hxy function (y is the
unknown and x the parameter)
And I get the expected values. But the graph of the function rotates to the
right 90 degrees. How to correct it?
<http://mailinglists.scilab.org/file/t497622/Ventana_de_gr%C3%A1fico_n%C3%BAmero_1.png>



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Hi Hermes,

 

Very confusing problem formulation…

To find solutions of Hxy(x,y) with fsolve along the x=0 slice, it might be easier to use a sliced function H0y as input:

 

clear all;
 
function val=Hxy(x, y)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; 
endfunction;
 
function z=H0y(y)   // slice of Hxy along x=0
    x=0;
    z= Hxy(x, y);
endfunction;
 
y1=fsolve(1,H0y);
disp(y1)
y2=fsolve(-1,H0y);
disp(y2)

 

    0.8 

 

  - 0.8 

 

Regards,

Rafael


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: numderivative Heart function

Thanks Rafael,
with your solution the final script, it seems to me more elegant, and it is
of the following form:
*
funcprot(0);
function val = Hxy(x,y)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1;
endfunction;
x = -1:0.01:1;
contour2d(x, 2*x, Hxy, [0 0]);
replot([-1.415,-1,1.415,1.415]);

function Jpx=dfx(v)  //v:(x,y)
Jpx=numderivative(Hxy,v);
Jpx=Jpx(1);
endfunction

//Find the local maxima.
function Z = flm(v)//v:(y,x)
    x=v(1);y=v(2);
   Z = [ Hxy(x,y)
         dfx(v)
       ]
endfunction
//v:(x,y)
xlm1=fsolve([-0.4;1.4],flm)
disp(xlm1)
xlm2=fsolve([0.4;1.4],flm)
disp(xlm2)
plot([xlm1(1) xlm2(1)],[xlm1(2) xlm2(2)],'gx')

//Find the y-intercepts. Hxy(x,y)
//To find solutions of Hxy(x,y) with fsolve along the x=0 slice, it might be
easier to use a sliced function H0y as input:
function z=H0y(y)   // slice of Hxy along x=0
    x=0;
    z= Hxy(x,y);
endfunction;

yint1=fsolve(1,H0y)// --->Hxy(y,x)=>1.5625*y^2-1
disp(yint1)
yint2=fsolve(-1,H0y)// ---> Hxy(y,x)=>1.5625*y^2-1
disp(yint2)
x=0;
plot([x x],[yint1 yint2],'rx')

*
Once again Thanks to you and all those who with their comments have
initiated me in SCILAB.

Before your solution I had written the following script, with the same
results:

*
function val = Hxyg(x,y)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1;
endfunction;
x = -1:0.01:1;
contour2d(x, 2*x, Hxyg, [0 0]);
replot([-1.415,-1,1.415,1.415]);

//We define the Hxy function with x as additional input argument( as a
parameter)
//Since y, will be our unknown, we have interposed x and y in the argument
list of the Hxy function
//(y it is the unknown and x is the parameter).
//This will be necessary to determine the intersects on the Y axis
function val = Hxy(y,x)
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; // switched .^ to ^ to handle
vectors
endfunction;

function Jpx=dfx(v)//v:(y,x)
Jpx=numderivative(Hxy,v);
Jpx=Jpx(2);
endfunction

//Find the local maxima.
function Z = flm(v)//v:(y,x)
   Z = [ HH(v)
         dfx(v)
       ]
endfunction
//v:(y,x)
xlm1=fsolve([1.4;-0.4],flm)
disp(xlm1)
xlm2=fsolve([1.4;0.4],flm)
disp(xlm2)
plot([xlm1(2) xlm2(2)],[xlm1(1) xlm2(1)],'gx')

//Find the y-intercepts. Hxy(y,x)
//the Hxy function uses x as a parameters.
//We define the Hxy function with x as additional input argument( as a
parameter),
//which is declared after the y unknown. Then we pass a list as the second
input argument
//of the  fsolve function after initial value of  Hxy function argument; in
this situation only  variable y.
//The first element of the list is the Hxy function. The additional variable
x is directly passed to the Hxy function.
x=0.0;
yint1=fsolve(1,list(Hxy,x))// --->Hxy(y,x)=>1.5625*y^2-1
disp(yint1)
yint2=fsolve(-1,list(Hxy,x))// ---> Hxy(y,x)=>1.5625*y^2-1
disp(yint2)
plot([x x],[yint1 yint2],'rx')

*
Note, I still can not put the codes the way you do. my email is in HTML
format

10^6 Gracias!!!



--
Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users