[scilab-Users] using optim function in different versions of Scilab

Yann Collette yann.collette at scilab.org
Thu Jun 25 10:38:35 CEST 2009


Hello,

I tested your script and the only problem I saw was:

function [yN]=fun(x)
  yN= -(2*real(trace(((expm(-%i*dt*HAMo)'*expm(-%i*dt*(x(1)*ix+x(2)*iy+x
	(3)*sx+x(4)*sy))- eye(4,4))*A*U*B*A') ))-dt*l1*((x(1)-x1(j-1)).*
	(x(1)+x1 (j-1)) +(x(2)-x2(j-1)).*(x(2)+x2(j-1))+(x(3)-x3(j-1)).*
	(x(3)+x3 (j-1)) +(x(4)-x4 (j-1)).* (x(4)+x4(j-1)))); 
endfunction

Which has been changed into

function [yN]=fun(x)
  yN= -(2*real(trace(((expm(-%i*dt*HAMo)'*expm(-%i*dt*(x(1)*ix+x(2)*iy+x ...
	(3)*sx+x(4)*sy))- eye(4,4))*A*U*B*A') ))-dt*l1*((x(1)-x1(j-1)).* ...
	(x(1)+x1 (j-1)) +(x(2)-x2(j-1)).*(x(2)+x2(j-1))+(x(3)-x3(j-1)).* ...
	(x(3)+x3 (j-1)) +(x(4)-x4 (j-1)).* (x(4)+x4(j-1)))); 
endfunction

There was no warning under scilab master, but I don't know was is the 
good result:

-->exec prop.sce;
BEFORE: 0.061004
AFTER: -0.345703
BEFORE: -0.011493
AFTER: -0.476168
BEFORE: -0.045253
AFTER: -0.350020
BEFORE: -0.028446
AFTER: -0.436120
BEFORE: 0.020816
AFTER: -0.116581
BEFORE: -0.051276
AFTER: -0.532219
BEFORE: -0.007288
AFTER: -0.149870
BEFORE: 0.076331
AFTER: -0.340303
BEFORE: 0.016214
AFTER: -0.676543
 
YC


Ivan Maximov a écrit :
> Hi everyone!
>
> I have a question what is confusing me. I use Scilab 4.1.2 under Linux (Fedora
> 10) and try to set up a optim function in the code below - but always get an
> error message. However, in Scilab 5.1.1 it works without any messages (the
> question does it work properly is still open). Could someone, please, explain
> for me what is a problem in optim application in my code? 
>
> Thanks a lot in advance.
> Ivan
>
> //-----------------------------------------------------------------------------------------------
> funcprot(0);
>
> function [y1,y2,y3,y4]=prop(x1,x2,x3,x4,ix,iy,sx,sy,dt,A,U,B,PROP,l1)
>
>   y1 = x1;
>   y2 = x2;
>   y3 = x3;
>   y4 = x4;
>
>   N=length(x1)+1;
>
>   for j=2:N
>     
>     HAMo = rand(4,4);
>     B=rand(4,4);
>     U=rand(4,4);
>
>       function [yN]=fun(x)
>         yN= -(2*real(trace(((expm(-%i*dt*HAMo)'*expm(-%i*dt*(x(1)*ix+x(2)*iy+x
> 		(3)*sx+x(4)*sy))- eye(4,4))*A*U*B*A') ))-dt*l1*((x(1)-x1(j-1)).*
> 		(x(1)+x1 (j-1)) +(x(2)-x2(j-1)).*(x(2)+x2(j-1))+(x(3)-x3(j-1)).*
> 		(x(3)+x3 (j-1)) +(x(4)-x4 (j-1)).* (x(4)+x4(j-1)))); 
>      endfunction
>     function [d_yN] =d_fun(x) 
> 	d_yN=derivative (fun,x); 
>     endfunction
>       
>     function [yN,grd,ind]=f(x,ind)
>         yN=fun(x);
>         grd=d_fun(x);
>     endfunction
> 	   
>     x0= [x1(j-1);x2(j-1);x3(j-1);x4(j-1)];
>    printf("BEFORE: %f\n",fun(x0));
> 	 [fopt,xN]=optim(f,x0);
>   printf("AFTER: %f\n",fun(xN));
> 	 y1(j-1)	= xN(1);
> 	 y2(j-1)	= xN(2);
> 	 y3(j-1)	= xN(3);
> 	 y4(j-1)	= xN(4);
> 	  
>   end
>
> endfunction
>
> x_pauli=[0,0.5;0.5,0]; 
> y_pauli=[0,-0.5*%i;0.5*%i,0]; 
> z_pauli=[0.5,0;0,-0.5];
> u_pauli=[1,0;0,1];
>
> J = 140;
> T = 1/J;  N=10;  NIT=5;
> dt=T/N;
> rf = 100;
> l1 = 1e-4;
>
> //---------------------------------------------------------------------------
> ix = x_pauli.*.u_pauli; iy = y_pauli.*.u_pauli; iz = z_pauli.*.u_pauli;
> sx = u_pauli.*.x_pauli; sy = u_pauli.*.y_pauli; sz = u_pauli.*.z_pauli;
> //---------------------------------------------------------------------------
> Hamilt=2*%pi*J*sz*iz;
> A=expm(-%i*0.5*dt*Hamilt);
>
> U = rand(4,4);
> B = rand(4,4);
> PROP=rand(4,4);
> //---------------------------------------------------------------------------
> u1 = rf*rand(1,N-1); u2 = rf*rand(1,N-1);
> u3 = rf*rand(1,N-1); u4 = rf*rand(1,N-1);
>
> [y1,y2,y3,y4]=prop(u1,u2,u3,u4,ix,iy,sx,sy,dt,A,U,B,PROP,l1);
>
>   




More information about the users mailing list