Non-linear fitting for more than three variables

I have a heat transfer experimental data set whrere Nu=f(Re,Theta,Beta).The data file is attached to this question.I want to make a non-linear curve fitting for this data set, so I wrote the following script file.For clarification purpose,Firstly,I imported the data from excel sheet as a matrix named data and as column vectors in the second import,each column is named according to the name in the excel sheet.When I try to run the script ,many errors prevent the running.Is this script correct for running?
% Create an anonymous function that describes the expected relationship
% between X and Y
f=@(c,x) c(1)*(x(:,1).^c(2))*(x(:,2).^c(3))*(x(:,3).^c(4));
% data set
% Specify x variables from data file,Re,Theta and Beta columns.
x=data;
% Specify y variable from data file ,(Nu)column.
y=Nu;
% Specify a vector of starting conditions for the solvers
c0=[1;1;1;1];
% Perform a nonlinear regression
Beta=nlinfit(x,y,f,c0);

5 件のコメント

Alex Sha
Alex Sha 2020 年 8 月 13 日
Refer to the results below:
Root of Mean Square Error (RMSE): 22.6077638837674
Sum of Squared Residual: 57244.4306363077
Correlation Coef. (R): 0.921067053007903
R-Square: 0.848364516136664
Adjusted R-Square: 0.842695899917474
Parameter Best Estimate
---------- -------------
c1 0.273964728046078
c2 0.739698526865773
c3 0
c4 -0.416259686940384
MOHAMED ABDULAZIM
MOHAMED ABDULAZIM 2020 年 8 月 13 日
Did you use the code that I have attached to this post?
Walter Roberson
Walter Roberson 2020 年 8 月 13 日
編集済み: Walter Roberson 2020 年 8 月 13 日
No, Alex used a commercial tool to find a good fit, very likely better than nlinfit could give.
MOHAMED ABDULAZIM
MOHAMED ABDULAZIM 2020 年 8 月 13 日
What is this tool?
Walter Roberson
Walter Roberson 2020 年 8 月 13 日
1st Opt by 7th Dimension Software

サインインしてコメントする。

回答 (2 件)

Alan Stevens
Alan Stevens 2020 年 8 月 13 日
編集済み: Alan Stevens 2020 年 8 月 14 日

0 投票

Because of the form of the desired fit, one can also do a fit by first taking logs. See the coding and result below. (Note Theta is turned into radians and 2pi added in order to avoid having zero angles give problems when taking logs). I've assumed the data is in the workspace with the names Nu, Re, Theta and Beta:
% Logarithmic variables
nu = log(Nu);
re = log(Re);
theta = log(Theta); %% Theta here is Theta*180/pi + 2pi to convert to radians and remove zeros.
beta = log(Beta);
% Construct vector and matrix
n = length(nu);
V = [sum(nu); sum(nu.*re); sum(nu.*theta); sum(nu.*beta)];
M = [n sum(re) sum(theta) sum(beta);
sum(re) sum(re.^2) sum(re.*theta) sum(re.*beta);
sum(theta) sum(theta.*re) sum(theta.^2) sum(theta.*beta);
sum(beta) sum(beta.*re) sum(beta.*theta) sum(beta.^2)];
c = M\V;
c1 = exp(c(1));
% Functions
nufit = @(re,theta,beta) c(1) + c(2)*re + c(3)*theta + c(4)*beta;
Nufit = @(Re,Theta,Beta) c1*(Re.^c(2)).*(Theta.^c(3)).*(Beta.^c(4));
% Plots
plot(nu), grid
hold on
nuvals = nufit(re,theta,beta);
plot(nuvals)
legend('log target','log fit')
hold off
figure(2)
plot(Nu), grid
hold on
Nuvals = Nufit(Re,Theta,Beta);
plot(Nuvals)
legend('target','fit')
hold off
Here is the final comparison graph:
Just out of interest I've calculated the Pearson correlation coefficient for this fit:
Matt J
Matt J 2020 年 8 月 13 日
編集済み: Matt J 2020 年 8 月 13 日

0 投票

When I try to run the script ,many errors prevent the running.Is this script correct for running?
No, you need to be using .* to do your vectorized multiplications. Also, you have many rows where x(:,2)=0 which cannot possibly agree with your model, and causes all kinds of NaNs to be generated during the iterative search.
Also, are the coefficients all supposed to be non-negative? If so, nlinfit will not let you apply positivity constraints. I would recommend lsqcurvefit instead,
load xyData
keep=all(x>0,2);
x=x(keep,:); y=y(keep);
X=log(x); Y=log(y);
e=ones(size(Y));
c0=[e,X]\Y; c0(1)=exp(c0(1)); %Use log-linearity of the model
f=@(c,x) c(1)*(x(:,1).^c(2)).*(x(:,2).^c(3)).*(x(:,3).^c(4));
[Beta,~,resid]=lsqcurvefit(f,c0,x,y);
I assume Alex did something similar, since the result I get from this is very close to his,
>> c0.',Beta.'
ans =
0.6438 0.7292 -0.2038 -0.4317
ans =
0.2564 0.7363 0.0000 -0.4029

カテゴリ

ヘルプ センター および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

製品

リリース

R2013a

タグ

質問済み:

2020 年 8 月 12 日

編集済み:

2020 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by