------------------------------------------------------------------------------ Section A, question 1 --------------------- make a file q1.m with the contents: function an=q1(t) an = sqrt( cos(t).^2 + sin(t.*(1-t)/6).^2 + 144*t.^2./(t+6).^2 ); and then do quad( @q1, 0, 2*pi) ------------------------------------------------------------------------------ Section A, question 2 --------------------- x(t):=cos(t); y(t):=sin(t*(1-t)/6); z(t):=12*t/(6+t); evalf(int( sqrt(diff(x(t),t)^2+diff(y(t),t)^2+diff(z(t),t)^2), t=0..2*Pi )); ------------------------------------------------------------------------------ Section A, question 3 --------------------- make a file q3a.m with the contents: function z=q3a(x,p) % note here x is a vector! z = (x(1)^2+x(2)^2+p*x(1))/sqrt(1+(x(1)+2*x(2))^2+(x(1)-x(2))^2); make a file q3b.m with the contents: function z=q3b(p); [w z] = fminsearch(@q3a, [0,0], [] , p); ------------------------------------------------------------------------------ Section A, question 4 --------------------- make a file q4.m with the contents: function f=q4(p) f(1)=det( [1 2 p; -1 2 -1; 2 -5 4] ); f(2)=det( [1 2 1; -1 2 p; 2 -5 4] ); f(3)=det( [1 2 1; -1 2 -1; 2 -5 p] ); then do (for example) p=[-2:0.05:2]; f=zeros(3,size(p,2)); for i=1:size(p,2) f(:,i)=q4(p(i)); end plot( p , f(1,:), 'r', p , f(2,:), 'g', p ,f(3,:), 'b') ------------------------------------------------------------------------------ Section A, question 5 --------------------- line 1: loading the LinearAlgebra package to do linear algebra calculations line 2: Defining the matrix A line 3: Computing the determinant of the matrix A line 4: Definining the matrix B, by setting t=-8 in A, so the determinant of B will be zero line 5: Since the determinant of B is zero, its column space should be less than 3 dimensional. Here we find a basis for the column space line 6: Defininig two vectors v1, v2 line 7: Solving B x = v1, B x = v2 Clearly v1 is in the column space of B, v2 is not. So B x = v1 has an infinite number of solutions (parametrized by _t0_3) B x = v2 has no solutions (hence the error message) We are seeing the rult that if B has determinant zero, then the equation B x = v has either no solutions or an infinite number of solutions, depending on whether v is or is not in the column space of B. ------------------------------------------------------------------------------ Section B, question 1 --------------------- (a) file q1a.m with content: function M=q1a(M) % this is allowed ! small=Inf; small_row=0; small_col=0; for i=1:size(M,1) for j=1:size(M,2) if abs(M(i,j))>0 & abs(M(i,j))<small small=abs(M(i,j)); small_row=i; small_col=j; end end end M(small_row,small_col)=0; % doesn't work for zero matrix! (b) file q1b.m with content: function M=q1b(M) big = max(abs(eig(M))); M1=M; bignew=big; while( abs(bignew-big)/big < 0.01) M=M1; M1=q1a(M); bignew=max(abs(eig(M1))); end ------------------------------------------------------------------------------ Section B, question 2 --------------------- with(LinearAlgebra): findt:=proc(A,B) local theta,M,q,r; q:=<|<-sin(theta),cos(theta)>>; M:=A.q-q.B; r:=simplify( Trace(M.Transpose(M)) ); return(solve( diff(r,theta)=0, theta)); end proc; findt2:=proc(A,B) local theta,M,q,r,sols,i,d2; q:=< |<-sin(theta),cos(theta)>>; M:=A.q-q.B; r:=simplify( Trace(M.Transpose(M)) ); sols:=[solve( diff(r,theta)=0, theta)]; for i from 1 to nops(sols) do d2:=simplify(subs( theta=op(i,sols) , simplify(diff(diff(r,theta),theta)) )); if evalf(d2)<0 then print(theta=op(i,sols),"maximum"); elif evalf(d2)>0 then print(theta=op(i,sols),"mimimum"); else print(theta=op(i,sols)); end if end do; end proc; example: A:=<<1,2>|<3,2>>; B:=<<2,3>|<-1,2>>; findt(A,B); 1/2 1/2 [arctan(-5 + 26 ), -arctan(5 + 26 )] findt2(A,B); 1/2 theta = arctan(-5 + 26 ), "mimimum" 1/2 theta = -arctan(5 + 26 ), "maximum" ------------------------------------------------------------------------------ Section B, question 3 --------------------- f:=1/(1+x^2+y^2); g:=x^2+2*y^2+x*y-y; (a) m:=proc(theta) local r,s; s:=fsolve( subs(x=r*cos(theta),y=r*sin(theta),f) = subs(x=r*cos(theta),y=r*sin(theta),g) , r=0..2) ; return( s, evalf(subs(x=s*cos(theta),y=s*sin(theta),f)) ); end proc; (b) for i from 0 to 100 do temp:=[m(2*Pi*i/100)]; r[i]:=op(1,temp); F[i]:=op(2,temp); end do: plot( [ [seq([2*Pi/100*i, r[i]],i=0..100)], [seq([2*Pi/100*i, F[i]],i=0..100)]], style=point, legend=['r','F'] ); see graph note the minimum r and the maximum F are at the same point! (c) To find the minimum of a function such as r(theta) in Maple is difficult (it is easy in Matlab). From the graph the minimum is near theta=3.8. It is necessary to do some trial and error around this point to get a more accurate estimate. ------------------------------------------------------------------------------ Section B, question 4 --------------------- (a) Write a Matlab function that computes f(x,y)-g(x,y) for scalars x,y. file q4a.m with content: function z=q4a(x,y) z=1/(1+x^2+y^2) - x^2 - 2*y^2 - y*x + y; function to check if f>g for a given x,y: file q4b.m with content: function z=q4b(x,y) if q4a(x,y)>0 z=1; else z=0; end; (b) To do the double integral need a function that returns f-g if f>g and 0 otherwise, must work on vectors x and scalars y. file q4c.m with content: function z=q4c(x,y) z=zeros(size(x)); for i=1:size(x,1) for j=1:size(x,2) z(i,j)=q4a(x(i,j),y)*q4b(x(i,j),y); end end now do dblquad( @q4c, -1, 1.5, -1 ,1 ) (see graph to get correct limits) ------------------------------------------------------------------------------
Back to course homepage
Back to my main teaching page
Back to my main page