How can I extend polyfit line on loglog scale ?

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)

 Accepted Answer

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 Comments

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

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!