# channelflow.org

### Site Tools

gibson:teaching:fall-2012:math445:pf1-solns

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 jth column of A.

 v = A(:,j);

3. Given matrix A, set v to the ith 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 where , , 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 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 , 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.

% 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 of probabilities that you'll end up at website 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 to convert between the two.

We have a straight line with logarithmic axis and a linear axis, so the equation will be of the form or . Determine from rise over run from to . Determine from value of at . That gives . So answer is Bonus: express exponential functions as powers of rather than powers of 10. Use to convert between the two. so 18. How would you graph the function , in a way that highlights this functional relationship? I.e. given vectors and satisfying , what Matlab command should you use to plot versus ?

loglog(x,y)

19. How would you graph the function , 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 returns the output vector 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. Let . Then  So 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  where , represents the vertical position, and represents the vertical velocity. Represent the two free variables with the vector and reexpress the two equations above as an ODE system of the form (both sides of that equation are vectors).  So Write an anonymous function in Matlab that computes for an input vector , and then use ode45 to integrate this system from to from the initial conditions .

  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 is defined by for each component of the M dimensional vector . 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 