add lsline or trend line to log-log graph

i want to draw a least square line to log-log plot i am using following scripy
wet=[120 49 30 21 12 10 9 7 4];
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
scatter(x1,wet);
set(gca,'XScale','log');
set(gca,'YScale','log');
lsline
but it is not working. is their any other way to draw a line which is straight pass through the points

 채택된 답변

Star Strider
Star Strider 2015년 3월 8일

0 개 추천

This works:
wet=[120 49 30 21 12 10 9 7 4];
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
scatter(x1,wet);
set(gca,'XScale','log');
set(gca,'YScale','log');
b = polyfit(log(x1), log(wet), 1);
wetfit = exp(b(2)) .* x1.^b(1);
hold on
plot(x1, wetfit)
hold off
producing:

댓글 수: 12

Nabeel
Nabeel 2015년 3월 8일
thanks it worked can i extend it to x-axis
Nabeel
Nabeel 2015년 3월 8일
can it also be used to dry scatter plot.
Star Strider
Star Strider 2015년 3월 8일
편집: Star Strider 2015년 3월 8일
My pleasure.
This code will plot (extrapolate) to intercept the x-axis (where wet=1). The x-intercept is calculated in ‘xint’:
wet=[120 49 30 21 12 10 9 7 4];
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
b = polyfit(log(x1), log(wet), 1);
wetfit = exp(b(2)) .* x1.^b(1);
xint = (1/exp(b(2)))^(1/b(1)); % ‘x’-Intercept
x1vec = linspace(min(x1), xint, 10); % Extrapolated ‘x1’
wetext = exp(b(2)) .* x1vec.^b(1); % Extrapolated ‘wet’ Fit
figure(1)
scatter(x1,wet);
set(gca,'XScale','log');
set(gca,'YScale','log');
hold on
plot(x1vec, wetext)
hold off
It won’t work for ‘dry’ because ‘dry’ has zero values. Since log(0)=-Inf, you cannot do a log-log fit or plot with zero-valued data. You have to decide what you want to do about the zero values first.
Nabeel
Nabeel 2015년 3월 9일
thanks again if i truncate 0 values the code for wet will work for dry as well? when i remove 0 from dry first code has worked and line touches the axis because of 1
can i label only dry and wet leaving dry trend and wettrend again
lot of thanks helping me
Star Strider
Star Strider 2015년 3월 9일
편집: Star Strider 2015년 3월 9일
My pleasure!
I am not certain what you want to do with your legend, but if you want to label only the points and not the lines, the easiest way is to plot them in this order:
figure(1)
scatter(x1, wet, 'ob')
hold on
scatter(x1, dry, 'or')
plot(x1vec, wetext)
plot(x1vec, dryext)
hold off
and then only include the first two in the legend:
legend('Wet','Dry','Location','NE')
You can fit all the ‘dry’ data, but not using polyfit. You have to use a nonlinear curve fitting routine.
Using fminsearch:
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
x1dry = linspace(min(x1), max(x1));
pwrfit = @(b,x) b(2) .* x.^b(1);
OLSCF = @(b) sum((dry-pwrfit(b,x1)).^2);
B = fminsearch(OLSCF, [-2; 50]);
figure(2)
loglog(x1, dry, 'or')
hold on
plot(x1dry, pwrfit(B,x1dry), '-r', 'LineWidth',2)
hold off
You can of course do this with the ‘wet’ data as well.
Nabeel
Nabeel 2015년 3월 12일
it is possible to fine R-squared for wet and dry data sets Thanks
Star Strider
Star Strider 2015년 3월 12일
I would use the corrcoef function on the log-transformed variables.
Nabeel
Nabeel 2015년 3월 13일
in equations
wetfit = exp(b(2)) .* x1.^b(1);
exp(b(2)) is the slope of equation?
Star Strider
Star Strider 2015년 3월 13일
In the nonlinear equation, ‘b(1)’ corresponds to the slope in the log-transformed equation, and ‘exp(b(2))’ corresponds to the intercept. (In my ‘pwrfit’ function, I kept the same representation for consistency.)
Nabeel
Nabeel 2015년 3월 13일
there is a function of r-squared at http://www.mathworks.com/matlabcentral/fileexchange/34492-r-square--the-coefficient-of-determination i want to use this function for the code you have given to me. how can it be used.. thanks
Nabeel
Nabeel 2015년 3월 14일
hi, i have applied the following code to my original data. wet=[807 427 268 130 83 41 27 14 5 4 2 3 1]; x1=[1 2 3 4 5 6 7 8 9 10 11 12 13]; dry=[494 332 245 150 126 78 69 48 38 35 26 21 21 22 9 9 13 7 10 3 4 5 4 3 5 3 2 3 2 1 5 1 2 4 2 3 1 1 1 1 1 1]; x2=[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33 34 36 37 38 40 42 44 46 48 53]; bwet = polyfit(log(x1), log(wet), 1); wetfit = exp(bwet(2)) .* x1.^bwet(1); wetslope=bwet(1); xintwet = (1/exp(bwet(2)))^(1/bwet(1)); m=length(x1)+1; x1vecwet = linspace(min(x1), xintwet, m); wetext = exp(bwet(2)) .* x1vecwet.^bwet(1); figure(1) scatter(x1,wet,'filled','k'); hold on plot(x1vecwet, wetext,'r','LineWidth',2.5,'LineStyle','--') scatter(x2,dry,'d'); set(gca,'XScale','log'); set(gca,'YScale','log'); bdry = polyfit(log(x2), log(dry), 1); dryfit = exp(bdry(2)) .* x2.^bdry(1); dryslope=bdry(1); plot(x2, dryfit,'LineWidth',2.5) hold off
it has r-squared of 0 for both wet and dry :-( (This a a rainfall wet and dry days) i am not a regular programmer....
Star Strider
Star Strider 2015년 3월 14일
The code in the FEX contribution seems to use the same algorithm mine does for R-squared. Mine uses the nonlinear fit and the OLSCF (Ordinary Least Squares Cost Function) to generate the ‘norm(Y-X)’ data.
I didn’t run the FEX submission, but I trust my code.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Log Plots에 대해 자세히 알아보기

질문:

2015년 3월 8일

댓글:

2015년 3월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by