From: srao Rao on
Hi,

Consider the example y = x^2, for e.g.
x = [1 2 3];
y = [1 4 9];
and plot loglog(x,y). Now if I use linear fit I must get a slope of 2. However the linear fit seems to work differently for the loglog plot. However if I just plot(log(x),log(y)) and use linear fit I get a slope of 2. How do I fit my data on the loglog plot to get a slope of 2?

Any help on this would be appreciated..Thanks..
From: John D'Errico on
"srao Rao" <sashankhrao(a)gmail.com> wrote in message <i1p3ec$7uh$1(a)fred.mathworks.com>...
> Hi,
>
> Consider the example y = x^2, for e.g.
> x = [1 2 3];
> y = [1 4 9];
> and plot loglog(x,y). Now if I use linear fit I must get a slope of 2.

NO.

You must not get a slope of 2 for a linear fit.
In fact, I would expect a slope of 4 for this case,
but not for any strong mathematical reason, but
only because that is the slope it will estimate.
Over some other range, you would get a different
model estimated.

polyfit(x,y,1)
ans =
4 -3.33333333333333

> However the linear fit seems to work differently for the loglog plot. However if I just plot(log(x),log(y)) and use linear fit I get a slope of 2. How do I fit my data on the loglog plot to get a slope of 2?
>

On a log-log plot, I'd expect to see a slope of 2.
because logging this expression yields

log(y) = 2*log(x)

therefore the log-log plot will be linear, with a slope
of 2.

polyfit(log(x),log(y),1)
ans =
2 -1.58969301072858e-16

The linear fit works as it should.

John
From: Sashankh Rao on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i1p8l8$3kl$1(a)fred.mathworks.com>...
> "srao Rao" <sashankhrao(a)gmail.com> wrote in message <i1p3ec$7uh$1(a)fred.mathworks.com>...
> > Hi,
> >
> > Consider the example y = x^2, for e.g.
> > x = [1 2 3];
> > y = [1 4 9];
> > and plot loglog(x,y). Now if I use linear fit I must get a slope of 2.
>
> NO.
>
> You must not get a slope of 2 for a linear fit.
> In fact, I would expect a slope of 4 for this case,
> but not for any strong mathematical reason, but
> only because that is the slope it will estimate.
> Over some other range, you would get a different
> model estimated.
>
> polyfit(x,y,1)
> ans =
> 4 -3.33333333333333
>
> > However the linear fit seems to work differently for the loglog plot. However if I just plot(log(x),log(y)) and use linear fit I get a slope of 2. How do I fit my data on the loglog plot to get a slope of 2?
> >
>
> On a log-log plot, I'd expect to see a slope of 2.
> because logging this expression yields
>
> log(y) = 2*log(x)
>
> therefore the log-log plot will be linear, with a slope
> of 2.
>
> polyfit(log(x),log(y),1)
> ans =
> 2 -1.58969301072858e-16
>
> The linear fit works as it should.
>
> John

Hi John,

Thanks for your reply.. Yes I made a mistake in saying that the slope would be 2 for the linear fit. Also I agree that the slope for the log plot should be 2 because log(y) = 2*log(x).. I want to know why I am not able to see a slope of 2 for the following input:

x = [1 2 3];
y = [1 4 9];
loglog(x,y);

Now having obtained the plot I want to see the equation of the line(which has the slope). I used to use the basic fitting tool and do the linear fit and check 'show equation'. Any suggestions on what I must do to view the equation on this log plot? Or do I have to manually place the equation on the plot? Thanks...
From: John D'Errico on
"Sashankh Rao" <sashankhrao(a)gmail.com> wrote in message <i1ph38$nn6$1(a)fred.mathworks.com>...

> Thanks for your reply.. Yes I made a mistake in saying that the slope would be 2 for the linear fit. Also I agree that the slope for the log plot should be 2 because log(y) = 2*log(x).. I want to know why I am not able to see a slope of 2 for the following input:
>
> x = [1 2 3];
> y = [1 4 9];
> loglog(x,y);
>
> Now having obtained the plot I want to see the equation of the line(which has the slope). I used to use the basic fitting tool and do the linear fit and check 'show equation'. Any suggestions on what I must do to view the equation on this log plot? Or do I have to manually place the equation on the plot? Thanks...

You don't get a slope of 2 because this is STILL a plot
of y versus x. The axes are scaled differently, but it is
still plotting y versus x. Look carefully at what is plotted.

Should matlab be prescient and know that you wanted
something different? Do you have the clairvoyance
toolbox installed? It is very expensive as an add on.

If you really wanted to fit log(y) as a function of log(x),
you just needed to do this:

plot(log(x),log(y))

or this

polyfit(log(x),log(y),1)

John
From: Sashankh Rao on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i1pigp$nha$1(a)fred.mathworks.com>...
> "Sashankh Rao" <sashankhrao(a)gmail.com> wrote in message <i1ph38$nn6$1(a)fred.mathworks.com>...
>
> > Thanks for your reply.. Yes I made a mistake in saying that the slope would be 2 for the linear fit. Also I agree that the slope for the log plot should be 2 because log(y) = 2*log(x).. I want to know why I am not able to see a slope of 2 for the following input:
> >
> > x = [1 2 3];
> > y = [1 4 9];
> > loglog(x,y);
> >
> > Now having obtained the plot I want to see the equation of the line(which has the slope). I used to use the basic fitting tool and do the linear fit and check 'show equation'. Any suggestions on what I must do to view the equation on this log plot? Or do I have to manually place the equation on the plot? Thanks...
>
> You don't get a slope of 2 because this is STILL a plot
> of y versus x. The axes are scaled differently, but it is
> still plotting y versus x. Look carefully at what is plotted.
>
> Should matlab be prescient and know that you wanted
> something different? Do you have the clairvoyance
> toolbox installed? It is very expensive as an add on.
>
> If you really wanted to fit log(y) as a function of log(x),
> you just needed to do this:
>
> plot(log(x),log(y))
>
> or this
>
> polyfit(log(x),log(y),1)
>
> John

Now I understand what you mean... Thanks John.