Effacer les filtres
Effacer les filtres

Fit model with 3 independent variables and many parameters to data

71 vues (au cours des 30 derniers jours)
Manuel
Manuel le 11 Fév 2013
Commenté : the cyclist le 11 Oct 2021
is it possible to fit the parameters of a non linear model with more than 2 independent variable (let's say 3 or 4 for example) to data???
here attached is my code, where the coefficients are a b c d and the independent variables are x,q,w
function [beta]=fitgen(Xtest,Ytest,Wtest,Ztest,p,p_low,p_up)
mdl=@(a,b,c,d,w,q,x) zfit(a,b,c,d,w,q,x);
algo1='Trust-Region';
fit_opt = fitoptions('Method','NonlinearLeastSquares',... 'Lower',p_low,'Upper',p_up,... 'Robust','on',... 'Normalize','off',... 'Algorithm',algo1); fit_typ = fittype(mdl,'option',fit_opt);
[Yfitt,gof,output]=fit([Xtest,Ytest,Wtest],Ztest,fit_typ,'Start',p)
%%where the function zfit is (I used a linear model just for sake of simplicity, but the final purpose is to fit a non linear model):
function [z]=zfit(a,b,c,d,w,q,x)
[x,q,w]=meshgrid(x,q,w);
z=a*x+b*q+c*w+d;
end
thank you very much in advance

Réponse acceptée

the cyclist
the cyclist le 11 Fév 2013
Yes, if you have the Statistics Toolbox you can use the nlinfit() function to do this. Here is a very simple example.
function [] = nlinfitExample()
% Here is an example of using nlinfit(). For simplicity, none of
% of the fitted parameters are actually nonlinear!
% Define the data to be fit
x=(0:1:10)'; % Explanatory variable
y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect)
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]);
% 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')
end
In my case, I just have one explanatory variable vector, x, but that could have been a matrix with multiple variables in each column.
Hope that helps.
  2 commentaires
nony lew
nony lew le 16 Avr 2018
I have used this answer of yours to fit my datas. And it went perfectly for me too, thank you.
x=[4.3...]; y=[11987.36...];
f = @(F,x) F(1)*x +F(2)+F(3)*(sin(F(4)*x+F(5))); F_fitted = nlinfit(x,y,f,[1 1 1 1 1]); % Display fitted coefficients disp(['F = ',num2str(F_fitted)]) % Plot the data and fit figure plot(x,y,'*',x,f(F_fitted,x),'r'); legend('data','fit')
I have another question:
How can I modify this code to apply to structures?
if in my case:
x = xdata (k) .part is a structure <1x200 struct>
and
y = ydata (k) .part is a structure struct
how can i change the code to fetch every dataset?
Thanks

Connectez-vous pour commenter.

Plus de réponses (2)

Manuel
Manuel le 11 Fév 2013
Thank you very much it works.
Does anyone knows how to apply boundary condition to the parameters during the fitting using the function nlinfit?
Thank you a lot
  1 commentaire
the cyclist
the cyclist le 11 Fév 2013
I suggest you ask this as a new question. Questions buried within other questions rarely get attention.

Connectez-vous pour commenter.


NgocDuy Nguyen
NgocDuy Nguyen le 7 Oct 2021
Hello everyone,
I have tried to fit a function f(x,y,z) to determine F1, F2, F3, ...., F9 coefficients.
My code is as follows:
function [] = nlinfitExample()
[x1,x2,x3] = meshgrid(362:1,362:1,362:1);
[ff]=meshgrid(362:1);
f=[ff(x1,x2,x3)];
[F_fitted] = meshgrid(9:1);
[F] = meshgrid(9:1);
x = x1;
y = x2;
z = x3;
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x,y,z) F(1)*exp(-((x-20)^2+(y-20)^2)/12)+F(2)*exp(-((x-38)^2+(y-50)^2)/43)+F(3)*exp(-((x-350)^2+(y-82)^2)/13)+F(4)*exp(-((x-58)^2+(y-82)^2)/24)+F(5)*exp(-((x-70)^2+(y-110)^2)/244)+F(6)+(0.0000532793*x^2-5-F(7)*(y-x)/(y+x))*ln(z-F(8)*((-1)^x+(-1)^y))+F(9)*0.0000532793*x^2+0.0000177598*x^2*ln(x+y)-0.022930657*x;
%F_fitted = nlinfit(x,y,z,f,[F(1), F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9)]);
F_fitted = nlinfit(x,y,z,f, [1 1 1 1 1 1 1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
end
But it gives the error as follows:
Error:
Requires a vector second input argument.
Error in nlinfitExample (line 17)
F_fitted = nlinfit(x,y,z,f, [1 1 1 1 1 1 1 1 1]);
Could you please help me to solve this problem?
Thank you.
  1 commentaire
the cyclist
the cyclist le 11 Oct 2021
Please open this as a new question, not making it an "answer" on a 8-year-old question

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by