echo on % This causes all steps and commenst to be shown/displayed. % % % This is a commented version (w/ a few add-ons) of the % file hello.m % The "problem" is to fit (interpolate) a number of data % points with a polynomial function. There are built-in % MATLAB functions (see at the very end) that complete % this task with a few key-strokes ... but here the idea % is to learn the basic commands on a reasonable problem. % % The first item is to learn about m-files, scripts, and % setting the path so that MATLAB can find the file..... % % Then enter the data points -- as column vectors. Note the % two different formats used: The x-values are equally % spaced and easily generated, the y-values have to be % typed in (unless they may be read in from a file). % xx=[1:1:8]'; yy=[1,0,2,3,1,9,6,0]'; % % Always "take a look", plot the data -- e.g. to enjoy the % picture, to get an idea what is going on, to plan ahead, % and to detect possible mistakes when entering the data. % % First clear the graphics window -- clg is an alternative, % try out "help clf" and "help clg" to find out the diffe- % rences. % Then plot the data -- enjoy the color and style options. % For details look up "help plot" clf plot(xx,yy,'ro') % % The main business is to create the matrix of coefficients. % Here we start by reserving the space -- first create a matrix % of only ones (similar try out "help zeros" and "help eye"). % A=ones(8,8); % % The second column should contain the values of the x-data. % Assign data to a column using A(:,k)=..... % A(:,2)=xx; % % The subsequent columns contain increaisng powers of the % values of the second column. There are many ways to create % these data -- we use it as a typical example for "array % multiplication" -- i.e. ".*" as opposed to "*"..... % A(:,3)=xx.*xx; A(:,4)=xx.*xx.*xx; % % A different way uses the previous column.... % A(:,5)=A(:,4).*xx; % % It is easy to use CNTRL-C, CNTRL-V and editing to get the % remaining columns -- but the last attempt suggests to simply % use a for-loop. This is not mandatory for our class, but % a very basic programming construct..... it is worth learning % the syntax for this in ANY language! % for i=3:8 A(:,i)=A(:,i-1).*xx; end % % Solving the system to obtain the coefficients is a real JOY, % a minimal number of keystrokes -- dividing A (from the left) % into y % cc=A\yy % % These are the coefficients of the polynomial function that we % were after! But to get a nice closure, let is overlay the % graph of the polynomial over the data points. % First we need to create the "vector" of x-values at which % we want to evaluate and plot the polynomial -- we are in % charge, MATLAB ONLY plots and connects points -- we have % to create the table of (x,y) pairs to bve plotted. First % the x-values in small steps as a column vector. % xxx=[0.8:0.1:8.4]; % % Then evaluate the polynomial at these x-values, i.e. % substitute the x-values into the polynomial. % The following is a very crude way of doing this -- there % are more efficient ways, but it is good for a start: % What matters is that we can USE array-operations (either % ".*" or ".^") to calculate all y-values from all x-values % basically "at the same time" (both xxx and yyy are long % column vectoirs of data). % yyy=cc(1)+... cc(2)*xxx+... cc(3)*xxx.^2+... cc(4)*xxx.^3+... cc(5)*xxx.^4+... cc(6)*xxx.^5+... cc(7)*xxx.^6+... cc(8)*xxx.^7; % % To overlay a plot use "hold on" -- see "help hold" for % details, and plot the interpolating curve in your favorite % color and style. % hold on plot(xxx,yyy,'m-') % % Faster commands use built in functions -- strike any % key to continue: pause % % There are many matrices that occur frequently, and that % have earned their own names. MATLAB has built-in functions % that create most of these, it is just a matter of learning % many names, looking around..... % Our matrix A is a "Vandermonde-matrix", and is easily % generated with a few keystrokes. However, the built-in % function has the matrix flipped right-to-left (this % corresponds to listing the powers c_k*x^k in the polynomial % in decreasing rather than increasing order -- hence we "flip % the matrix right-left".... % A=fliplr(vander([1:8])) % %Strike any key to continue: pause % % But it is even easier -- the command PLOYFIT will find the % (coefficients c_k of the) polynomial in one step. % ccc=polyfit(xx,yy,7) % % Note that these coefficients are again listed in reverse order % (compared to our hands-on attempt). And they come as a row % vector, rather than a column vector. Taking transposes (') and % flipping right-to-left or up-down easily converts the format. % %Strike any key to continue: pause % % Finally, given (the coefficients c_0, c_1,.... c_n of a % polynomial the function POLYVAL will evaluate the polynomial % at any (vector of) x-values --- i.e. there is no need to % laboriously type in the formula for the polynomial! % % The main issues are whether the coeffiencts are to be given % in descending or ascending order of the powers, and whether % the vectors ccc of coefficients and the vector xxx of x-values % have to be column or row vectors -- for such issues it is best % to quickly visit the help pages "help polyval" % yyy = polyval(ccc,xxx); plot(xxx,yyy,'b-.') % echo off %