Clear Filters
Clear Filters

add lsline or trend line to log-log graph

10 views (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 8 Mar 2015
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 Comments
Nabeel
Nabeel on 14 Mar 2015
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 on 14 Mar 2015
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!