How can I extend polyfit line on loglog scale ?

7 次查看(过去 30 天)
I wrote this code, but I need to extend this line from y=10 to y=100. I tried but I couldn't do it. Can anybody help ?
x= [0.310 0.220 0.125 0.065]
y= [95.4 63.3 36.8 22.4]
X=log10(x); Y=log10(y);
P=polyfit(X,Y,1);
yhat=10.^[polyval(P,[X(1) X(end)])];
loglog(x,y,'s')
hold on
loglog([x(1) x(end)],yhat)
xlabel('(mm)')
ylabel('(%)')
grid on
slope = P(1)
txt = ['m=' num2str(P(1))]
text(0.4,40,txt)

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2022-1-6
When you calculate yhat you only do that for the first and last element of your x-values that you use for the fit. You should try to extend the range of x-values and calculate yhat-values for a larger range. Something like this might help you on your way:
x= [0.310 0.220 0.125 0.065]
y= [95.4 63.3 36.8 22.4]
X=log10(x); Y=log10(y);
P=polyfit(X,Y,1);
Xi = log10(linspace(min(x)/10,max(x)*10),101); % New variable to use for the evaluation of the fit
yhat=10.^[polyval(P,Xi)];
loglog(x,y,'s')
hold on
loglog(10.^Xi,yhat) % Modified plotting.
xlabel('(mm)')
ylabel('(%)')
grid on
slope = P(1)
txt = ['m=' num2str(P(1))]
text(0.4,40,txt)
From that you might need to adjust the Xi-variable and zoom in the plot.
HTH
  2 个评论
A. Panton
A. Panton 2022-1-6
Thank you very much, but the log10 line gives an error I think it should be like this
Xi = log10(linspace(min(x)/10,max(x)*10,101)); % New variable to use for the evaluation of the fit

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by