X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 15 Nov 2012 12:47 AM by  anon
curve fit a sine function
 2 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
15 Nov 2012 12:47 AM
    I cannot figure out how to curve fit a sine function. Could someone please provide an example? I have already figured out how to curve fit other plots, but for some reason cannot get sine to work. The problem is that keep getting "CURVEFIT: Failed to converge- CHISQ increasing without bound" Thanks for any help

    Deleted User



    New Member


    Posts:
    New Member


    --
    30 Nov 2012 12:21 PM
    Hi Karakura, I think the following is a very nice example. You will need to use a combination of gaussian and a quadratic using GAUSSFIT(). I just modified the example code in the IDL help to use a sin() function instead, I hope this helps: pro ex_gaussfit ; Define the independent variable. n = 101 ;x = (FINDGEN(n)-(n/2))/4 X = 2*!PI/100 * FINDGEN(100) ; Define the coefficients. a = [4.0, 1.0, 2.0, 1.0, 0.25, 0.01] print, 'Expected: ', a z = (x - a[1])/a[2] ; Gaussian variable !P.MULTI = [0,2,2] ; set up 2x2 plot window seed = 123321 ; Pick a starting seed value for nterms=3,6 do begin ; Define the dependent variables. Start with random noise. ;y = 0.4*RANDOMN(seed, n) y = sin(x) ; Use a switch statement so we fall through to each term. switch nterms of 6: y = y + a[5]*x^2 5: y = y + a[4]*x 4: y = y + a[3] 3: y = y + a[0]*exp(-z^2/2) endswitch ; Fit the data to the function, storing coefficients in ; coeff: yfit = GAUSSFIT(x, y, coeff, NTERMS=nterms) print, 'Result: ', coeff[0:nterms-1] ; Plot the original data and the fitted curve: PLOT, x, y, TITLE='nterms='+STRTRIM(nterms,2) OPLOT, x, yfit, THICK=2 endfor end Cheers, Fernando

    Deleted User



    New Member


    Posts:
    New Member


    --
    30 Nov 2012 12:25 PM
    Hi again, Also, this page is nice as well: http://math.stackexchange...to-a-sinusoidal-wave cheers, Fernando
    You are not authorized to post a reply.