The 'fun' function has to return a residual value 'r' of type Float64, 
calculated at each iteration of the data, as follows:

r = y - fun(x, coefs)

so your function y=a1*sin(x*a2-a3) will be defined as:

fun(x,a) = x[2]-a[1]*sin(a[2]*x[1] - a[3])

Where:

  x[2] is a value of 'y' vector  
  x[1] is a value of 'x' vector  
  a[...] is the set of parameters
  
The 'fun' function has to return a single Float64, so the operators can't 
be 'dot version' (.*).

By calling the nonlinear_fit function, the first parameter must be an array 
Nx2, with the first column containing N values of 'x' and the second, 
containing N values of 'y', so you must concatenate the two vectors 'x' and 
'y' in a two columns array:

xy = [x y]

and finally, call the function:

coefs, converged, iter = CurveFit.nonlinear_fit(xy , fun , a0 , eps, 
maxiter) 

The complete 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)

posted on another thread, as well: 
https://groups.google.com/forum/#!searchin/julia-users/curvefit/julia-users/ZlIujG4fI_c/pNWzHd40AAAJ

HTH

On Tuesday, January 19, 2016 at 12:40:37 AM UTC-2, [email protected] 
wrote:
>
>
>
> Code: 
>
> 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];
>
>
> x0 = vec(x)
> y0 = vec(y)
>
> a = [1.5 1.5 1.0]
> eps = 0.000000000001
> maxiter= 200.0
>
> fun(xm,a) = a[1].*sin(a[2].*xm - a[3]);
>
> xy=hcat(x0,y0);
>
> coefs,converged,iter = CurveFit.nonlinear_fit(xy,fun,a,eps,maxiter);
>
>

Reply via email to