How to use Nlinfit for a function with two independent variables?
조회 수: 7 (최근 30일)
이전 댓글 표시
Hi here is my data and code and I am trying to predict parameters for a function with two independent variables but Nlinfit is giving me error.
clc
clear %all; % Clear the workspace.
close all; % Close all figures.
format compact
%% Read in data
data =readmatrix('Flexible_BUC.xlsx');
x1=[7.50000000000000
7.50000000000000
7.70000000000000
5
5
5
5
5
5
5
5
5
7.50000000000000
7.50000000000000
7.50000000000000
7.50000000000000
7.50000000000000
6
6
6
6
6
6
3.75000000000000
3.75000000000000
3.75000000000000
3.75000000000000
3.75000000000000
8
8
8
8
8
8
7.50000000000000
7.50000000000000
7.50000000000000
8
8
8];
x2=[0.00153000000000000
0.00522000000000000
0.000189000000000000
3.73000000000000e-06
3.73000000000000e-06
1.17000000000000e-05
8.66000000000000e-05
1.17000000000000e-05
1.51000000000000e-05
2.99000000000000e-05
7.00000000000000e-05
7.92000000000000e-05
6.06000000000000e-05
0.000163000000000000
0.000656000000000000
0.000818000000000000
0.00129000000000000
9.78000000000000e-06
0.000161000000000000
0.000183000000000000
0.000204000000000000
0.000297000000000000
0.000343000000000000
0.00705000000000000
0.00850000000000000
0.0233000000000000
0.0250000000000000
0.0267000000000000
0.00125000000000000
0.00245000000000000
0.00391000000000000
0.00878000000000000
0.00111000000000000
0.00122000000000000
3.23000000000000e-05
4.26000000000000e-05
4.53000000000000e-05
4.13000000000000e-05
8.24000000000000e-05
8.33000000000000e-05];
yobs=[5.95833333300000
0.300000000000000
0.0625000000000000
0.111111111000000
0.213809289000000
0.140625000000000
0.651315789000000
0.351694915000000
12.0555555600000
0.626846311000000
0.555555556000000
0.136363636000000
6.38793103400000
0.233051458000000
0.540000000000000
0.0277777780000000
1.05555555600000
0.113636364000000
0.0933323590000000
0.352272727000000
3.20833333300000
0.897435897000000
1.17046404700000
1.41666666700000
1.79545454500000
1.15384615400000
1.85576923100000
10.8333333300000
0.848684211000000
6.18835443000000
0.767441860000000
0.527777778000000
1.54872306000000
0.337691494000000
1.08333333300000
1.87477002000000
1.81654734900000
1.97222222200000
0.550000000000000
0.340020401000000];
xm=[x1 x2];
%% Initial parameter guesses
C1=0.25;
C2=0.73;
beta0(1)=C1; %initial guess beta 1
beta0(2)=C2; %initial guess beta 2
p=length(beta0); %p = # parameters
%% define function to be used for inverse problem
fINV=@Project_funcINV;
%fnameINV=@forderexpINV;
[beta,resids,J,COVB,mse] = nlinfit(xm,yobs,fINV,beta0);
beta
%% Functions
function y=Project_funcINV(beta0,x1,x2)
c2s=@(x)-2.40874-39.748*(1+x).^-2.856;
y=100./(1+exp(-beta0(1).*c2s(x1)+(beta0(2).*c2s(x1).*log10(100.*x2))));
end
Someone can please help, I would appreciate it.
댓글 수: 0
채택된 답변
Star Strider
2023년 2월 23일
You are using the correct approach with:
xm=[x1 x2];
In the function, refer to ‘x1’ as ‘xm(:,1)’ and ‘x2’ as ‘xm(:,2)’ , passing ‘xm’ as the independent variable to ‘Project_funcINV’. I made those changes, and added a fitnlm call to display the statistics, and provided a plot of the data and the fit to it (as a line plot).
Try this —
x1=[7.50000000000000
7.50000000000000
7.70000000000000
5
5
5
5
5
5
5
5
5
7.50000000000000
7.50000000000000
7.50000000000000
7.50000000000000
7.50000000000000
6
6
6
6
6
6
3.75000000000000
3.75000000000000
3.75000000000000
3.75000000000000
3.75000000000000
8
8
8
8
8
8
7.50000000000000
7.50000000000000
7.50000000000000
8
8
8];
x2=[0.00153000000000000
0.00522000000000000
0.000189000000000000
3.73000000000000e-06
3.73000000000000e-06
1.17000000000000e-05
8.66000000000000e-05
1.17000000000000e-05
1.51000000000000e-05
2.99000000000000e-05
7.00000000000000e-05
7.92000000000000e-05
6.06000000000000e-05
0.000163000000000000
0.000656000000000000
0.000818000000000000
0.00129000000000000
9.78000000000000e-06
0.000161000000000000
0.000183000000000000
0.000204000000000000
0.000297000000000000
0.000343000000000000
0.00705000000000000
0.00850000000000000
0.0233000000000000
0.0250000000000000
0.0267000000000000
0.00125000000000000
0.00245000000000000
0.00391000000000000
0.00878000000000000
0.00111000000000000
0.00122000000000000
3.23000000000000e-05
4.26000000000000e-05
4.53000000000000e-05
4.13000000000000e-05
8.24000000000000e-05
8.33000000000000e-05];
yobs=[5.95833333300000
0.300000000000000
0.0625000000000000
0.111111111000000
0.213809289000000
0.140625000000000
0.651315789000000
0.351694915000000
12.0555555600000
0.626846311000000
0.555555556000000
0.136363636000000
6.38793103400000
0.233051458000000
0.540000000000000
0.0277777780000000
1.05555555600000
0.113636364000000
0.0933323590000000
0.352272727000000
3.20833333300000
0.897435897000000
1.17046404700000
1.41666666700000
1.79545454500000
1.15384615400000
1.85576923100000
10.8333333300000
0.848684211000000
6.18835443000000
0.767441860000000
0.527777778000000
1.54872306000000
0.337691494000000
1.08333333300000
1.87477002000000
1.81654734900000
1.97222222200000
0.550000000000000
0.340020401000000];
xm=[x1 x2];
%% Initial parameter guesses
C1=0.25;
C2=0.73;
beta0(1)=C1; %initial guess beta 1
beta0(2)=C2; %initial guess beta 2
p=length(beta0); %p = # parameters
%% define function to be used for inverse problem
fINV=@Project_funcINV;
%fnameINV=@forderexpINV;
[beta,resids,J,COVB,mse] = nlinfit(xm,yobs,fINV,beta0);
beta
mdl = fitnlm(xm,yobs,fINV,beta0) % ADDED
figure % ADDED
stem3(x1, x2, yobs, 'filled')
hold on
plot3(x1, x2, Project_funcINV(beta,xm), '-r')
hold off
%% Functions
function y=Project_funcINV(beta0,xm)
c2s=@(x)-2.40874-39.748*(1+x).^-2.856;
y=100./(1+exp(-beta0(1).*c2s(xm(:,1))+(beta0(2).*c2s(xm(:,1)).*log10(100.*xm(:,2)))));
end
The fit is reasonably good, although ‘beta(2)’ may not be significnatly different from zero.
.
댓글 수: 13
Walter Roberson
2023년 2월 27일
Alex uses a commercial program named 1stOpt that does some very nice optimization. Sometimes I am able to improve a little on his results, but not usually, and when I do manage then it is only after a couple of days of continuous computations.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Descriptive Statistics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!