Exam solutions, Moed Aleph, 5768 (Tuesday group)

These are only notes, not full answers.
All the questions can be answered in many ways! Especially the ones in part B.

==============================================================================

SECTION A, QUESTION 1  
=====================

%% auxiliary function to find f(x,y,p) (note we make (x,y) into a vector x):

f1.m
----
function z=f1(x,p)
  z=-sin(x(1)+2*x(2)+p)^2/(1+x(1)^2+2*x(2)^2); 

%% function to fund the minimum: 

f2.m
----
function z=f2(p) 
  z=fminsearch(@f1,[0;0],[],p) 

%% commands to plot the graph, taking p from 0 to 10: 

f3.m
----
p=[0:0.1:10]
x=zeros(size(p));
y=zeros(size(p));
for i=1:size(p,2)
   z=f2(p(i));
   x(i)=z(1);
   y(i)=z(2);
end 
plot3(p,x,y)

==============================================================================

SECTION A, QUESTION 2
=====================

a:=(x-2)/3;
b:=exp(-x)*sin(x)/(1+x^2);

A1:=int( a-b, x=fsolve(a=b,x=-2)..fsolve(a=b,x=-1) );
A2:=int( b-a, x=fsolve(a=b,x=2)..fsolve(a=b,x=-1) );

==============================================================================

SECTION A, QUESTION 3
=====================

% range of x is -1 to 3
% range of y is  1 to 5 

dblquad(ff,-1,3,1,5)

% here ff defined as follows: 

ff.m
----
function z=ff(x,y)

z=zeros(size(x));
for i=1:size(z,1)
   for j=1:size(z,2) 
       if (x(i,j)-1)^2+(y-3)^2<4 & y>x(i,j) 
          z(i,j)=cos((x(i,j)-2)/(y+2) - (y-2)/(x(i,j)+2)); 
       end 
   end
end 
  
==============================================================================

SECTION A, QUESTION 4
=====================

with(LinearAlgebra);
M:=<<1,2,4>|<-1,4,4>|<-2,3,0>>;
Eigenvalues(M);
r1:=RowSpace(M-2*IdentityMatrix(3));
r2:=RowSpace(M-4*IdentityMatrix(3));
tem:=a1*r1[1]+a2*r1[2]-a3*r2[1]-a4*r2[2];
solve( {tem[1]=0,tem[2]=0,tem[3]=0} , {a1,a2,a3} );
%% suppose get answer a3=xx (it will be a function of a4) 
int:=xx*r2[1]+a4*r2[2]; 
%% this is the intersection, it will be of the form a4*some_vector 

% you would have thought you could be more clever than this....

==============================================================================

SECTION A, QUESTION 5
=====================

f:=x^2/4+b*sin(x)/(4+x);
df:=diff(f,x);
solve( subs(x=Pi,df)-subs(x=-Pi,df)=0, b);
%% suppose get answer b=xx
f1:=subs(b=xx, f);
g:=piecewise( x>-3*Pi and x<-Pi, subs(x=x+2*Pi,f), x>-Pi and x<Pi, subs(x=x+2*Pi,f), 
              x>Pi and x<3*Pi, subs(x=x-2*Pi,f) );
plot(g,x=-3*Pi..3*Pi);

==============================================================================

SECTION B, QUESTION 1
=====================

a) function z=checksit(A)

d=sqrt((A(1,1)-A(2,1))^2+(A(1,2)-A(2,2))^2);
if d+A(2,3)<A(1,3) 
   z=1;
elseif d+A(1,3)<A(2,3) 
   z=2;
elseif d==abs(A(2,3)-A(1,3)) || d==A(2,3)+A(1,3)
   z=3;
elseif d>abs(A(2,3)-A(1,3)) & d<A(2,3)+A(1,3)
   z=4;
else 
   z=5;
end

%%%% 

b) function B=findcircs(A)

n=size(A,1);     % number of circles
m=zeros(n,1);    % row i of  m  counts how many circles intersect circle i 

