How can I fit a function to my experimental data?
52 views (last 30 days)
Show older comments
Hi, apologies if something has been asked similar before but I couldn't find anything that was that appropiate to my problem.
I have some experimental data that I am trying to fit to the function y = C1 + e^(dt) * (C2 cos(ωt)+ C3 sin(ωt)) where C1, C2, C3, ω and d are all constants which I need to validate my model.
I have tried to follow the lsqcurvefit example where I have done:
>> Data=...
[0,1.48737300000000;0.000977000000000000,1.48615200000000...........0.125000000000000,1.48325300000000];
t=Data(:,1);
>> y=Data(:,2);
>> % axis([0 2 -0.5 6])
>> % axis([1.47 1.5 0 0.125])
>> % hold on
>> plot(t,y,'ro')
>> title('Data points')
>> %hold off
>> F=@(x,xdata)x(1)+exp(x(2)*xdata)*(x(3)*cos(x(4)*xdata)+x(5)*sin(x(4)*xdata));
>> x0=[1 100 1 1 1];
>> [x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,y)
hold on
plot(t,F(x,t))
hold off
Unfortunately I am getting the following error:
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
@(x,xdata)x(1)+exp(x(2)*xdata)*(x(3)*cos(x(4)*xdata)+x(5)*sin(x(4)*xdata))
Error in ==> lsqcurvefit at 209
initVals.F =
feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function
evaluation. LSQCURVEFIT cannot continue.
Could anyone tell me where I am going wrong? And am I using the right function? I have seen fminsearch has been used on similar problems but couldn't find any examples to follow through. I should apologise in advance for my lack of experience in Matlab. If anyone could help I would be hugely appreciative!
Thank you
0 Comments
Accepted Answer
Star Strider
on 16 Mar 2014
You need to vectorize your objective function. Experiment with this version:
F=@(x,xdata) x(1)+exp(x(2).*xdata).*(x(3).*cos(x(4).*xdata)+x(5).*sin(x(4).*xdata));
The dot operator modifies multiplication (.*), division (./), and exponentiation (.^) for element-by-element operations rather than matrix or vector operations.
6 Comments
More Answers (2)
John D'Errico
on 18 Mar 2014
- You have no good physical model for the process.
- The relationship is a complicated one.
The first point is clear. That you seem willing to try a different model tells me that you have no model that makes sense from physical reasoning.
There are valid reasons to need a nonlinear curve fit of course. But if all you need is a smooth curve that replicates the essential shape of your data, then a spline model is right. You can use it to create a plot, or to predict values of the relationship at any point. Of course, splines don't extrapolate well, but there is no sane way to extrapolate this relationship anyway. And if you want to see a functional form that you can write down to put in a paper, then again you are out of luck with a spline model.
With 10 equally spaced knots, we get a curve that replicates the essential shape, but does a fair amount of smoothing.
slm = slmengine(t,y,'plot','on','knots',10,'reg','c');
With 50 equally spaced knots, it has the flexibility to chase after some of the wiggles in the curve.
slm = slmengine(t,y,'plot','on','knots',50,'reg','c');
Use of cross validation here to automatically smooth the curve based on the amount of noise it sees seems the right choice, so with 25 knots, the resulting curve was not that much different from what I see with 50 knots.
As it turns out, I needed little of the many capabilities of SLM to fit your data, but a spline model seems best to me.
1 Comment
Star Strider
on 18 Mar 2014
I would agree, except that Joshua mentioned in his original post that he has a model for his data but that he was having problems fitting it, most likely because of the noise. Empirically determining the offset, amplitude and period from the data and using them as initial parameter estimates for x(1), x(3), and x(4) allowed a simple nonlinear regression to fit his model to the other parameters as well.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!