nonlinear curve , lsqcurvefit
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I am using 'lsqcurvefit' to fit a nonlinear model ( Actualy a fourier series in which I know the frequencies. I want the amplitude of each sin/cosine term). So problem is like this,
suppose I have 'N' grid point and for each grid point I have time series data for which I know three dominant frequencies. Now the model which I want to fit on the time series for each grid is
H=A+B*t + C1*cos(2.pi.f2*t)+ C2*sin(2.pi.f1*t)+ C3*cos(2.pi.f2*t)+
C4*sin(2.pi.f2*t)+ C5*cos(2.pi.f3*t)+ C6*sin(2.pi.f3*t)
As I mentioned, I know the value of f1, f2, f3 for each grid and want to estimate A,B,C1,C2,... for each grid. Here 't' is independent variable.
I want to do this in loop for i=1:N and change the value of 'f' for each loop as I have these values for each grid.
Can you please help me out from this problem.
Here is what I was trying but its not working. Firstly I define a function with some function name say 'FUN' hence
@FUN((x0,Xdata,f) H=x0(1)+x0(2)*Xdata+x0(3)*cos(2*pi*f1.*Xdata)+x0(4)...
*sin(2*pi*f1.*Xdata)+x0(5)*cos(2*2*pi*f2.*Xdata)+...
x0(6)*sin(2*2*pi*f2.*Xdata)+x0(7)*cos(3*2*pi*f3.*Xdata)+...
x0(8)*sin(3*2*pi*f3.*Xdata);
and
f=[f1,f2,f3]
then using lsqcurvefitting
[P1,N1,R1,EF1,OP1,L1,J1]=lsqcurvefit(@EWHmodelFunc3,ones(8,1)*0.5,f0,t,EWH);
Please help me out
2 comentarios
Matt J
el 15 de Sept. de 2016
Your objective function is linear in all of the parameters. Looks like you should really be using lsqlin(). If the lower bound EWH is non-critical, you could even be using mldivide().
Respuestas (1)
Matt J
el 15 de Sept. de 2016
Editada: Matt J
el 15 de Sept. de 2016
Just some guesses, but you really need to provide copy/pastes of error messages as requested here. Your definition of FUN has the wrong syntax. Looks like it should be
EWHmodelFunc3 = @FUN((x0,Xdata,f) x0(1)+x0(2)*Xdata+x0(3)*cos(2*pi*f1.*Xdata)+x0(4)...
*sin(2*pi*f1.*Xdata)+x0(5)*cos(2*2*pi*f2.*Xdata)+...
x0(6)*sin(2*2*pi*f2.*Xdata)+x0(7)*cos(3*2*pi*f3.*Xdata)+...
x0(8)*sin(3*2*pi*f3.*Xdata);
Also, the call to lsqcurvefit looks like it should be
[P1,N1,R1,EF1,OP1,L1,J1]=...
lsqcurvefit(@(x0,Xdata) EWHmodelFunc3(x0,Xdata,f),ones(8,1)*0.5,f0,t,EWH);
4 comentarios
Saurabh Srivastava
el 17 de Sept. de 2016
Editada: Matt J
el 17 de Sept. de 2016
Matt J
el 17 de Sept. de 2016
Editada: Matt J
el 18 de Sept. de 2016
I get the feeling you're just putting the lsqcurvefit input arguments in the wrong places. Earlier you said 't' is the independent variable, but here you've called lsqcurvefit with t as the dependent variable. Also, you are now telling lsqcurvefit that 'f' is the independent variable, whereas earlier, you said it was just a set of 3 frequency parameters.
x = lsqcurvefit(fun,x0,xdata,ydata,lb)
and so lsqcurvefit is making the associations,
fun= @(x0,Xdata) H(x0,Xdata,f); %model function
x0= ones(8,1)*0.5; %initial parameter guess
xdata = f; ydata = t; %data to be fit
lb=EWH; %lower bounds
lsqcurvefit is iteratively evaluating H(x0,Xdata,f) for different parameter choices and comparing the result element-wise to the 4th input argument, ydata=t. The error message is complaining that H(x0,Xdata,f) does not return an array the same size as ydata=t making the comparison impossible.
There is also a warning saying that the lower bound vector EWH is shorter in length than x0= ones(8,1)*0.5. In other words, you haven't specified a bound on all 8 of your unknown parameters. Therefore, lsqcurvefit is going to assume you want no bounds on all x0(i), i>length(EWH).
Ver también
Categorías
Más información sobre Mathematics and Optimization en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!