Fitting two arbitrary set of data without tool box

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

For that particular ypred, a pure linear model, you can replace your entire code with a call to polyfit(evect, trajdata, 1)
Dear Walter,
Attached is one trajectory, y(:,1); It is not linear. Do you still think polyfit will work here ?
Thanks and regards
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.

Sign in to comment.

Answers (1)

Categories

Asked:

on 19 Oct 2018

Commented:

on 19 Oct 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!