This is actually not a nonlinear system, but a linear one. Thus, in the general case where your sampling vector X is not regular, a linear least squares fit could be done easily with the pseudo inverse of the system matrix:
np = 160 ; Number of samples
nc = 4 ; Number of even/odd terms
;; Irregular sampling points
x = 2*!dpi*randomu(seed, np)-!dpi
x = x[sort(x)]
;; Generate signal according to model
H = dblarr(np, 2*nc+1) ; System matrix
H[*,0] = 1 ; Constant term
for n=1,nc do begin
H[*,n] = cos(n*x) ; even terms
H[*,n+nc] = sin(n*x) ; odd terms
endfor
coeff = randomn(seed, 2*nc+1) ; Random coefficents
s = H#coeff ; signal
n = randomn(seed, np) ; noise
;; least squares fit to signal:
Hpinv = invert(transpose(H)#H)#transpose(H) ; pseudoinverse of linear system
coeff_est = Hpinv#s
print, 'RMS: ', sqrt(mean(abs(coeff_est - coeff)^2)) ; Exact within numerical precision
;; least squares estimate of system coefficients
coeff_est = Hpinv#(s+n)
print, 'RMS: ', sqrt(mean(abs(coeff_est - coeff)^2))
;; Fitted signal
s_fit = H#coeff_est
plot, x, s+n, linestyle=1, thick=2. ; Noisy observation
oplot, x, s, color='ff'x ; True signal
oplot, x, s_fit, color='ff00'x ; Fitted signal
8<------------------------
If your sample vector X happens to be regular, the solution to your problem is actually nothing more than an FFT, and pick the 5 first complex coefficients. The first coefficient is the constant term A0 (not included in your problem), and real/imaginary parts of the following coefficients corresponds to cosine terms A1-A4 and the sine terms B1-B4, respectively.
--
Yngvar
On Saturday, 25 October 2014 07:47:17 UTC+2, siumt...@gmail.com wrote:
> I think you should try to be for specific to ask question here.
>
> Suppose I have a timeseries with the S size.
>
> I want to do nonlinear fitting to the timeseries using the following fourier series ( harmonic function)
>
> F(X) = ∑((Ancos(nπx/L)+Bnsin(nπx/L) ) , Where n = 1,2,3,4
>
> And I would find 8 coefficients such as An and Bn where n = 1,2,3,4
>
> That is.
>
> A1,A2,A3,A4
> B1,B2,B3,B4
>
> I have attempted to understand how it works mpfit by Craig and curvefit . Unfortunately, I did not because I am not IDL expert. So I posted this if anyone can help
>
>
> Best Wishes
>
>
> Thanks for you help
|