1. Produce a plot of //sin(x)// versus //x// in blue and //cos(x)// versus //x// in
red for 100 evenly spaced points between 0 and 2pi. Label the axes.
x = linspace(0,2*pi, 100);
plot(x, sin(x), 'b', x, cos(x), 'r');
xlabel('x')
legend('sin x', 'cos x') % makes more sense than a ylabel, but you could do that, too
2. Given matrix A, set //v// to the //j//th column of //A//.
v = A(:,j);
3. Given matrix A, set //v// to the //i//th row of //A//.
v = A(i,:);
4. Suppose matrix //A// has //M// rows and //N// cols. Set //B// to //A// with its columns in reversed order.
B = A(:,N:-1:1);
5. Solve the system of equations
2y - x = 1
3y + 2z = 5
-3x + z = -2
A = [-1 2 0 ; 0 3 2 ; -3 0 1];
b = [1 ; 5 ; -2];
x = A\b;
6. Produce a vector of 100 random floating-point numbers between 0 and 10.
rand(100,1)
7. Produce a vector of 100 random integers between 0 and 10.
randi(11, 100, 1) - 1
8. Produce a random permutation of the integers between 1 and 10.
randperm(10)
9. Produce a random permutation of the vector v = [3 3 4 5 7];
v = [3 3 4 5 7]; % no problem if you assume v is already set
v(randperm(5))
10. Produce all permutations of the vector v = [3 3 4 5 7];
perms(v)
11. Produce a quiver plot of the vector field $v = [v_x, v_y]$ where $v_x = \sin(x) \cos(y)$,
$v_y = x y$, and //x// and //y// range from -pi to pi. Label the axes.
x = linspace(-pi, pi, 30);
y = linspace(-pi, pi, 30);
[X, Y] = meshgrid(x,y);
Vx = sin(X) .* cos(Y);
Vy = X .* Y;
quiver(X, Y, Vx, Vy);
xlabel('x');
ylabel('y');
12. Produce a contour plot of $f(x,y) = \sin(x^2+y^2)/\sqrt{x^2 + y^2}$
where //x// and //y// range from -10 to 10. Label the axes.
x = linspace(-10, 10, 50);
y = linspace(-10, 10, 50);
[X, Y] = meshgrid(x,y);
R2 = X.^2 + Y.^2;
F = sin(R2) ./ sqrt(R2);
contour(X, Y, F);
xlabel('x');
ylabel('y');
(Comment: I meant to set $f(x,y) = \sin(\sqrt{x^2+y^2})/\sqrt{x^2 + y^2}$,
which makes a prettier plot.)
13. Produce a 3d surface plot of the function from problem 12 over the same range, and with a color bar.
Label the axes.
surf(X, Y, F);
colorbar;
xlabel('x');
ylabel('y');
14. Write down the connectivity matrix //C// for the links in this small
network of websites.
{{:gibson:teaching:fall-2012:math445:network2.png?direct&300}}
% C((i,j) = 1 if i -> j, 0 otherwise
C = [ 0 1 0 0 0 0 ;
0 0 0 0 1 1 ;
1 1 0 0 0 0 ;
0 0 1 0 0 1 ;
1 0 0 1 0 0 ;
0 0 1 0 0 0 ];
15. Write Matlab code that converts the connectivity matrix //C// to a
transition matrix //T// that governs the transition of probabilities
under random surfing.
T = C;
for j = 1:6;
T(:,j) = T(:,j)/sum(T(:,j));
end
16. Given //T//, write Matlab code that computes the vector $x$ of
probabilities $x_j$ that you'll end up at website $j$ after a
long night of random websurfing.
x = [1 0 0 0 0 0]; % start with probability 1 at page 1, 0 elsewhere
N = 10000; % or another large number
x = T^N * x; % x(j) = probability of landing at page j after N random clicks
17. Write an equation for //y// as a function of //x// for
the following data plot. Bonus: express exponential functions
as powers of //e// rather than powers of 10. Use $e^{2.3}\approx 10$
to convert between the two.
{{:gibson:teaching:fall-2012:math445:fig1.png?direct&300}}
We have a straight line with logarithmic $Y$ axis and a linear $x$ axis,
so the equation will be of the form $\log_{10} y = m x + b$ or
$y = 10^{mx + b} = c 10^{mx}$. Determine $m$ from rise over run from
$x=2$ to $x=10$.
m = (\Delta \log_{10} y)/(\Delta x) \approx (-2 - 0)/(10 - 2) = -1/4
Determine $c$ from value of $y$ at $x=0$. That gives $c \approx 4$. So answer is
y \approx 4 \cdot 10^{-x/4}
Bonus: express exponential functions as powers of $e$ rather than powers of 10.
Use $e^2.3 \approx 10$ to convert between the two.
10^{-x/4} \approx e^{2.3 \cdot (-x/4)} \approx e^{-0.58 x}
so
y \approx 4 \cdot e^{-0.58 x}
18. How would you graph the function $y(x) = x^c$, in a way that highlights
this functional relationship? I.e. given vectors $x$ and $y$ satisfying
$y_i = x_i^c$, what Matlab command should you use to plot $y$ versus $x$?
loglog(x,y)
19. How would you graph the function $y(x) = c^x$, in a way that highlights
this functional relationship?
semilogy(x,y)
20. Write an anonymous function that returns the square of its input.
f = @(x) x^2;
21. Write an anonymous function that, for an input vector $x = [x_1, ~x_2]$
returns the output vector $f(x) = [4 x_1 x_2, ~\sin(x_1) \cos(x_2)]$
f = @(x) [4*x(1)*x(2), sin(x(1))* cos(x(2))];
22. Convert the following 2nd order ODE to a 1st order system of ODE in
two variables.
$dx^2/dt^2 + 3\; dx/dt + \sin(x) = 0$
Let $v = [v_1, v_2] = [x, dx/dt]$. Then
dv_1/dt = dx/dt = v_2
dv_2/dt = d^2x/dt^2 = -3 dx/dt - \sin x = -3 v_2 - \sin v_1
So $dv/dt = [dv_1/dt, dv_2/dt] = [v_2, -3 v_2 - \sin v_1]$
22. Show how to integrate the system of ODEs from problem 22 from
t = 0 to 10 using the initial condition x(0) = 0, dx/dt(0) = 1.
Write the ODE system in Matlab using an anonymous function.
f = @(t,v) [v(2) ; -3*v(2) - sin(v(1))];
[t,v] = ode45(f, 0:0.1:10, [0 ; 1]);
23. Compute the terminal velocity of a ping-pong ball dropped from
a great height, using this system of equations
dy/dt = v_y
d v_y/dt = -g - \mu v_y |v_y|
where $g = 9.81, ~\mu =0.35$, $y$ represents the vertical position, and
$v_y$ represents the vertical velocity. Represent the two free variables
with the vector $x = [y, ~v_y]$ and reexpress the two equations above as
an ODE system of the form $dx/dt = f(x)$ (both sides of that equation are vectors).
dx_1/dt = dy/dt = v_y = x_2
dx_2/dt = dv_y/dt = -g - \mu v_y |v_y| = -g - \mu x_2 |x_2|
So $dx/dt = [x_2, ~-g - \mu x_2 |x_2|]$
Write an anonymous function in Matlab that computes $dx/dt = f(x)$ for an input vector $x$,
and then use //ode45// to integrate this system from $t=0$ to $t=100$
from the initial conditions $x(0) = [y(0), ~v_y(0)] = [0, 0]$.
g = 9.81;
mu = 0.35;
f = @(x) [x(2) ; -g - mu*x(2)*abs(x(2)];
[t, X] = ode45(f, 0:100, [0 ; 0]);
24. Plot the position of the ping-pong ball as a function of time.
Label the axes.
plot(t, X(:,1)) % first column of returned X is 1st component of x as a function of time
xlabel('t')
ylabel('y = height of ball')
25. Write a function that performs matrix-vector multiplication for
a **sparse** matrix //A//, that accesses only nonzero elements of //A//.
For an // M x N // matrix //A// and an //N//-dimensional vector //x//,
the matrix-vector product $y = Ax$ is defined by
y_i = \sum_{j=1}^N A_{ij} x_j
for each component $y_i$ of the //M// dimensional vector $y$. But don't code that
formula directly! Instead start your function with
K = nnz(A);
[i,j,a] = find(A);
and write the matrix-vector multiplication as a loop over the K nonzero elements
of A.
function y = sparse_matvec_mult(A,x)
K = nnz(A);
[i,j,a] = find(A);
[M,N] = size(A);
y = zeros(M,1); % set y to M-dim col vector
for k=1:K;
y(i(k)) = y(i(k)) + a(k)*x(j(k));
end
end