[Scilab-users] Help with leastsq

Steer Serge Serge.Steer at laposte.net
Fri Feb 3 14:48:55 CET 2017


They are several problem in your code

-  First of all  as you pass vr,vs and x by the context, x is redefined 
some where in the leastsq function
    to avoid the problem you must define fun1 as follow
    function [z]=fun1(const,vr,vs,x)
        ...
    endfunction

    and call leastsq with
    [f,ans] = leastsq(list(fun1,vr,vs,x),const0)
- second
   for some value of const the value returned by fun1 is complex (it 
seems tht the imaginary parts are zeros so adding z=real(z) at the end 
of fun1 solves the problem.

Regards
Serge Steer

On 03/02/2017 13:53, David Brant wrote:
> 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.
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20170203/7cbc410d/attachment.htm>


More information about the users mailing list