Fit model with 3 independent variables and many parameters to data
Show older comments
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
Accepted Answer
More Answers (2)
Manuel
on 11 Feb 2013
2 votes
1 Comment
the cyclist
on 11 Feb 2013
I suggest you ask this as a new question. Questions buried within other questions rarely get attention.
NgocDuy Nguyen
on 7 Oct 2021
0 votes
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 Comment
the cyclist
on 11 Oct 2021
Please open this as a new question, not making it an "answer" on a 8-year-old question
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!