Clear Filters
Clear Filters

Best fit line for log scale Y-axis and linear X-axis. I also want to extrapolate this line.

6 views (last 30 days)
I want to plot 'life' (in log scale) vs E in linear (x-axis) and then extrapolate this until E=3. Below is my current code. The points are fine, but the line is curved instead. The best line should be straight and so the extrapolation would also get a higher Y axis value.
{ life = [p10(1); p9_7(1); p9_4(1); p9_1(1); p8_6(1); p8_3(1); p8_0(1)];
E = [10; 9.7; 9.4; 9.1; 8.6; 8.3; 8.0];
figure;
coef_fit = polyfit(E,life,1);
xi = linspace(3,10,100);
yi = polyval(coef_fit,xi);
plot(E,life,'x',xi,yi, '--');
ax=gca;
ax.YScale='log'; }

Accepted Answer

David Goodmanson
David Goodmanson on 8 Mar 2018
Hello Tsalsabilla
'life' is proportional to exp(-E), meaning that log(life) is proportional to E. E and log(life) have a linear relationship, so E vs. log(life) is what you want to do the polyfit on. After you fit log(life), you exponentiate it to get back to 'life', as follows.
E = [10; 9.7; 9.4; 9.1; 8.6; 8.3; 8.0];
life = flip([53; 40; 20; 1.7;.48; .15; .06]); % estimates off of the plot
loglife = log(life);
coef_fit = polyfit(E,loglife,1);
xi = linspace(3,10,100);
loglife_fit = polyval(coef_fit,xi);
semilogy(E,life,'x',xi,exp(loglife_fit),'--')
However, once you have the plot, you will see how dangerous it is to extrapolate all the way down to E = 3. A small difference in the slope of the fit line is going to make a very large difference in the predicted lifetime.
  2 Comments
David Goodmanson
David Goodmanson on 19 Mar 2018
Edited: David Goodmanson on 19 Mar 2018
Hi alsa, not so obvious. Easy enough to make the required vectors of upper and lower values for the error bars, 1.05*life and life/1.05. Then see
https://www.mathworks.com/matlabcentral/answers/324880-how-to-add-error-bars-to-a-semilogy-plot#answer_254732
for a solution.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!