%Lesson 3d - Math 151
%Objective is to illustrate ability to do
%a linear regression on a loglog plot.
%This is for equations like
% y = a (x^b) for constants a,b.
%First read in data from a file
% load b:loglog.dat
%then read in a command file that is
%this file of commands for MATLAB
%by first telling MATLAB to use the b: drive
% !b:
%then telling it to run lesson3b
% lesson3d
%where lesson3d.m is a file on your
%floppy in the b: drive
who,pause
!c:
% First plot the raw data
plot(x,y,'+w')
title('x versus y'),pause
% Now plot the data on semilog scale
semilogy(x,y,'+')
title('x versus y semilog scale'),pause
% Can see the above doesn't help, so try
% plotting on a loglog scale
loglog(x,y,'+')
title('x versus y loglog scale'),pause
% Can see that the above plot is the same
% as plotting log(x) versus log(y) from the below
% we let ly = vector of log(y) values base 10
% and let lx = vector of log(x) values base 10
lx=log10(x),pause
ly=log10(y),pause
plot(lx,ly,'+')
title('log(x) versus log(y)'),pause
% Now do a linear regression of the
% log(x) versus log(y) plot
c=polyfit(lx,ly,1),pause
% here c(1) is the slope of the regression
% which for the above equation is b
% and c(2) is the intercept of the regression
% which for the above is log10(a)
% Now plot the regression line on plot
% of log(x) versus log(y)
m=[min(x) max(x)],pause
lm=log10(m)
ym=polyval(c,lm),pause
plot(lx,ly,'+',lm,ym)
title('log(x) versus log(y)'),pause
% Now to show original data and the fitted
% regression model, first set up the fitted
% model y values (called yfit)
len=length(x)
a=10^c(2),pause
b=c(1),pause
for i=1:len
yfit(i)=a*(x(i)^b);
end
plot(x,y,'+w',x,yfit)
title('x versus y and fit'),pause