vertical error bar in loglog plot

Hi
I am looking for a way to include error bars (vertical) in my plot from the codes that I am having now.
x=[20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37];
e=[2100 1750 1400 1150 1100 1000 760 620 570 500 450 420 320 310 290 210 220 170];
loglog(x,e,'bo');
hold on
Const1 = polyfit(log(x),log(e), 1);
m1 = Const1(1); k1 = Const1(2);
A = x.^m1.*exp(k1);
loglog(x, A,'k--');
xlim([10 40]);
grid on
Thanks a lot for your kind help!

 Accepted Answer

dpb
dpb on 22 Jun 2019
Edited: dpb on 22 Jun 2019
hEB=errorbar(x,A,err);
hAx=gca;
hAx.XScale='log';
hAx.YScale='log';
You'll have to have defined the error magnitude array, err, of course.

4 Comments

Hi dpb
Thanks for the solutions! At least it works now that I can already see the error bars being plotted. Unfortunately they appear to be extremely big, that I doubt whether I have defined the error magnitude "err" correctly.
Initially, to be honest, I got the whole codes for fitting my curve from this website too. Not hundred-precently understood every single line, but they seem to work with my own data. Now that I am not sure on how the error magnitude should be defined. I have been doing curve fit and plotting error bars in regular scales, but this is the first time that I need to do it in loglog scale! Any idea how should the error magnitude be defined correctly?
Thanks a lot!
Capture.PNG
Well, you've not given any information on how you did calculate an error or what the error is, so "I dunno!" :) The log scale amplifies the visual aspect on the negative side but that is simply an artifact of being a logarithmic scale so can't use that as the criterion of whether the values are, or are not, correct.
But, I will note that the error magnitudes you're showing are about the size of the observation and it appears that are actually larger than the observation at some locations so the negative error bar end value was negative and so not drawn--I presume you'll have gotten the warning message of "Negative data ignored"?
So, I'd guess probably the error values aren't what you're intending to show, but I don't what you should have used instead without more information.
One minor note on Matlab syntax with polyval for logarithmic fitting as you've done, you can write the evaluation somewhat simpler as
b=polyfit(log(x),log(e), 1); % fit log linear model
ehat=exp(polyval(b,log(x))); % evaluate, pass log(x) --> y=K*x^m
Thanks a lot!! Now it works after used the simpler evaluation as you suggested! :D
Capture.PNG
That alone should make no difference--the ehat values above are the same as your A array to machine rounding accounting for the difference in computational sequence between the two expressions.
It looks like you now did pass err as the fitting residual on a point basis whereas the previous plot looked more like you had subtracted the residual from the value and used that (altho that didn't reproduce your figure exactly, it was much more similar, so not sure just exactly what you had done).
What you might really be wanting, however, is the RMS error of the overall fit, or a prediction error for the mean or an individual prediction...but, that's entirely up to you depending upon what error it is that you're interested in and for what purpose.

Sign in to comment.

More Answers (0)

Products

Asked:

on 22 Jun 2019

Commented:

dpb
on 23 Jun 2019

Community Treasure Hunt

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

Start Hunting!