%Lesson 3c - Math 151 %Objective is to illustrate ability to do %a linear regression on a semilog plot. %This is for equations like % y = a (b^x) for constants a,b. % This is the same as % N(t) = N(0) exp(k t) for a constant k. % k>0 gives exponential growth % k<0 gives exponential decay. %First read in data from a file % load b:semilog.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 % lesson3c %where lesson3c.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 that the above plot is the same % as plotting x versus log(y) from the below % we let ly = vector of log(y) values base 10 ly=log10(y),pause plot(x,ly,'+') title('x versus log(y)'),pause % Now do a linear regression of the % x versus log(y) plot c=polyfit(x,ly,1),pause % here c(1) is the slope of the regression % which for the above equation is log10(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 x versus log(y) m=[min(x) max(x)],pause ym=polyval(c,m),pause plot(x,ly,'+',m,ym) title('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=10^c(1),pause for i=1:len yfit(i)=a*b^x(i); end plot(x,y,'+w',x,yfit) title('x versus y and fit'),pause