%Math 128B Spring 2005 %Hmwk1 - Solutions (Jan. 26, 2005) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function CosInterval() %see Help: "Plot[2]" %create vector of equally spaced x-values x = linspace(-1,1); %create vector of function values (y-values) y = cos(x); %display the graph plot(x,y) %improve appearance axis([-1 1 0 1.01]) %provide some whitespace above graph ylabel('cos(x)') title('Math128 - Hmwk1.1') text(-0.2,1/3,'{\it Graph of the cosine function}') return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %called with ssolve(10, 0.1, 30) and ssolve(4, -0.05, 50) function result = ssolve(a,b,n) %enable plotting within function for Problem #3 doPlot = 1; %create the matrix M M = eye(n); M(1,2) = a; M(n,1:n-1) = b; %create the row vector y y = 1:n; %solve M*result = y' result = M\y'; %WHY NOT inv(M)*y' ? %see Help: "division matrix(arithmetic...)" if doPlot plot([1:n],result); title('Math128 - Hmwk1.3') end return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function HInv = HilbertInv(n) %flag for enabling/disabling debug information Debug = 1; %create the Hilbert matrix H = zeros(n); for i=1:n for j=1:n %j=i:n could be used to avoid redundant computation H(i,j) = 1/(i+j-1); end %inner j-loop for columns end %outer i-loop for rows %return the inverse Hilbert matrix HInv = inv(H); if Debug %display this during debugging norm(H - hilb(n),inf) %compare our H with Matlab's norm(HInv - invhilb(n),inf) %compare our HInv with Matlab's end return