% MVW, 2007-09-05 % Cut and paste this text into Matlab % % Example Matlab program to print 20 values of a 2-term recurrence equation. % p0=1; p1=1/2; % initial two values n=0; while n<20, % compute 20 more values n=n+1; p=(3/2)*p1-(1/2)*p0; p0=p1; p1=p; p, % print the current value end % MVW, 2009-09-02 % Cut and paste this text into Matlab % % Another way to print 20 values of a 2-term recurrence equation % format short g; % short ==> 4 digits; g ==> scientific notation if needed p(1)=1; % p0 in text p(2)=1/2; % p1 in text n=2; while n<20, n=n+1; p(n)=(3/2)*p(n-1)-(1/2)*p(n-2); % recurrence equation end % % Reporting: % exact = (1/2).^(1:20); % exact values are known to be 1/2^n abserr = abs(p - exact); relerr = abs(p - exact )./exact; [(1:n)' p' exact' abserr' relerr']