Can Matlab generate a function based on XY points?

Hello,
I was wondering if this is possible in Matlab.
Say I generate an X and Y array that is not linear. I could connect the points and create a curve but this is labor intensive.
If given a data set can you use Matlab to generate a function that represents these points?
Excel has built in functions to do this and I may try to determine the relationship this way but was wondering if I could do it in Matlab.
Thank you,
Mike

 Accepted Answer

Star Strider
Star Strider on 29 Oct 2012
Edited: Star Strider on 29 Oct 2012
It depends what you want to do. I suggest polyfit and its related functions. You can find the links to all of the other functions at the end of the documentation page for polyfit.
Another option is spline and its friends.

2 Comments

Thank you, is there a way to tell how closely the fit matches the data? In Excel there is the R^2 value...
The polyval function will produce confidence intevals on the data if you ask it to. The calculated confidence intervals are likely better than R^2 at estimating goodness-of-fit.
There isn't a direct way to calculate the confidence intervals on the parameters, although the documentation for polyfit outlines a way to estimate the covariance matrix of the estimated parameters from the S structure it returns if you ask it to. If you want to calculate the critical values to estimate the confidence intervals on the parameters from the square root of the diagonal of the covariance matrix, use these:
[p,S] = polyfit(x,y,n); % Fit the data
COVB = (S.R.'*S.R)\eye(size(S.R)) * S.normr^2/S.df;
SE = sqrt(diag(COVB));
CV = @(alpha) -sqrt(2) * erfcinv(2*alpha); % Equivalent to ‘norminv’
alpha = 0.95;
zs = CV([(1-alpha)/2 1-(1-alpha)/2]);
CI95 = bsxfun(@plus,bsxfun(@times,SE,zs),p.');
See the documentation for polyfit for S.R, S.normr, and S.df. The CI95 variable are the confidence intervals on the parameters. The zs variable will calculate the critical values (or z-scores) corresponding to whatever value of alpha you want. (I set that code up for 95% confidence intervals.)

Sign in to comment.

More Answers (1)

Categories

Community Treasure Hunt

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

Start Hunting!