% go through all pairs of circles and count intersections

for i=1:(n-1)
  for j=(i+1):n
    if checksit([A(i,:);A(j,:)])==4
       m(i)=m(i)+1;
       m(j)=m(j)+1;
    end
  end
end

% build the answer

M=max(m)
B=[];
for i=1:n
  if m(i)==M
     B=[B;A(i,:)];
  end
end

==============================================================================

SECTION B, QUESTION 2
=====================

a) two options:

findbest:=proc(x,n)
   local q;
   q:=evalf(x*n);
   if frac(q)<1/2 then return(floor(q)/n) else return(floor(q+1)/n) end if;
end proc;

findbest:=proc(x,n)
    local q;
    q:=round(x*n);
    return(q/n);
end proc;

b) 

findapps:=proc(x,D)
    local q,app,n,ans;
    app:=findbest(x,1);  
    ans:=app; 
    for n from 2 to D do 
          q:=findbest(x,n); 
          if abs(evalf(x-q))<abs(evalf(x-app)) then app:=q; ans:=ans,q; end if;
    end do:   
    return(ans);
end proc;

c) It is a problem with Digits !!

==============================================================================

SECTION B, QUESTION 3
=====================

a) 
auxiliary function in file f.m

function z=f(x,p)
  z=x^2-5-p*sin(x); 

to get the graph:

p1=[0:0.1:10];       % p's starting from zero going up
a1=zeros(size(p1));  % the upper solution for p's in p1
b1=zeros(size(p1));  % the lower solution for p's in p1

p2=[0:(-0.1):(-10)];       % p's starting from zero going down
a2=zeros(size(p2));        % the upper solution for p's in p2
b2=zeros(size(p2));        % the lower solution for p's in p2

a1(1)=sqrt(5);
b1(1)=-sqrt(5);
a2(1)=sqrt(5);
b2(1)=-sqrt(5);

% get the solutions, starting from p=0 and going up:  

for i=2:size(p1,2)
    a1(i)=fzero(@f,a1(i-1),[],p1(i));
    b1(i)=fzero(@f,b1(i-1),[],p1(i));
end

% get the solutions, starting from p=0 and going down:  

for i=2:size(p2,2)
    a2(i)=fzero(@f,a2(i-1),[],p2(i));
    b2(i)=fzero(@f,b2(i-1),[],p2(i));
end

% make the graph 

hold on
plot(p1,a1)
plot(p1,b1)
plot(p2,a2)
plot(p2,b2) 

b) This is not so easy. As p increases there are more and more x's. But
since |sin(x)|<1 we have x^2 < 5+|p| giving |x| < sqrt(5+|p|). For p=100
this gives  |x|<10.25. The x's in the picture for p=100 are near -2\pi,-\pi,
0, \pi, 2\pi, 3\pi. This makes sense as to get a solution with p big 
we will need sin(x) small (i.e. near a multiple of pi). 

The following program does _quite_ well. It exploits the behavior of 
fsolve in Maple - if fsolve is asked to find a root in a specific range,
it will not seek out of this range. But if it is asked to find a root
near a specific point, it interprets "near" as flexible. But as a result, 
the program often reports pairs of roots as single roots. So - for example
- it fails from p=15 to p=34, giving only 3 roots not 4 in this range. 
There are various ways it can be fixed up, maybe I will write down one 
sometime - the hallmark of a bad answer is that the number of roots comes
out odd, not even. 

findxs:=proc(p)

local x,n,i; 

x:=fsolve(x^2=5+p*sin(x),x=0..Pi),fsolve(x^2=5+p*sin(x),x=-Pi..0);
n:=floor(sqrt(5+abs(p))/Pi);
for i from -n to n do 
   x:=x,fsolve(x^2=5+p*sin(x),x=i*Pi) 
end do;
return({x});

end proc; 

==============================================================================


Back to course homepage
Back to my teaching homepage
Back to my homepage