# channelflow.org

### Site Tools

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

# Math 445 HW1 Solutions

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:2: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 - 2*x + 3)
xlabel('x')
ylabel('y = x^2 - 2*x + 3)')

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('y = 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-', );
xlabel('x')
legend('sin(x)', 'cos(x)');

7. Produce a vector whose components are random integers between 0 and 10, inclusive.

x = randi([0,10], 13,1);  % The problem didn't specify the length of the vector, choose 13

8. Produce a vector whose components are random real numbers between 0 and 10, inclusive.

x = 10*rand(13,1);        % The problem didn't specify the length of the vector, choose 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.

size(A,1) == size(A,2)

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 = divisible_by_3(x)
r = (x == 3*round(x/3));
end

or better, use the mod function

function r = divisible_by_3(x)
r = (mod(x,3) == 0);
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 = is_sorted(x)

s = 1; % start assuming x is sorted
for n=1:length(s)-1;
if s(n) > s(n+1)
s = 0;
break;
end
end
end  

17. finds a zero of another function using the bisection search algorithm.

function xcenter = bisectsearch(f, xleft, xright)
% function xcenter = bisectsearch(f, xleft, xright)
% find a zero of f between xleft and xright using bisection search algorithm

eps = 1e-13; % stopping condition: abs(f(xcenter)) < eps

fleft = f(xleft);
fright = f(xright);

% should put in check that fleft and fright have opposite signs

xcenter = 0.5*(xleft + xright);
fcenter = f(xcenter);

% repeat until f(xcenter) is close enough to zero
while (abs(fcenter) > eps)

if (sign(fleft) == sign(fcenter))
% replace the left stuff with the center stuff;
fleft = fcenter;
xleft = xcenter;
else
% replace the right stuff with the center stuff;
fright = fcenter;
xright = xcenter;
end

xcenter = 0.5*(xleft + xright);
fcenter = f(xcenter);
end

end  

18. 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);
% returns y = A*x;

[M,N] = size(A); % assume x is N x 1

y = zeros(M,1);

for i = 1:M
for j = 1:N
y(i) = y(i) + A(i,j)*x(j);
end
end
end 