For each of the following, provide an answer in Matlab-digestable syntax
1. Assign the value 0.00014 to the variable x, using compact scientific notation.
x = 1.4e-04
2. Set the variable x to a vector whose components are the even numbers between 14 and 36.
x = 14:36
3. Set the variable x to 200 evenly spaced points between 0 and 2.
x = linspace(0, 2, 200)
4. Produce a plot of y = x^2 - 2x + 3 for the x of the previous problem. Label the axes.
plot(x, x.^2 - 2x + 3); xlabel('x'); ylabel('x^2 - 2x + 3');
Note: We have to use x.^2
rather than x^2
to get the square of each component of the vector x
.
5. Plot tan(x) versus x for 200 evenly spaced points between 0 and 1.57 ≈ pi/2, using a logarithmic scale on the y axis.
x = linspace(0, 1.57, 200); semilogy(x, tan(x)); xlabel('x'); ylabel('tan(x)');
6. Plot sin(x) and cos(x) for 200 evenly spaced points between -π and π, on the same plot, using red for sin(x) and blue for cos(x). Add a legend that indicates which function is which color.
x = linspace(-pi, pi, 200); plot(x, sin(x), 'r', x, cos(x), 'b'); legend('sin(x)', 'cos(x)'); xlabel('x')
7. Produce a vector of length 13 whose components are random integers between 0 and 10, inclusive.
randi(10, 1, 13)
8. Produce a vector of length 13 whose components are random real numbers between 0 and 10, inclusive.
10*rand(1, 13)
9. Show how you would solve the following system of equations with Matlab
x + 2y - z = 5 3x + y + 6z = 37 -3x + y + 2z = -11
A = [1 2 -1; 3 1 6; -3 1 2]; b = [5; 37; -11]; x = A\b
10. Write a conditional expression that is true if scalar variables x and y are both nonzero and false otherwise.
x ~= 0 && y ~= 0
11. Set variable A to a 3 x 5 matrix of zeros.
A = zeros(3,5)
12. Set variable A to a 4 x 7 matrix of random real numbers, using a guassian (normal) distribution.
A = randn(4,7)
13. Write a conditional expression that is true if a matrix A is square and false otherwise.
dim(A,1) == dim(A,2) % a one-liner, but maybe you don't know dim works this way
or
[M, N] = size(A) M == N
14. Write a conditional expression that is true if either x or y is an integer.
x == round(x) || y == round(y)
Write a Matlab function that
15. returns 1 (true) if its argument is divisible by 3 and 0 (false) if it's not.
function r = divisiblebythree(n) % divisiblebythree(n) : return 1 if n is divisible by 3 or 0 if it's not r = 3*round(n/3) == n end
16. takes a vector x as input and returns 1 if the components of x are sorted in ascending order, 0 if not.
function s = sorttest(x) % return 1 if x is sorted in ascending order, 0 if not N = length(x); % count how many of {x(1) through x(N-1)} are less than {x(2) through x(N)} % the count will be N-1 if the x is sorted in ascending order s = sum(x(1:N-1) < x(2:N)) == N-1 end
17. computes the product y = Ax of an M x N matrix A and an N x 1 vector x, according the formula
function y = matvecmult(A,x); % matvecmult: compute matrix-vector multiplication A x [M,N] = size(A); % M x N matrix times N-vector equals an M-vector y = zeros(M,1); % evaluate formula for ith component of y for i=1:M % evaluate y_i = sum_{j=1}^n A_ij x_j for j=1:N y(i) = y(i) + A(i,j)*x(j); end end end
A few more problems, added 1/14/2013
18. Set vector v to the 3rd column of matrix A.
v = A(:,3);
19. Set vector v to the 4th row of matrix A.
v = A(4,:);
20. Set vector v to the first 3 entries in the 7th row of A.
v = A(7,1:3);
21. Set the 4th row of the 8 x 5 matrix A to (1, 2, 9, 5, 0).
A(4,:) = [1 2 9 5 0];
22. Set the 2nd column of the 4 x 3 matrix A to a column vector with entries (3, 0, -1, 5).
A(:,2) = [3; 0; -1; 5];
or
A(:,2) = [3 0 -1 5]';
23. Zero all the entries in the last column of a matrix A.
[M,N] = size(A); A(:,N) = zeros(M,1);
24. Produce a contour plot of where x and y range from -10 to 10. Label the axes.
N = length(x); F = zeros(N,N); for i=1:N; for j=1:N; r = x(i)^2 + y(j)^2; F(i,j) = sin(r)/sqrt(r); end end % plot matrix with contour contour(x,y,F); xlabel('x'); ylabel('y'); % problem didn't ask for these, but they make the plot nicer title('sin(x^2 + y^2)/sqrt(x^2 + y^2)') axis equal axis tight
—-
A few more problems, added late 10/14/2013. You don't have to turn these in, but they are fair game for the exam. I will add more example problems over the next few days.
25. Write an expression that returns the positive elements of a vector v.
v(v>0)
26. Write an expression that returns the sum of the positive elements of a vector v.
sum(v(v>0))
27. Write an expression that returns the number of positive elements in a vector v.
sum(v>0)
Even more practice problems! Hooray! added 10/22/3013
28. Write a function that computes the factorial of an integer n using a while
loop.
function f = factorial2(n) % factorial2: compute n! f = 1; while (n>1); f = f*n; n = n-1; end end
29. Write an isPrime(n)
function that returns 1 (true) is n is prime
and 0 (false) if n is not prime. Don't worry about making the test efficient
or doing it with integer arithmetic.
function r = isprime(n) % isprime(n): return 1 (true) of n is prime, 0 (false) if it isn't. (crude!) r = 1; % set answer to true to start % check if n is divisble by 2, return false if it is if 2*round(n/2) == n r = 0; % set answer to false return; % exit function right away end % now check all odd numbers between 3 and sqrt(n) for m=3:2:round(sqrt(n)) if m*round(n/m) == n r = 0; return; end end end
30. Write a function that returns a vector of all the integer divisors of an integer n. Again, don't worry about efficiency or integer arithmetic.
function d = divisors(n) % divisors(n): return vector of all divisors of n m = 0; % set number of divisors to zero for k = 1:n if k*round(n/k) == n m = m+1; % increment number of divisors d(m) = k; % add m to vector of divisors end end end