0

I have a certain power spectrum array saved as test, depending on a frequency array f.

This power spectrum generally looks like in the following figure enter image description here

It is to be remarked that the above power spectrum stemms from a simulated time series.

What I want to do, in principle, is to fit a curve approaching the simulated power spectrum as shown below:

enter image description here

I know that the theoretical power spectrum can be defined as follows:

function ps_theo = ps_th(L,Uhub,f)

const = L/Uhub;
f_x = 6.*f.*(L/Uhub);
exp = 5/3;
ps_theo = (4*const)./((1 + f_x).^exp);

end

where L is a constant length scale, Uhub, a constant speed and f the frequency vector.

The question is: I don't know the value of 'L', therefore I was thinking of an optimized non-linear resolution by using lsqcurvefit.

I have been proceeding as follows:

xdata = f;
ydata = test;
Uhub = 10;

fit_func = @(L) ps_th(L,Uhub,f);

L_opt = lsqcurvefit(@fit_func,330.2,xdata,ydata)

which retrieves an error msg on the number of inputs variables for the fit_func function.

Would you mind to shed a light?

fpe
  • 2,700
  • 2
  • 23
  • 47
  • Read the help for lsqcurvefit. Look at the examples. How does it pass in the arguments to your function? How does it optimize when there is more than one variable? THINK! –  Apr 08 '13 at 13:09

1 Answers1

1

The function you're fitting can only take in two arguments. You can rewrite ps_th like this:

function ps_theo = ps_th(x0,f)
L = x0(1);
Uhub = x0(2);

const = L/Uhub;
f_x = 6.*f.*(L/Uhub);
exp = 5/3;
ps_theo = (4*const)./((1 + f_x).^exp);

end

Then call lsqcurvefit with something like this:

x0Start = [330.2,10]; % vector of initial parameters
x0_opt = lsqcurvefit(@ps_th,x0Start,xdata,ydata);
Molly
  • 13,240
  • 4
  • 44
  • 45
  • I have done this already, but then also `Uhub` is being changed; only `L` is supposed to vary. – fpe Apr 08 '13 at 13:29
  • Then don't make Uhub an input argument to ps_th. – Molly Apr 08 '13 at 13:38
  • beside the variability of `Uhub` (which is undesired), the final fit is really poor. – fpe Apr 08 '13 at 13:39
  • You can try changing the initial parameters or varying the Options arguments, particularly TolFun, TolX, and MaxIter. Are you sure a good fit is possible with one varying parameter? – Molly Apr 08 '13 at 13:44
  • it should since the only parameter I want to evaluate is 'L'. I'll work around what you propose – fpe Apr 08 '13 at 13:46