Hi, João,

The "y = 1 * sin (x * a2-a3)" is a harmonic function, so the coefficients 
returning from the function call, depend heavily on the parameter a0 
("initial guess for each fitting parameter") you will send as the third 
parameter of the nonlinear_fit function (with maxiter=200_000):

a0=[1.5, 1.5, 1.0]  
coefficients: [0.2616335317043578, 1.1471991302529982,0.7048665905560775]

a0=[100.,100.,100.]  
coefficients: [-0.4077952060368059, 90.52328921205392, 96.75331155303707]

a0=[1.2, 0.5, 0.5]  
coefficients: [1.192007321713507, 0.49426296880933257, 0.19863645732313934]

I think the results you're getting are harmonics, as the graph (posted on 
StackOverflow):

http://i.stack.imgur.com/9WZSJ.png

Where:

- blue line:  
f1(xx)=0.2616335317043578*sin(xx*1.1471991302529982-0.7048665905560775)

- yellow line:
f2(xx)=1.192007321713507*sin(xx*0.49426296880933257-0.19863645732313934)

- pink line:
f3(xx)=-0.4077952060368059*sin(xx*90.52328921205392-96.75331155303707)

- blue dots are your initial data.

The graph was generated with Gadfly:

plot(layer(x=x,y=y,Geom.point),layer([f1,f2,f3],0.0, 15.0,Geom.line))

Since nonlinar_fit uses a Newton like algorithm to find the coefficients 
and the function is harmonic (f and f' are not continuous on the data's 
interval)  the coefficients depend on the initial parameters.

The code:

using CurveFit

x = 
[0.0,0.2,0.4,1.0,1.6,1.8,2.0,2.6,2.8,3.0,3.8,4.8,5.0,5.2,6.0,6.2,7.4,7.6,7.8,8.6,8.8,9.0,9.2,9.4,10.0,10.6,10.8,11.2,11.6,11.8,12.2,12.4];
y = [-0.183, -0.131, 0.027, 0.3, 0.579, 0.853, 0.935, 1.133, 1.269, 1.102, 
1.092, 1.143, 0.811, 0.91, 0.417, 0.46, -0.516, -0.334, -0.504, -0.946, 
-0.916, -0.975, -1.099, -1.113, -1.297, -1.234, -0.954, -1.122, -0.609, 
-0.593, -0.403, -0.51];

xy=[x y]

meps=1e-4

fun(x::Vector{Float64}, a::Vector{Float64})=x[2] - a[1] * sin(a[2] * x[1] - 
a[3])

a0=[1.2, 0.5, 0.5];

nonlinear_fit(xy, fun, a0, meps, 200_000)
# returns: 
([1.192007321713507,0.49426296880933257,0.19863645732313934],false,200000)

HTH



On Saturday, January 23, 2016 at 9:42:08 PM UTC-2, [email protected] 
wrote:

hello tshort

I published this post on the "stack". the solution presented has errors in 
the coefficients.



Em sábado, 23 de janeiro de 2016 14:08:07 UTC-2, tshort escreveu:
One link:
http://stackoverflow.com/questions/34840875/julia-using-curvefit-package-non-linear
On Jan 23, 2016 10:13 AM, <[email protected]> wrote:
I wish someone would publish a nonlinear fitting code using package CurveFit
using this data, how we can use CurveFit?
x = [0.0 0.2 0.4 1.0 1.6 1.8 2.0 2.6 2.8 3.0 3.8 4.8 5.0 5.2 6.0 6.2 7.4 
7.6 7.8 8.6 8.8 9.0 9.2 9.4 10.0 10.6 10.8 11.2 11.6 11.8 12.2 12.4];

y = [-0.183 -0.131 0.027 0.3 0.579 0.853 0.935 1.133 1.269 1.102 1.092 
1.143 0.811 0.91 0.417 0.46 -0.516 -0.334 -0.504 -0.946 -0.916 -0.975 
-1.099 -1.113 -1.297 -1.234 -0.954 -1.122 -0.609 -0.593 -0.403 -0.51];

asasasa




On Saturday, January 23, 2016 at 9:42:08 PM UTC-2, [email protected] 
wrote:
>
>
> hello tshort
>
> I published this post on the "stack". the solution presented has errors in 
> the coefficients.
>
>
>
> Em sábado, 23 de janeiro de 2016 14:08:07 UTC-2, tshort escreveu:
>>
>> One link:
>>
>>
>> http://stackoverflow.com/questions/34840875/julia-using-curvefit-package-non-linear
>> On Jan 23, 2016 10:13 AM, <[email protected]> wrote:
>>
>>> I wish someone would publish a nonlinear fitting code using package 
>>> CurveFit
>>> using this data, how we can use CurveFit?
>>>
>>> x = [0.0 0.2 0.4 1.0 1.6 1.8 2.0 2.6 2.8 3.0 3.8 4.8 5.0 5.2 6.0 6.2 7.4 
>>> 7.6 7.8 8.6 8.8 9.0 9.2 9.4 10.0 10.6 10.8 11.2 11.6 11.8 12.2 12.4];
>>>
>>> y = [-0.183 -0.131 0.027 0.3 0.579 0.853 0.935 1.133 1.269 1.102 1.092 
>>> 1.143 0.811 0.91 0.417 0.46 -0.516 -0.334 -0.504 -0.946 -0.916 -0.975 
>>> -1.099 -1.113 -1.297 -1.234 -0.954 -1.122 -0.609 -0.593 -0.403 -0.51];
>>>
>>

Reply via email to