==============================================================================
SECTION A, QUESTION 1
=====================
A = [1 -7 2 ; 2 0 4 ; -5 3 1] ;
b = [-3 ; 1 ; 6 ] ;
x = A\b
There are many ways to check the solution is unique.
For example type det(A) to see if the determinant is nonzero.
==============================================================================
SECTION A, QUESTION 2
=====================
Here is a good way to do it. Each term in the series is x/(a+n)/(b+n)
times the previous term:
f:=proc(a,b,x)
local n,s,t;
s:=0;
t:=1/a/b;
for n from 1 to 100 do
t:=t*x/(a+n)/(b+n)
s:=s+t;
end do;
end proc;
This is doing it exactly, not numerically. For numerically I should
put "evalf" somewhere (and there are other subtleties too, that are
not for this course).
Here is a less good way to do it, computing each term separately:
f:=proc(a,b,x)
local i,n,s,den;
s:=0;
for n from 1 to 100 do
den:=a*b;
for i from 1 to n do
den:=den*(a+i)*(b+i);
end do
s:=s+x^n/t;
end do;
end proc;
==============================================================================
SECTION A, QUESTION 3
=====================
Define a function
function a = q3(x)
a = (x(1)+2*x(2)+3*x(3))/(1+x(1)^2+x(2)^4+x(3)^6);
Then type
opts = optimset( 'xtol', 1e-8 )
format long
fminsearch( @q3, [0,0,0], opts )
==============================================================================
SECTION A, QUESTION 4
=====================
Define the 3 functions:
f1:=1+3*x-x^2; the "down" parabola
f2:=(x^2-4*x+2)/5; the "up" parabola
f3:=(x-1)/5; the line
Each function intersects the other in two points. We need to find
these points and select the right one:
solve( f1=f2, x);
p1:= (take the smaller x where the 2 parabolas meet)
solve( f2=f3, x);
p2:= (take the smaller x where the line meets the "up" parabola)
solve( f1=f3, x);
p3:= (take the larger x where the line meets the "down" parabola)
The area we want is
int( f1-f2, x=p1..p2) + int( f1-f3, x=p2..p3) ;
(The answer, after simplifying, is
-(13/60)*sqrt(13)+(433/2160)*sqrt(433)+(158/375)*sqrt(79)-1771/54000
which is about 7.1)
==============================================================================
SECTION A, QUESTION 5
=====================
Here is a solution using logic similar to question 4.
Define functions g1=f1-f2, g2=f2-f3, g3=f3-f1 .
I will do it inline, you could use m-files
g1=inline('(1+3*x-x.^2)-(x.^2-4*x+2)/5')
g2=inline('(x.^2-4*x+2)/5-(x-1)/5')
g3=inline('(x-1)/5-(1+3*x-x.^2)')
p1=fzero( g1, 0 )
p2=fzero( g2, 1 )
p3=fzero( g3, 3 )
quad( g1, p1, p2) - quad( g3, p2, p3)
Here is another much better solution:
Define the following single function
function y=q5(x)
y=zeros(size(x));
for i=1:size(x,1)
for j=1:size(x,2)
f1 = 1+3*x(i,j)-x(i,j)^2 ;
f2 = (x(i,j)^2-4*x(i,j)+2)/5 ;
f3 = (x(i,j)-1)/5) ;
if f1>max(f2,f3)
y(i,j)=f1-max(f2,f3);
end
end
end
and then do quad(@q5, -0.5, 3.5)
==============================================================================
SECTION A, QUESTION 6
=====================
with(LinearAlgebra);
A:=<<t,2,1>|<2,3,-1>|<1,-1,5>>;
t1:= solve( subs(x=3,CharacteristicPolynomial(A,x))=0 , t );
Eigenvalues(subs(t=t1,A));
gives t1=-9, other eigenvalues are -2+sqrt(55), -2-sqrt(55)
==============================================================================
SECTION B, QUESTION 1
=====================
Basically we need to find the integral of g-f over regions where it is
positive. So build a function that gives g-f when this is positive and
zero otherwise.
function z=q1a(x,y)
% g-f if this is positive, otherwise zero
% must work for x a vector and y a scalar
f = 6./(1+x.^2+y^2);
g1 = 1./(1/6+x.^2+(y-2)^2);
g2 = 1./(1/6+(x+1).^2+(y-sqrt(3)/2)^2);
g3 = 1./(1/6+(x-1).^2+(y-sqrt(3)/2)^2);
z = g1+g2+g3-f ;
for i=1:size(z,1);
for j=1:size(z,2);
if z(i,j)<0
z(i,j)=0
end
end
end
Now the question can be solved by doing three double integrals over
appropriate regions
dblquad( @q1a, 0,3,-2,1 ) % closest shpitz, x between 0 and 3, y between -2 and 1
dblquad( @q1a, -3,0,-2,1 ) % shpitz on left, x between -3 and 0, y between -2 and 1
dblquad( @q1a, -2,2,1,4 ) % shpitz at the back, x between -2 and 2, y between 1 and 4
These regions are all bigger than necessary. It is possible to find smaller
rectangles to integrate over. There are many ways to do this - for example
write a function that takes a rectangle and checks whether f>g (using q1a)
at all points of the boundary of the rectangle.
==============================================================================
SECTION B, QUESTION 2
=====================
I have written everything as functions, this is not necessary
a)
function [Ppt,Qpt,d]=q2a(P,Q)
% finds the maximum distance between points in P and Q
% returns the point in P, the point in Q and the distance
d=sqrt((P(1,1)-Q(1,1))^2+(P(1,2)-Q(1,2))^2); the distance
Ppt=P(1,:); % the point in P
Qpt=Q(1,:); % the point in Q
for i=1:n
for j=1:n
d2=sqrt((P(i,1)-Q(j,1))^2+(P(i,2)-Q(j,2))^2);
if d2>d
d=d2;
Ppt=P(i,:);
Qpt=Q(j,:);
end
end
end
b) same but with "d2<d" as opposed to "d2>d"
function [Ppt,Qpt,d]=q2b(P,Q)
% finds the minimum distance between points in P and Q
% returns the point in P, the point in Q and the distance
d=sqrt((P(1,1)-Q(1,1))^2+(P(1,2)-Q(1,2))^2); the distance
Ppt=P(1,:); % the point in P
Qpt=Q(1,:); % the point in Q
for i=1:n
for j=1:n
d2=sqrt((P(i,1)-Q(j,1))^2+(P(i,2)-Q(j,2))^2);
if d2<d
d=d2;
Ppt=P(i,:);
Qpt=Q(j,:);
end
end
end
c)
function q2c(P,Q)
[P1,Q1]=q2a(P,Q); % find the points with the maximum distance
[P2,Q2]=q2b(P,Q); % find the points with the minimum distance
hold on
plot( P(:,1) , P(:,2), 'b.' ) % plot points in P in blue
plot( Q(:,1) , Q(:,2), 'r.' ) % plot points in Q in red
plot( [P1(1),Q1(1)] , [P1(2),Q1(2)], 'g' ) % green line joining P1 and Q1
plot( [P2(1),Q2(1)] , [P2(2),Q2(2)], 'k' ) % black line joining P2 and Q2
==============================================================================
SECTION B, QUESTION 3
=====================
a)
(1) q1,q2 are defined to be the appropriate quadratic expressions
(2) fsolve is used to find a solution of q1=q2=0. however, it just finds
one expression
(1) the combination 6q1-2q2 does not have an x-squared term in, i.e. it
is linear in x and we can find x as a function of y
(2) we write x as the appropriate function of y in q1, and use fsolve
to find all the values of y for which this vanishes.
(3) for each value of y, find the appropriate value of x
b)
f:=proc(A1,B1,C1,D1,E1,F1,A2,B2,C2,D2,E2,F2)
local q1,q2,x,y,X,Ys,i;
q1:=A1*x^2+2*B1*x*y+C1*y^2+D1*x+E1*y+F1;
q2:=A2*x^2+2*B2*x*y+C2*y^2+D2*x+E2*y+F2;
X:=solve( A2*q1-A1*q2, x );
Ys:=[fsolve(numer(simplify(subs(x=X,q1)))=0,y)];
for i from 1 to nops(Ys) do
print( [subs(y=Ys[i],X), Ys[i] ] );
end do;
end proc;
c) do with(plots) before starting
we need to find the right range to draw. start by finding the largest and
smallest x and y of an intersection point. then add a bit. then draw.
the following should be added to the procedure:
local q1,q2,x,y,X,Ys,i,xmin,xmax,ymin,ymax,p1,p2; (instead of what was before)
xmin:=subs(y=Ys[1],X);
xmax:=subs(y=Ys[1],X);
ymin:=Ys[1];
ymax:=Ys[1];
for i from 2 to nops(Ys) do
if subs(y=Ys[i],X)<xmin then
xmin:=subs(y=Ys[i],X);
end if;
if subs(y=Ys[i],X)>xmax then
xmax:=subs(y=Ys[i],X);
end if;
if Ys[i]<ymin then
ymin:=Ys[i];
end if;
if Ys[i]>ymax then
ymax:=Ys[i];
end if;
end do;
xmin:=xmin-1;
xmax:=xmax+1;
ymin:=ymin-1;
ymax:=ymax+1;
p1:=implicitplot(q1=0,x=xmin..xmax,y=ymin..ymax);
p2:=implicitplot(q2=0,x=xmin..xmax,y=ymin..ymax);
display(p1,p2);
It is rather weak, it will work sometimes, but often it won't
==============================================================================
Back to course homepage
Back to my teaching homepage
Back to my homepage