% QUESTION 1 % The operator * is the usual multiplication, which by default % applies to two matrices, e.g: A*B returns the product of two % matrices A and B. With the dot, .* multiplies componentwise, % i.e.: when C = A*B, then C(i,j) = A(i,j)*B(i,j). % We use the operator * to compute the residual of a solution % to a linear system A*x = b, e.g.: b - A*x gives an idea how % accurate the solution is. % The operator .* is used to evaluate a formula, sampled for % plotting, e.g.: z = x.*y samples the surface z = x y. % QUESTION 2 t = 0:0.1:25; r = sqrt(t); polar(t,r); % QUESTION 3 r = -pi:pi/20:pi; [x,y] = meshgrid(r,r); z = y.^2.*cos(x+y); mesh(x,y,z); % QUESTION 4 % (a) subplot(2,1,1); t = -1:0.01:1; x = t; y = t.^2; z = t.^3; plot3(x,y,z); % (b) subplot(2,1,2); r = -1:0.01:1; [x1,y1] = meshgrid(r,r); z1 = x1.^3; mesh(x1,y1,z1); view(3); hold on [x2,z2] = meshgrid(r,r); y2 = x1.^2; mesh(x2,y2,z2); % QUESTION 5 % The three commands we have seen to encounter approximate data % are polyfit, spline, and fft. Their application domains are % the following: % polyfit: hypothesis testing, least squares fitting % (see answer to QUESTION 6); % spline: geometric design (see answer to QUESTION 6); % fft: signal processing, filtering of noise from signal by % the computation of the spectrum via discrete Fourier transform % implemented in fft. % QUESTION 6 % (a) The command polyfit creates a polynomial of a given degree % which fits given data, using least squares. % The command spline approximates given data using a piecewise % function, where for each piece typically a cubic polynomial is used. % (b) The command polyfit is useful to discover a relationship between % two variables x and y, to test a hypothesis on measured data, % e.g.: does y depend linearly or quadratically on y? % Since the degree is most important here, spline is inappropriate here. % (c) The command spline is used in computer aided geometric design, % given some control points, we can draw a shape of an object. % Since in the design we want the adding of new points to have only % a local effect, constructing a high degree polynomial as with polyfit % is not appropriate for this type of application. % QUESTION 7 % (a) the file simpson.m: function y = simpson(f,a,b) % returns an approximation for the integral of f(x) over [a,b] % using Simpson's rule fa = feval(f,a); fm = feval(f,(a+b)/2); fb = feval(f,a); y = (b-a)*(fa+4*fm+fb)/3; % (b) simpson('cos',pi/4,pi/2) % QUESTION 8 t = 0:1/30:1; y = sin(2*pi*t); plot(t,y); axis([0 1 -2 2]; % Taking a step size of 1/30, % -- or 30+1 smaples in [0,1[ -- % gives a good plot for sin(2*pi*t) % 1. we need 3*30+1 = 91 samples for sin(2*pi*t) in [0,3] % and 10*91 = 910 samples for sin(2*pi*10*t) in [0,3]: t = 0:1/900:3; y = sin(2*pi*t); hold on plot(t,y); % 2. for any k, w will need k*91 samples to have a good plot % of sin(2*pi*k*t) in [0,3], and use t = 0:1/(k*90):3; % QUESTION 9 p3 = [ 0 0 1; 0 1 0; 1 0 0]; p3*[1:3]' % the answer for any n, e.g.: n = 31; rows = [1:n]; cols = [n:-1:1]; p31 = sparse(rows,cols,1); p31*[1:n]' % QUESTION 10 [x y z] = sphere(30); x1 = .1*x; y1 = .1*y - 2; z1 = .1*z + 2; figure hold on surf(x,y,z); surf(x1,y1,z1); axis off % QUESTION 11 f = [-23 -44]; A = [1 12; 24 30; -2 83; -1 0; 0 -1]; b = [123 912 134 0 0]'; s = linprog(f,A,b) format bank s A*s - b % the place of the zero elements in the answer % tell where the answer s reaches equality