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
2015년 3월 8일
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
2015년 3월 8일
thanks it worked can i extend it to x-axis
Nabeel
2015년 3월 8일
can it also be used to dry scatter plot.
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
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
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
2015년 3월 12일
it is possible to fine R-squared for wet and dry data sets Thanks
Nabeel
2015년 3월 13일
in equations
wetfit = exp(b(2)) .* x1.^b(1);
exp(b(2)) is the slope of equation?
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
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
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....
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에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
