How to do custom equation (non linear) regression?

조회 수: 15 (최근 30일)
yehuda kristo
yehuda kristo 2023년 4월 12일
댓글: Image Analyst 2023년 4월 12일
I need to find some constant from data that usually is shown in log-log scale, the equation related to the data would be y=(a*x^b)/(26.1-x). How do I find the a and b constants?

채택된 답변

Star Strider
Star Strider 2023년 4월 12일
There are several nonlinear parameter estimation function to choose from.
This uses fitnlm
yfcn = @(a,b,x) (a*x.^b)./(26.1-x);
T1 = readtable('experiment_data.xlsx');
x = T1.x;
y = T1.y;
B0 = rand(2,1);
mdl = fitnlm(x,y,@(b,x)yfcn(b(1),b(2),x), B0)
mdl =
Nonlinear regression model: y ~ yfcn(b1,b2,x) Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ _______ b1 0.059055 0.28207 0.20936 0.83475 b2 -1.6212 1.5325 -1.0578 0.29362 Number of observations: 75, Error degrees of freedom: 73 Root Mean Squared Error: 0.00156 R-Squared: -0.0655, Adjusted R-Squared -0.0801 F-statistic vs. zero model: 1.29, p-value = 0.282
xsrt = sort(x);
[ypred,yci] = predict(mdl,xsrt);
figure
plot(x, y, '.', 'DisplayName','Data')
grid
hold on
plot(xsrt, ypred, '-r', 'DisplayName','Function Fit')
plot(xsrt, yci, '--r', 'DisplayName','±95% Confidence Intervals')
hold off
legend('Location','best')
The model is a statistically poor fit to the data and does not describe the data well.
.
  댓글 수: 2
yehuda kristo
yehuda kristo 2023년 4월 12일
Oh okay I understand it now, Thanks for the explanation Sir.
Star Strider
Star Strider 2023년 4월 12일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Davide Masiello
Davide Masiello 2023년 4월 12일
Assume these are your experimental data
x = linspace(0,20,30);
y = rand(size(x))/3+(pi*x.^(sqrt(2)/2))./(26.1-x);
figure(1)
plot(x,y,'or')
To find a and b you can do the following.
modelfun = @(p,x) (p(1)*x.^p(2))./(26.1-x);
par = nlinfit(x,y,modelfun,[1 1]);
a = par(1)
a = 6.3320
b = par(2)
b = 0.4825
figure(2)
plot(x,y,'or',x,modelfun(par,x),'k')

Image Analyst
Image Analyst 2023년 4월 12일
I usually use fitnlm (fit non-linear model). You can specify the equation you want to fit to. I'm attaching some examples of fitnlm.
  댓글 수: 4
yehuda kristo
yehuda kristo 2023년 4월 12일
Here's my .m file and my x and y data
Image Analyst
Image Analyst 2023년 4월 12일
Well, looks like you're going to use Star's solution, so I won't bother, unless you really want me to.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by