[Scilab-users] evaluate matrix in a function

classic Classic list List threaded Threaded
10 messages Options
Hermes Hermes
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
Hermes Hermes
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
Hermes Hermes
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
Philippe Roux Philippe Roux
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: evaluate matrix in a function

In reply to this post by Philippe Roux
Hi Philippe:
Where this error?

// The function to differentiate
function val = HH(z)
    x=z(1);
    y=z(2);
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1;
endfunction;
// The exact gradient
function Sys=ge(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
// Compute the approximate Jacobian
t=[3,1]; //for function f, which does not allow multiple inputs [x1,1 x2,1
x3,1; ...; x1, n x2, n x3, n]
t1=[3,1;3,1];//for function f,  allow multiple inputs [x1,1 x2,1 x3,1; ...;
x1, n x2, n x3, n]

*// test function*
function z=f(x)
z=diffcode_jacobian(HH,x) endfunction

disp(f(t)',ge(t1),HH(t))

function z=feval2(x,y,g)
    //  eval f on the curve t->(x(t),y(t))
    // and on the grid defined by x,y
    deff('z=newf(k)',['a=[x(k),y(k)]';'z=g(a)'])
    // x,y are global variables for the "newf" function
    z=feval(1:length(x),newf)
    // "newf" is a local variable for "feval2"
endfunction
// Compute the approximate Jacobian
x=-3:3;y=x;

z=feval2(x,y,f) // curve evaluation
disp(z ," feval2");

*in builtin                
feval2 ( C: \Curve11.sce line 47 )
newf: Wrong size for output argument #1: A Scalar expected.
敦慶l: An error occured in '硥捥敆慶䙬' subroutine.
*
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
Hermes Hermes
Reply | Threaded
Open this post in threaded view
|

Re: evaluate matrix in a function

In reply to this post by Hermes
A possible solution,
we compare "diffcode_jacobian" and "numderivative". in this case seem to
calculate with the same accuracy

function val = HH(z)
    x=z(1);
    y=z(2);
    val=(1.25*y-sqrt(abs(x))).^2+x.^2-1;
endfunction;

function z=f(x)
z(1,1:2)=diffcode_jacobian(HH,x)
z(1,3:4)=numderivative(HH,x)
endfunction

xt = -1.4:0.01:1.4;

function Z=g(X,Y,f)

  for i=1:size(X,"*")
   T=[X(i) Y(i)];
   Z(i,:)=f(T);
  end
endfunction
//And for 280 values calculate fast
F=g(xt,xt)



--
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