# [Scilab-users] evaluate matrix in a function

10 messages
Open this post in threaded view
|

## [Scilab-users] evaluate matrix in a function

 how do I declare the functions to be able to evaluate a matrix variable(Multiple evaluation of a function). Where the first column corresponds to the first variable of the function. And so on. is only possible within a "for" cycle? how to declare the function to be able to use the operator "dot" r=[3 1;4 2;3 5;8 2]; function val = H(z)     x=z(1);     y=z(2);     val=(1.25*y-sqrt(abs(x)))^2+x^2-1; endfunction; function Sys=g(x)   gx=x(1);   gy=x(2);   g1=2*gx-(gx*(1.25*gy-sqrt(abs(gx))))/abs(gx)^(3/2); g2=2.5*(1.25*gy-sqrt(abs(gx)));   Sys(:,1)=g1;   Sys(:,2)=g2; endfunction V=g(r)'; disp(V,"g"); J = diffcode_jacobian(H,r); disp(J, "Jacobian"); Gracias Hermes -- 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: evaluate matrix in a function

 Your code has the solution (for the matrix function output). Do the same for input:   r=[3 1;4 2;3 5;8 2];   function val=H(z)     x=z(:,1);     y=z(:,2);     val=(1.25*y-sqrt(abs(x))).^2 + x.^2 - 1; endfunction;   Regards, Rafael   -----Original Message----- From: users [mailto:[hidden email]] On Behalf Of Hermes Sent: Saturday, October 07, 2017 1:05 PM To: [hidden email] Subject: [Scilab-users] evaluate matrix in a function   how do I declare the functions to be able to evaluate a matrix variable(Multiple evaluation of a function). Where the first column corresponds to the first variable of the function. And so on. is only possible within a "for" cycle? how to declare the function to be able to use the operator "dot"   r=[3 1;4 2;3 5;8 2];   function val = H(z)     x=z(1);     y=z(2);     val=(1.25*y-sqrt(abs(x)))^2+x^2-1; endfunction;     function Sys=g(x)   gx=x(1);   gy=x(2);  g1=2*gx-(gx*(1.25*gy-sqrt(abs(gx))))/abs(gx)^(3/2); g2=2.5*(1.25*gy-sqrt(abs(gx)));   Sys(:,1)=g1;   Sys(:,2)=g2; endfunction   V=g(r)'; disp(V,"g"); J = diffcode_jacobian(H,r); disp(J, "Jacobian");   Gracias Hermes   _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: evaluate matrix in a function

 Hello The H function works. the other two evaluations I can not solve the problem. function val = H(z)     x=z(:,1);     y=z(:,2);     val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; endfunction; function Sys=g(x)   gx=x(:,1);   gy=x(:,2);   g1=2*gx-(gx  *(1.25*gy-sqrt(abs(gx))))/abs(gx).^(3/2); g2=2.5*(1.25*gy-sqrt(abs(gx)));   Sys(:,1)=g1;   Sys(:,2)=g2; endfunction r=[3 1;4 2;3 5;8 2] disp(H(r),"H"); H    8.232373    15.25    28.411865    63.107864 V=g(r'); disp(V,"g"); J = diffcode_jacobian(H,r); disp(J, "Jacobian"); at line     4 of function g ( E:\Heart Curve8.sce line 28 ) at line    36 of executed file E:\Heart Curve8.sce *Inconsistent row/column dimensions. * 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: evaluate matrix in a function

 Hi, It seems you are missing a few dots in g1, try: g1=2*gx-(gx  .*(1.25*gy-sqrt(abs(gx))))./abs(gx).^(3/2); Also, I believe you should also remove the transposed input in V=g(r'), and make it  V=g(r); Rafael and I believe your -----Original Message----- From: users [mailto:[hidden email]] On Behalf Of Hermes Sent: Saturday, October 07, 2017 6:44 PM To: [hidden email] Subject: Re: [Scilab-users] evaluate matrix in a function Hello The H function works. the other two evaluations I can not solve the problem. function val = H(z)     x=z(:,1);     y=z(:,2);     val=(1.25*y-sqrt(abs(x))).^2+x.^2-1; endfunction; function Sys=g(x)   gx=x(:,1);   gy=x(:,2);   g1=2*gx-(gx  *(1.25*gy-sqrt(abs(gx))))/abs(gx).^(3/2); g2=2.5*(1.25*gy-sqrt(abs(gx)));   Sys(:,1)=g1;   Sys(:,2)=g2; endfunction r=[3 1;4 2;3 5;8 2] disp(H(r),"H"); H    8.232373    15.25    28.411865    63.107864 V=g(r'); disp(V,"g"); J = diffcode_jacobian(H,r); disp(J, "Jacobian"); at line     4 of function g ( E:\Heart Curve8.sce line 28 ) at line    36 of executed file E:\Heart Curve8.sce *Inconsistent row/column dimensions. * Gracias _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: evaluate matrix in a function

 Hello The H and g functions work. But the diffcode_jacobian (H, r) evaluation does not accept the dot operator. Only redefining the H function (without dot operators) will achieve the results function val = HH(z)     x=z(1);     y=z(2);     val=(1.25*y-sqrt(abs(x)))^2+x^2-1; // switched *.^* to *^* endfunction; t=[3. 1.] disp(diffcode_jacobian(HH,t)); -->6.2783122   -1.205127 J=[]; for k=1:size(r,"r")     J(k,:)=diffcode_jacobian(HH,r(k,:));   end disp(J, "Jacobian"); -->Jacobian    6.2783122  -1.205127    7.75            1.25        3.3915608   11.294873    16.116117  -0.8210678 disp(and(J==g)) -->F   Why? The results of J and g look the same !! g    6.2783122  -1.205127    7.75             1.25        3.3915608   11.294873    16.116117  -0.8210678      J    6.2783122  -1.205127    7.75            1.25        3.3915608   11.294873    16.116117  -0.8210678 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: evaluate matrix in a function

 diffcode_jacobian does not seem to accept multiple input points, but only one N-dimension point X:   // Calling Sequence:   //   J = diffcode_jacobian(f,x)   //  x : a n-by-1 matrix of doubles, real, the point where to compute the derivatives You cannot compare g and J, because the former is a function and the latter has type constant. However you can compare J and V but the answer is false because of tiny numerical difference: J==V  ans  =   T T     T T     F T     T T   J-V  ans  =     0.           0.       0.           0.     - 4.441D-16    0.       0.           0. Rgds, Rafael -----Original Message----- From: users [mailto:[hidden email]] On Behalf Of Hermes Sent: Saturday, October 07, 2017 8:10 PM To: [hidden email] Subject: Re: [Scilab-users] evaluate matrix in a function Hello The H and g functions work. But the diffcode_jacobian (H, r) evaluation does not accept the dot operator. Only redefining the H function (without dot operators) will achieve the results function val = HH(z)     x=z(1);     y=z(2);     val=(1.25*y-sqrt(abs(x)))^2+x^2-1; // switched *.^* to *^* endfunction; t=[3. 1.] disp(diffcode_jacobian(HH,t)); -->6.2783122   -1.205127 J=[]; for k=1:size(r,"r")     J(k,:)=diffcode_jacobian(HH,r(k,:));   end disp(J, "Jacobian"); -->Jacobian    6.2783122  -1.205127    7.75            1.25        3.3915608   11.294873    16.116117  -0.8210678 disp(and(J==g)) -->F   Why? The results of J and g look the same !! g    6.2783122  -1.205127    7.75             1.25        3.3915608   11.294873    16.116117  -0.8210678      J    6.2783122  -1.205127    7.75            1.25        3.3915608   11.294873    16.116117  -0.8210678 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: evaluate matrix in a function

 In reply to this post by Hermes Hi, Le 07/10/2017 à 13:05, Hermes a écrit : > how do I declare the functions to be able to evaluate a matrix > variable(Multiple evaluation of a function). Where the first column > corresponds to the first variable of the function. And so on. > is only possible within a "for" cycle? how to declare the function to be > able to use the operator "dot" I'm not sure of what you are asking for. It looks like you want to evaluate a function f on the curve defined by 2 vectors x,y  instead of evaluating it on the grid defined by x,y (which is done by feval). If this is  what you are searching for  you can use something like feval2 below : function z=feval2(x,y,f)     //  eval f on the curve t->(x(t),y(t))     // and on the grid defined by x,y     deff('z=newf(k)','z=f(x(k),y(k))')     // x,y are global variables for the "newf" function     z=feval(1:length(x),newf)     // "newf" is a local variable for "feval2" endfunction then try the example  : -->deff('z=f(x,y)','z=x.^2-y.^2') // test function -->x=-3:3;y=x; -->z=feval(x,y,f)// grid evaluation  z  =     0.    5.    8.    9.    8.    5.    0.   - 5.    0.    3.    4.    3.    0.  - 5.   - 8.  - 3.    0.    1.    0.  - 3.  - 8.   - 9.  - 4.  - 1.    0.  - 1.  - 4.  - 9.   - 8.  - 3.    0.    1.    0.  - 3.  - 8.   - 5.    0.    3.    4.    3.    0.  - 5.     0.    5.    8.    9.    8.    5.    0. -->z=feval2(x,y,f) // curve evaluation  z  =     0.    0.    0.    0.    0.    0.    0. Best regards Philippe _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: evaluate matrix in a function

 Right, and if one follows the thread till the end, how does that apply to diffcode_jacobian ? -----Original Message----- From: users [mailto:[hidden email]] On Behalf Of philippe Sent: Tuesday, October 10, 2017 8:56 AM To: [hidden email] Subject: Re: [Scilab-users] evaluate matrix in a function Hi, Le 07/10/2017 à 13:05, Hermes a écrit : > how do I declare the functions to be able to evaluate a matrix > variable(Multiple evaluation of a function). Where the first column > corresponds to the first variable of the function. And so on. > is only possible within a "for" cycle? how to declare the function to be > able to use the operator "dot" I'm not sure of what you are asking for. It looks like you want to evaluate a function f on the curve defined by 2 vectors x,y  instead of evaluating it on the grid defined by x,y (which is done by feval). If this is  what you are searching for  you can use something like feval2 below : function z=feval2(x,y,f)     //  eval f on the curve t->(x(t),y(t))     // and on the grid defined by x,y     deff('z=newf(k)','z=f(x(k),y(k))')     // x,y are global variables for the "newf" function     z=feval(1:length(x),newf)     // "newf" is a local variable for "feval2" endfunction then try the example  : -->deff('z=f(x,y)','z=x.^2-y.^2') // test function -->x=-3:3;y=x; -->z=feval(x,y,f)// grid evaluation  z  =     0.    5.    8.    9.    8.    5.    0.   - 5.    0.    3.    4.    3.    0.  - 5.   - 8.  - 3.    0.    1.    0.  - 3.  - 8.   - 9.  - 4.  - 1.    0.  - 1.  - 4.  - 9.   - 8.  - 3.    0.    1.    0.  - 3.  - 8.   - 5.    0.    3.    4.    3.    0.  - 5.     0.    5.    8.    9.    8.    5.    0. -->z=feval2(x,y,f) // curve evaluation  z  =     0.    0.    0.    0.    0.    0.    0. Best regards Philippe _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users