<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">They are several problem in your code<br>
      <br>
      -  First of all  as you pass vr,vs and x by the context, x is
      redefined some where in the leastsq function<br>
         to avoid the problem you must define fun1 as follow<br>
         function [z]=fun1(const,vr,vs,x)<br>
             ...<br>
         endfunction<br>
      <br>
         and call leastsq with<br>
         [f,ans] = leastsq(list(fun1,vr,vs,x),const0)<br>
      - second <br>
        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.<br>
      <br>
      Regards<br>
      Serge Steer<br>
      <br>
      On 03/02/2017 13:53, David Brant wrote:<br>
    </div>
    <blockquote
cite="mid:VI1P18901MB0096EAA5D49CAB575906FDADD84F0@VI1P18901MB0096.EURP189.PROD.OUTLOOK.COM"
      type="cite">
      <pre wrap="">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.
</pre>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:users@lists.scilab.org">users@lists.scilab.org</a>
<a class="moz-txt-link-freetext" href="http://lists.scilab.org/mailman/listinfo/users">http://lists.scilab.org/mailman/listinfo/users</a>
</pre>
    </blockquote>
    <p><br>
    </p>
  </body>
</html>