function []=hw4_2_4(a,b,n) % makes an nxn Triadiagonal matrix with a's on the main diagonal % and b's above and below, and test the convergence of the geometric series % for different values of a and b % first make an n-long vector of all a's and an (n-1)-long vector of all b's u=a*ones(1,n); v=b*ones(1,n-1); % now use the command diag to put these vectors on the appropriate diagonals of % a matrix A A=diag(v,-1)+diag(u)+diag(v,1); % add up 5 and 10 terms of the series GS5=zeros(n); for k=0:5 GS5=GS5+A^k; end GS10=GS5; for k=6:10 GS10=GS10+A^k; end % compare the partial sum to the result of the geoemetric series theorem GS=inv(eye(n)-A); disp('error after 5 terms') disp(norm(GS-GS5)) disp('error after 10 terms') disp(norm(GS-GS10))