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 offRun this--you will get the arc of Snoopy's nose and paw (compare to page 157).