[Scilab-users] Help with leastsq

David Brant brantosaurus at hotmail.com
Fri Feb 3 13:53:34 CET 2017


Hi, i am having problems with the below code (file attached).

The function call appears to return the correct differences(Z), but 
falls over at the leastsq call to optimize const.
I understand what the error is implying and have solved similar 
instances many times before, but not this one!

Any help or advice would be very much appreciated.
Regards, Dave


Code:
function [z]=fun1(const)
     q=4*const(1)*const(3)-const(2)^2
     sqrtq=sqrt(q)
     e1=const(1)*vs.^2+const(2)*vs+const(3)
     e2=const(1)*vr.^2+const(2)*vr+const(3)
     e3=2*const(1)*vs+const(2)
     e4=2*const(1)*vr+const(2)
     var0=e3./sqrtq
     var1=e4./sqrtq
     t = 2/sqrtq*( atan(var0) - atan(var1) )
     var2=e1./e2
     x1=x*ones(vs)
     z = (log(var2) - const(2)*t)/2/const(1) - x1
disp(z,'z',x1,'x1',var2,'var2',t,'t',var1,'var1',var0,'var0',e4,'e4',e3,'e3',e2,'e2',e1,'e1',sqrtq,'sqrtq',q,'q',const,'const') 

endfunction

vsr=[    250    90;
         500    180;
         750    275;
         1000    365;
         1250    450;
         1500    550;
         1750    640;
         2000    730]
vs=vsr(:,1)
vr=vsr(:,2)
x=0.2
a0=5
b0=1
c0=0.2
const0=[a0 b0 c0];

[Z]=fun1(const0)
disp(Z,'Z')
[f,ans] = leastsq(fun1,const0)
disp(f,'f',ans,'ans')


Response:
  Z

     0.0040458
     0.0041880
     0.0005683
     0.0015020
     0.0042734
     0.0006144
     0.0011409
     0.0015368
  !--error 10
Inconsistent multiplication.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: problem.sce
Type: application/x-scilab
Size: 786 bytes
Desc: problem.sce
URL: <https://lists.scilab.org/pipermail/users/attachments/20170203/82b73b6d/attachment.bin>


More information about the users mailing list