**Problem 1:** Write Matlab code for plotting $y = 1.2 e^{-0.8x}$ as a red line,
for 100 evenly spaced points in $x$ between $-10$ and $10$. Choose a plotting
function that highlights the functional relationship between $y$ and $x$. Label
the axes and title the graph.
x = linspace(-10,10,100);
y = 1.2*exp(-0.8*x);
semilogy(x,y,'r-');
xlabel('x')
ylabel('y')
title('graph of y = 1.2 exp(-0.8x)'); % one choice of title
**Problem 2:** Write an equation for $y$ as a function of $x$ for the
following data plot.
We see a straight line on a plot where both axes are logarithmic. That means
a power-law relationship, i.e. the form $y = c x^b$. Increasing $x$ by a
factor of $10^2$ causes an increase in $y$ by a factor of $10^6$. Therefore
$b=3$. At $x=1$, $y=10^{-2}$, so $c=10^{-2}$. Therefore the functional
relationship is $y=10^{-2} x^3$.
**Problem 3:** Write Matlab code that produces a contour plot of
$(1-y^2) \sin x$ over the rectangle $-1 \leq y \leq 1$ and
$-\pi \leq x \leq \pi$, with 30 evenly spaced points in each of $x$ and $y$.
x = linspace(-pi,pi,30);
y = linspace(-1,1,30);
[X,Y] = meshgrid(x,y);
Z = (1-Y.^2).*sin(X);
contour(x,y,Z);
xlabel('x')
ylabel('y')
title('contours of (1-y^2) sin(x)')
**Problem 4:** Write Matlab code that produces the solution of the
following system of equations
\begin{align*}
w - x + z &= 2 \\
2x + 3y - 4z &= -5 \\
4w - 3x + 2y + z &= 3 \\
-w + 3x + 6y &= 11
\end{align*}
A = [1 -1 0 1; 0 2 3 4; 4 -3 2 1; -1 3 6 0];
b = [2; -5; 3; 11];
x = A\b
**Problem 5:** Write one line of Matlab code that would verify that
the solution computed in the previous problem satisfies the given equations.
Hint: you can answer this in as little as five characters, or perhaps even
three.
Ax-b
**Problem 6:** Write a Matlab function that returns a numerical approximation
to $e$ using the following series, where $N$ is an input argument to the
function.
e \approx \sum_{n=0}^{N} \frac{1}{n!}
function e = eulernumber(N)
% return approximation of e from sum_{n=0}^N 1/n!
% it's easiest to take care of n=0 term before loop
nfactorial = 1;
e = 1;
for n=1:N
nfactorial = n*nfactorial;
e = e + 1/nfactorial;
end
end
**Problem 7:** The Lorenz system is a ordinary differential equation in
three variables $[x, y, z]$ defined by
\begin{align*}
\frac{dx}{dt} = s (y - x), \quad \frac{dy}{dt} = x (r -z) - y, \quad \frac{dz}{dt} = xy - bz
\end{align*}
Write Matlab code for an anonymous function that returns the vector
$dv/dt = [dx/dt, ~dy/dy, ~dz/dt]$ given an input vector $v = [x, y, z]$,
using parameter values $s=10$, $b=8/3$, and $r=28$.
f = @(v) [10*(v(2)-v(1)); v(1)*(28-v(3)) - v(2); v(1)*v(2) - 8*v(3)/3];
**Problem 8:** Revise your answer to Problem 7 so that the anonymous function
is in the right form to be passed into Matlab's {\tt ode45} function, and write
Matlab code that would integrate the Lorenz system from $t=0$ to $t=100$, from
the initial condition $x=0$, $y=1$, $z=0$.
f = @(t, v) [10*(v(2)-v(1)); v(1)*(28-v(3)) - v(2); v(1)*v(2) - 8*v(3)/3];
[t,v] = ode45(f, [0 100], [0; 1; 0]);
**Problem 9:** Suppose you have a biased coin that has a 53.8\% chance
of landing ``heads'' on any given toss, and you want to figure out how
likely it is that, out of ten coin tosses, you'll get exactly $n$ heads,
for each value of $n$ between 0 and 10. Write a Matlab function that
would estimate the probability getting $n$ heads in ten tosses.
function p = biasedcoin(n);
% estimate prob of getting n heads in 10 tosses of a 53.8\% head biased coin
Ntrials = 1e04;
count = 0;
for n=1:Ntrials
flips = rand(10,1); % simulate ten coin flips
if sum(flips < 0.538) == n % test if there are n heads in ten flips
count = count + 1;
end
end
p = count/Ntrials;
**Problem 10:** Write Matlab code that would set $C$ to a matrix
representing the connectivity of the following network of links.
C = zeros(4,4);
C(3,1) = 1;
C(4,1) = 1;
C(1,2) = 1;
C(4,2) = 1;
C(1,3) = 1;
C(4,3) = 1;
C(2,4) = 1;