I did this using MATLAB 2010b but it should work with other versions assuming that the polyfit function hasn’t changed much.įirst, open up the polyfit function in the MATLAB editor edit polyfitĬhange the first line so that it reads function = wpolyfit(x,y,n,w)
#Polyfit matlab how to
So, instead I will give you instructions on how to make it yourself. So where is this new function I hear you ask? Normally this is where I would provide you with a download link but unfortunately I created wpolyfit by making a very small modification to the original built-in polyfit function and so I might be on dicey ground by distributing it. My solution was to create a new function, wpolyfit, that does accept a vector of weights: wpolyfit(x,y,2,w) Sadly, however, we cannot supply the weights to the polyfit function as it currently stands (as of 2010b). Which would agree with the curve fitting toolbox if we set the weights to all ones.
So far so good but she didn’t have access to the curve fitting toolbox so what to do? One function that almost meets her needs is the standard MATLAB function polyfit which can do everything apart from the weighted part. Now, if she had the curve fitting toolbox this would be nice and easy: x= Someone recently contacted me with a problem – she wanted to use MATLAB to perform a weighted quadratic curve fit to a set of data.