curve fitting with numeric array instead of analytic form for function of x
6 views (last 30 days)
Show older comments
Hello,
Sorry if this is a silly question, but I've been searching through the help documentation and I've been unable to find an answer. Does MATLAB have the ability to do linear least squares fitting of the type y = a*f1(x) + b*f2(x) + ..., where fi(x) are numerically defined functions of the independent variable? I have f1, f2, f3, and f4 defined in vectors as the normalized (to maximum value) results of source signal measurements. I have another set of signal measurements that I want to 'decompose' into a linear sum of the four sources, with amplitudes determined by least squares.
I've found that I can define functions of 'x' and use the matlab function 'fit' with no problem, but I'm unable to write down an analytic functional form for this type of model, so I don't think the curve fitting toolbox will be of much use. Does anyone have any suggestions?
Thanks a lot,
Chris
2 Comments
Star Strider
on 10 Aug 2012
Do your functions f1(x) ... fi(x) produce vectors equal to length(y) (assuming y is a vector and not a matrix), or are they nonlinear functions that have to be evaluated during the parameter estimation process?
Accepted Answer
Star Strider
on 10 Aug 2012
Edited: Star Strider
on 10 Aug 2012
‘Yes, the fi(x) are vectors of the same length y, the data I would like to decompose.’
In that situation, this seems to me a multiple linear regression problem. If your matrix of f(*) vectors is F, your vector of parameters a, b, ... is P, then your system becomes (with the appropriate dimensions of F):
y = F*P
and the solution:
P = F\y;
A;though you may need to augment F with a constant vector such as:
F1 = [ones(size(F,1) F];
and then:
P = F1\y;
13 Comments
Star Strider
on 11 Aug 2012
I would appreciate it, yes.
You might consider writing it up formally as a function, documenting it internally, and submitting it to the MATLAB File Exchange. (I believe the FEX has some suggestions on how best to do that. I haven't checked, since I haven't submitted any files in a while.) It seems to be needed. Even though lscov exists, not everyone may have the Statistics Toolbox, or want to go through everything you had to do to re-create your function.
More Answers (1)
Teja Muppirala
on 10 Aug 2012
If I understand you correctly, then I think all you need is the "backslash" operator. Just like this:
x = (0:0.1:10)';
f1 = sin(x);
f2 = exp(x/10);
f3 = 3./(1+x);
y = 3*f1 - 4*f2 + 8*f3 + 1*randn(size(x));
plot(x,y);
estimated_coeffs = [f1 f2 f3]\y
hold on;
plot(x,[f1 f2 f3]*estimated_coeffs,'r');
3 Comments
Tom Lane
on 10 Aug 2012
Teja's example calculated f1-f3 as expressions. I would think you could calculate them by fetching them out of the table, and still carry out the backslash operation that he recommends.
See Also
Categories
Find more on Spline Postprocessing 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!