%% Solutions to Practice Set A, Algebra and Arithmetic

%% 1.
%% (a) 
1111 - 345  

%% (b)
format long; [exp(14), 382801*pi]  
%%
% The second number is bigger.

%% (c)
[2709/1024; 10583/4000; 2024/765]

%%
sqrt(7)  
%%
% The third, that is $2024/765$, is the best approximation.

%% 2.
%% (a)
cosh(0.1)  

%% (b)
log(2) 

%% (c)
atan(1/2) 

%%
format short

%% 3.
[x, y, z] = solve('3*x + 4*y + 5*z = 2', ...
    '2*x - 3*y + 7*z = -1', 'x - 6*y + z = 3', ...
    'x', 'y', 'z')  
%%
% Now we'll check the answer.

A = [3, 4, 5; 2, -3, 7; 1, -6, 1]; A*[x; y; z]  
%%
% It works!

%% 4.
[x, y, z] = solve('3*x - 9*y + 8*z = 2', ...
    '2*x - 3*y + 7*z = -1', 'x - 6*y + z = 3', ...
    'x', 'y', 'z')  
%%
% We get a one-parameter family of answers depending on the variable $z$.  In
% fact, the three planes determined by the three linear equations are not
% independent, because the first equation is the sum of the second and
% third.  The locus of points that satisfy the three equations is not a
% point, the intersection of three independent planes, but rather a line,
% the intersection of two distinct planes.  Once again we check:

B = [3, -9, 8; 2, -3, 7; 1, -6, 1]; B*[x; y; z]  

%% 5.
syms x y; factor(x^4 - y^4)  

%% 6.
%% (a)
simplify(1/(1 + 1/(1 + 1/x)))  

%% (b)
simplify(cos(x)^2 - sin(x)^2)  

%% 7.
3^301  

%%
pretty(sym('3^301'))

%% 8.
%% (a)
solve(67*x + 32 == 0)

%% (b)
vpa(ans, 15)  

%% (c)
syms p q
pretty(solve(x^3 + p*x + q == 0, x))

%% (d)
ezplot(exp(x)); hold on; ezplot(8*x - 4)
title('e^x and 8x - 4')
%%
fzero(@(x) exp(x) - 8*x + 4, 1)
%%
fzero(@(x) exp(x) - 8*x + 4, 3)

%% 9.
%% (a)
figure; ezplot(x^3 - x, [-4 4])  

%% (b)
figure; ezplot(sin(1/x^2), [-2 2])  

%%
figure; X = -2:0.1:2; plot(X, sin(1./X.^2))  

%%
% Both pictures are incomplete.  Let's see what happens if we refine the
% mesh.

X = -2:0.001:2; 
figure; plot(X, sin(1./X.^2))  
%%
% This is better, but because of the wild oscillation near $x = 0$,
% neither *|plot|* nor *|ezplot|* gives
% a totally accurate graph of the function.

%% (c)
figure; ezplot(tan(x/2), [-pi pi]); axis([-pi, pi, -10, 10]) 

%% (d) 
X = -1.5:0.01:1.5;
figure; plot(X, exp(-X.^2), X, X.^4 - X.^2)  

%% 10.
% Let's plot $2^x$ and $x^4$ and look for points of intersection.  We plot them
% first with *|ezplot|* just to get a feel for the graph.

figure; ezplot(x^4); hold on; ezplot(2^x); hold off  
title('x^4 and 2^x')

%%
% Note the large vertical range.  We learn from the plot that there are no
% points of intersection between $2$ and $6$ or between $-6$ and $-2$;
% but there are
% apparently two points of intersection between $-2$ and $2$.  Let's change to
% *|plot|* now and focus on the interval between $-2$ and $2$.  We'll
% plot $x^4$ dashed.

X = -2:0.1:2; plot(X, 2.^X); 
hold on; plot(X, X.^4, '--'); hold off   
%%
% We see that there are points of intersection near $-0.9$ and $1.2$.  Are
% there any other points of intersection?  To the left of $0$, $2^x$ is always
% less than $1$, whereas $x^4$ goes to infinity as $x\to -\infty$.
% However, both $x^4$ and $2^x$ both go to infinity as $x\to \infty$,
% so the graphs may cross again to the right of $6$.  Let's check:

X = 6:0.1:20; plot(X, 2.^X); 
hold on; plot(X, X.^4, '--'); hold off   
%%
% We see that they do cross again, near $x = 16$.  If you know a little
% calculus, you can show that the graphs never cross again (by taking
% logarithms, for example), so we have found all the points of
% intersection.  Now let's use *|fzero|* to find these points of
% intersection numerically.  This command looks for a solution near a given
% starting point.  To find the three different points of intersection, we
% will have to use three different starting points. The above graphical
% analysis suggests appropriate starting points.

r1 = fzero(@(x) 2^x - x^4, -0.9)
r2 = fzero(@(x) 2^x - x^4, 1.2)
r3 = fzero(@(x) 2^x - x^4, 16)
%%
% Let's check that these ``solutions'' satisfy the equation.

double(subs(2^x - x^4, x, r1)) 
double(subs(2^x - x^4, x, r2))
subs(2^x - x^4, x, r3) 
%%
% So *|r1|* and *|r2|* very nearly satisfy the equation, and *|r3|*
% satisfies it exactly.  It is easily seen that $16$ is a solution.  It 
% is also interesting to try *|solve|* on this equation.

syms x real; symroots = solve(2^x - x^4 == 0)  

%%
% The function *|lambertw|* is a ``special function'' that is built into
% MATLAB.  You can learn more about it by typing *|help lambertw|*.  
% The command *|solve|* finds all of the roots, as you can see by looking
% at the decimal values.

double(symroots)


