How to do custom equation (non linear) regression?
14 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Star Strider
2023-4-12
There are several nonlinear parameter estimation function to choose from.
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)
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 个)
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)
b = par(2)
figure(2)
plot(x,y,'or',x,modelfun(par,x),'k')
0 个评论
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 个评论
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!