%
% locallin.m - This MATLAB file illustrates local linearization
% by graphing a function, it's linearization about a point, and
% showing the fraction error in using the linearization. The
% function used is the general 3th order polynomial
% y = a x^3 + b^x^2 + c x + d
% The user is prompted for the coefficients and for
% the x value around which to do the linearization,
% as well as the range of x values over which to display 
% the graphs.
!c:
s=1;
n=100;
while s>0;
a=input('input a the coefficient of x^3:    ')
b=input('input b the coefficient of x^2:    ')
c=input('input c the coefficient of x:    ')
d=input('input d the constant term:    ')
x0=input('input x0 around which to linearize:     ')
xr=input('input xr for range to plot around x0:     ')
del=xr/n;
slope=3*a*x0^2+2*b*x0+c
y0=a*x0^3+b*x0^2+c*x0+d
xstart=x0-xr/2;
y=zeros(n+1,1);
lin=zeros(n+1,1);
xerr=zeros(n+1,1);
x=zeros(n+1,1);
for i=1:n+1
x(i)=xstart+(i-1)*del;
lin(i)=y0+slope*(x(i)-x0);
y(i)=a*x(i)^3+b*x(i)^2+c*x(i)+d;
err(i)=(lin(i)-y(i))/y(i);
end
plot(x,lin,x,y)
title('graph and linear approx'),pause
plot(x,err)
title('fractional error of approx'),pause
s=input('Do you want to stop - if so enter 0')
end

