Fitting multiple data sets to single curve in least square sense
Show older comments
I am trying to fit multiple data sets i.e., x1,x2,x3 -> y1,y2,y3 to a single cuve f following this exapmle. However, it returns error that the second coulmn must be a vector. The ultimate goal is to fit to a curve such that the sum(abs(f(x)-y))<3. How can I do it in matlab starting with the following example
x1=(0:1:10)'; % Explanatory variable
x2=(0:1:10)'+1;
x3=(0:1:10)'+2;
x=[x1 x2 x3];
y = 5 + 3*x + 7*x.^2;
y = y + 2*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x,y,f,[1 1 1]); %Error: Requires a vector second input argument.
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')
The actuall dataset is attached.
Answers (2)
the nlinfit function take a vector as second input y, because it's cost function for optimization and regression is based on scalar output. you should fitt each model separately.
x1=(0:1:10)';
x2=(0:1:10)'+1;
x3=(0:1:10)'+2;
x=[x1 x2 x3];
y = 5 + 3*x + 7*x.^2;
y = y + 2*randn((size(x)));
model = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
for i=1:size(x,2)
F_fitted(i,:) = nlinfit(x(:,i),y(:,i),model,[1 1 1]);
end
for i=1:size(x,2)
subplot(1,3,i)
plot(x(:,i),y(:,i),'*',x(:,i),model(F_fitted(i,:),x(:,i)),'g')
title(['(X_' num2str(i) ',Y_' num2str(i) ')'])
end
9 Comments
Abdulllah
on 11 Mar 2022
well then. it means different dataset are actually more information for 1 curve. then combine them together and solve the problem.
x1=(0:1:10)';
x2=(0:1:10)'+0.2;
x3=(0:1:10)'+0.6;
x=[x1 x2 x3];
y = 5 + 3*x + 7*x.^2;
y = y + 2*randn((size(x)));
model = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x(:),y(:),model,[1 1 1]);
X_sort = sort(x(:));
Y_sort = sort(y(:));
plot(X_sort,Y_sort,'*',X_sort,model(F_fitted,X_sort),'b')
Abdulllah
on 11 Mar 2022
column of your data doen't seem to belong to same curve!!! they look like have a small shift to up. is this what you want?
load('dataset.mat')
figure;
scatter(x(:,1),y(:,1),'filled');hold on
for i=2:6
scatter(x(:,i),y(:,i),'filled')
end
legend('col1','col2','col3','col4','col5','col6','Location','northwest')
if this is okay with you, you can treat this x and y like my previous comment.
[X_sort,index] = sort(x(:));
Y_sort = y(index);
the rest is same.
Abdulllah
on 11 Mar 2022
Abolfazl Chaman Motlagh
on 11 Mar 2022
i scatterd them to demonstrate the fact that they don't lie on same curve. the solution is in second comment.
Abdulllah
on 11 Mar 2022
Abolfazl Chaman Motlagh
on 11 Mar 2022
let us review our conversation, because i think there's misunderstanding between us.
-------------------------------------------------------------------------
you have two variable x and y. both have same number of elements. column are x and y are different observations. but we know that all datasets obey same relation. which means there is a function like f(x) that describe all relations. which means
. j here is index of dataset (which is column) and i is index of element within a dataset (which is row). so basically:
. j here is index of dataset (which is column) and i is index of element within a dataset (which is row). so basically:
so basically all elements of x and y are some information we can use in finding function f.
--------------------------------------------------------------------------
if this is the problem making x and y, a column vector using x(:) and y(:) and then finding best fit on all data will do the work. (my second comment)
but here comes my objection. if column of x (and y) are describing same functionality and relation. then ploting them should illustrate this fact. but as we can see in scatter plot that i put in third comment, we can see the
pairs don't show same relation for any i. this means there is no functionality describing all this relations at once.
pairs don't show same relation for any i. this means there is no functionality describing all this relations at once.
AndresVar
on 11 Mar 2022
@Abdulllah like Abolfazi said, your data won't fit a single f(x). The data fits a*exp(b*x) pretty well but for different values of a.
There might be another parameter that is missing that collapses the data.
Find the different coefficient by fitting each series or find them like this: simulataneous Curve fitting - (mathworks.com)
x1,x2,x3 are column vectors that you combined into a matrix, so then y was evaluated to a matrix also.
nlinfit expects vectors not matrices
to fix it, combine x1 x2 x3 into 1 long column vector using ";"
x=[x1;x2;x3]
Then you should probably sort it and apply same sorting to y Sort array elements - MATLAB sort (mathworks.com)
1 Comment
Abdulllah
on 11 Mar 2022
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!

