MATLAB Answers

Curve fit for multiple datasets

조회 수: 55(최근 30일)
RJohnson 2015년 11월 23일
답변: Szu-Yu Lee 2021년 4월 8일
I have a relatively easy question but since I'm new to Matlab I haven't been able to figure it out. I have two arrays; one of x values and one of y values. Both are arrays with 101 rows and 40 columns. The first column in the X1 array corresponds to the the x values for the first column of values in Y1. The second column in X1 to the second column in Y1; the third column in X1 to the third column in Y2 and so forth. I want to perform a polynomial curve fit on all 40 columns of data and have the resultant data (i.e. the new "fitted" Y values) available in the workspace. I can perform the fit for one curve with no problem. The code is below for the 20th column of data:
[curve1 gof1]=polyfit(X1(:,20),Y1(:,20),9);
%(optional plotting):
If I want to fit all 40 columns, can I use a for-loop or is there something more elegant? Here is what I have tried; it works, but I'm open to suggestions for improvement.
for i=1:40;
[curve1 gof1]=polyfit(X1(:,i),Y1(:,i),9);


Image Analyst
Image Analyst 2015년 11월 24일
I'd call unique(X) to get all the unique x values, just in case they're different from one column to the next.
uniqueX = unique(X);
Then I'd call interp1() on each x,y pairing with the unique values of x. So now you have all the curves sampled at the same x locations. So then I'd just take the mean of all the interpolated y values to get the average at each x
meanY = mean(interpolatedY, 2);
Then do your curve fitting, whether it's via polyfit() or something fancier, on the curve with uniqueX and meanY. Others may have different ideas but it seems reasonable to me. It's worth a shot. Give it a try.
  댓글 수: 4
Image Analyst
Image Analyst 2021년 1월 9일
Start a new discussion thread and attach your data and the order of the polynomial you want to fit.

댓글을 달려면 로그인하십시오.

Szu-Yu Lee
Szu-Yu Lee 2021년 4월 8일
Hi, I guess you are trying to fit a large number of independent equations? MATLAB "fit" function do not support multiple independent curves fitting. I have created a function that takes in a 2D matrix, where each row is a curve to be fitted to some polynomial expression. The function does not use for-loop, so can work on large number of rows in a very short time.

Community Treasure Hunt

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

Start Hunting!

Translated by