% % Script to iterate a difference equation and check against exact solution % % % First enter the right number of initial conditions (note MATLAB array % indices must start at 1; this has the effect that n must be changed to % (n-1) in the formula for the exact solution): % x(1)=1; x(2)=2; phi=pi/4; % (For exact solution) tworoot2=2*sqrt(2); % (Ditto) for n=3:20 x(n)=4*x(n-1)-8*x(n-2); exact=tworoot2^(n-1)*cos((n-1)*phi); fprintf('n: %4i FDE: %10.6g Formula: %10.6g\n',n,x(n),exact) end