[Scilab-users] Problem with function fitting
Hi, I have problem with fitting function to measured data using scilab. I tried scilab function datafit(), lsqrsolve(), leastsq() but none returns me a proper result or can't perform calculations. //I have measured data: xm;ym 2.0;99.9449173761 1.0;99.8097145719 0.5;97.9769654482 0.25;36.4046069104 0.1;1.4872308463 0.071;0.5207811718 0.063;0.3705558338 //Script used to fit data miny=min(ym); maxy=max(ym); minx=min(xm); maxx=max(xm); Zz=[xm;ym]; //Function describing problem (I want to find best fit by finding n()). function g1=f1(x, n) g1 = miny+((maxy-miny) ./((1+(n(1).*(x ./n(2)).^n(3))+(1-n(1)).*((x ./n(2)).^n(4.^n(5)) endfunction function e = G(n,z) xm = z(1) ym = z(2) e = ym - f1(xm,n) endfunction n0 = [1 ; 1; 1; 1; 1] [n0_opt,err] = datafit(G,Zz,n0) After calling function scilab returns me error: Variable returned by scilab argument function is incorrect. I tried to find solution for this problem in scilab mailing lists and tried some posted scripts and advices but I can't localize problem. Łukasz -- View this message in context: http://mailinglists.scilab.org/Problem-with-function-fitting-tp4031546.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Problem with function fitting
Your problem comes from the fact that for some values of n the computed value of e are complex values (some negative values to a non integer exponent. This can be made obvious with the following function function e = G(n,z) xm = z(1) ym = z(2) e = ym - f1(xm,n) if ~isreal(e) then pause,end endfunction The solution depends on the problem you want to solve. for example one can replage G by function e = G(n,z) xm = z(1) ym = z(2) e = ym - f1(xm,n) e=real(e*e') endfunction Serge Steer Le 17/12/2014 18:53, Maihem a écrit : Hi, I have problem with fitting function to measured data using scilab. I tried scilab function datafit(), lsqrsolve(), leastsq() but none returns me a proper result or can't perform calculations. //I have measured data: xm;ym 2.0;99.9449173761 1.0;99.8097145719 0.5;97.9769654482 0.25;36.4046069104 0.1;1.4872308463 0.071;0.5207811718 0.063;0.3705558338 //Script used to fit data miny=min(ym); maxy=max(ym); minx=min(xm); maxx=max(xm); Zz=[xm;ym]; //Function describing problem (I want to find best fit by finding n()). function g1=f1(x, n) g1 = miny+((maxy-miny) ./((1+(n(1).*(x ./n(2)).^n(3))+(1-n(1)).*((x ./n(2)).^n(4.^n(5)) endfunction function e = G(n,z) xm = z(1) ym = z(2) e = ym - f1(xm,n) endfunction n0 = [1 ; 1; 1; 1; 1] [n0_opt,err] = datafit(G,Zz,n0) After calling function scilab returns me error: Variable returned by scilab argument function is incorrect. I tried to find solution for this problem in scilab mailing lists and tried some posted scripts and advices but I can't localize problem. Łukasz -- View this message in context: http://mailinglists.scilab.org/Problem-with-function-fitting-tp4031546.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Problem with function fitting
Thanks for answer but it seems that implementation of this only allowed to avoid error cause calculated parameters still don't fit to data. Parameters calculated by implementation real on e gives fit parameters: n(1)= 1.264264 n(2)= - 0.1891816 n(3)= 1.6592188 n(4)= 0.9977104 n(5)= 1.0404815 but this parameters are wrong. For example I fitted this equation using Gnuplot and I recived: n(1)= 0.960002 n(2)= 0.331008 n(3)= -8.26945 n(4)= -1.9777 n(5)= 0.423854 and this results from gnuplot fits this function to data I'm little confused why I can't receive similar solution from scilab Łukasz -- View this message in context: http://mailinglists.scilab.org/Problem-with-function-fitting-tp4031546p4031549.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users
Re: [Scilab-users] Problem with function fitting
Thanks for help, now it looks fine Łukasz -- View this message in context: http://mailinglists.scilab.org/Problem-with-function-fitting-tp4031546p4031551.html Sent from the Scilab users - Mailing Lists Archives mailing list archive at Nabble.com. ___ users mailing list users@lists.scilab.org http://lists.scilab.org/mailman/listinfo/users