# [Scilab-users] numderivative Heart function

13 messages
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
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
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
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
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
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
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
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: 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
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
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
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? -- 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
 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