Math 310, Fall 1999
Bueler

Printing and plotting polynomials...

Suppose you want to print out some polynomials.  For instance, in Assignment #3 you will find coefficients for the cubic splines for Snoopy's nose and paw.  If you want to see this list of coefficients written out as polynomials, you would type:
x=[27.7 28 29 30];
a=[4.1 4.3 4.1];
b=[0.749 0.503 -0.781];
c=[0.0 -0.819 -0.470];
d=[-0.910 0.116 0.157];

for i=1:3
   ['p_' num2str(i) '(x) = ' num2str(a(i)) ' + ' ...
         num2str(b(i)) ' (x - ' num2str(x(i)) ') + ' ...
         num2str(c(i)) ' (x - ' num2str(x(i)) ')^2 + ' ...
         num2str(d(i)) ' (x - ' num2str(x(i)) ')^3']
end;
Note that the main line which prints out the polynomial has no ";", so that it is displayed.  Also, one types "..." to continue a line onto the next.

Now, a related job is to plot the function defined piecewise by these polynomials.  Note that we want to use the values of  x(i)  to determine the range of each plot.  Furthermore we want to plot several pieces on the same graph, so we use the "hold" command.  Thus we get a piece of code which looks like:

x=[27.7 28 29 30];
a=[4.1 4.3 4.1];
b=[0.749 0.503 -0.781];
c=[0.0 -0.819 -0.470];
d=[-0.910 0.116 0.157];

for i=1:3
   XX=x(i):.01:x(i+1);   % approximately 100 points plotted for each piece
   YY=a(i)+b(i)*(XX-x(i))+c(i)*(XX-x(i)).^2+d(i)*(XX-x(i)).^3;
   plot(XX,YY);
   hold on;
end;
hold off
Run this--you will get the arc of Snoopy's nose and paw (compare to page 157).