Fitting two arbitrary set of data without tool box
Show older comments
Hello Matlab experts,
I have measurements of several vectors such that y(:,i) = [a1i,a2i,....ani]. This data set also includes measurement errors. Now I have a modeled solution such that ymodl =[b1, b2, b3,....bn]. I am trying to fit measurement data with modeled data using fminsearch as following:
function errprd=sumsqerror(fitparam,vecdata,datatraj)
a =fitparam(1);
b =fitparam(2);
ypred = a*vecdata +b;
errprd = sum((ypred-datatraj).^2);
fitfun = @(x)sumsqerror(x,evect,trajdata);
[bestx,errsumsqr, existflag, output] = fminsearch(fitfun,x);
My hypothesis suggests that one of columns of y measurement matrix should fit with modeled vector.
Though, I was unable to find a solution with this algorithm. I will highly appreciate if someone could tell me a better procedure (if there is any) to optimize a fitting of any arbitrary set of data.
Thank you.
Kind regards,
3 Comments
Walter Roberson
on 19 Oct 2018
For that particular ypred, a pure linear model, you can replace your entire code with a call to polyfit(evect, trajdata, 1)
Arun
on 19 Oct 2018
Walter Roberson
on 19 Oct 2018
You have
ypred = a*vecdata +b;
That is a plain y = a * x + b prediction, which polyfit(x,y,1) will calculate pretty quickly. Or more directly,
ab = [evect(:), ones(numel(evect),1] \ trajdata(:);
When I look at your data I would say, that a*x+b is a poor fit for the data -- but it is the model implemented by your ypred.
The data you display is pretty low quality. It looks like you only have 16 datapoints. I would guess maybe sum of three gaussians, maybe sum of four gaussians would be needed. That would require either 6 or 8 parameters. I would expect a low quality fit considering the small amount of data.
Answers (1)
madhan ravi
on 19 Oct 2018
0 votes
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!