User Tools

Site Tools


gibson:teaching:fall-2014:math445:hw2solns

Math 445 HW2 solutions

% John Gibson
% Math 455 HW2
% Oct 7, 2014

% Problem 1: Write a Matlab expression that sums the first N terms 
% of each series and evaluate it for N=100.

% (a) 1 + 1/2 + 1/3 + 1/4 + … 

N=100; sum(1./(1:N))
ans =
    5.1874

% (b) 1 + 1/2 + 1/4 + 1/8 + …

N=100; sum(0.5.^(0:N-1))
ans =
     2

% (c) 1 + 1/3 + 1/9 + 1/27 + …

N=100; sum(3.^-(0:N-1))
ans =
    1.5000

% (d) 1/2 + 2/3 + 3/4 + 4/5 + …

N=100; sum((1:N)./(2:N+1))
ans =
   95.8027

% (e) 1/2 - 2/3 + 3/4 - 4/5 + …

N=100; sum((-1).^(0:N-1).*(1:N)./(2:N+1))
ans =
   -0.3019

% Problem 2: Given vectors x,y of the same length, write an expression 
% that has value true (1) if each component of x is greater than the 
% corresponding component of y, and has value false (0) otherwise.

all(x>y)

% Problem 3: Given vectors x,y of the same length, write an expression 
% that has value true (1) if any component of x is greater than the 
% corresponding component of y, and is false (0) otherwise.

any(x>y)

% Problem 4: Given vector x, write an expression that has value true (1) 
% if the elements of x are sorted in increasing order (that is, if 
% no element is less than the previous element) and false otherwise.

all(x(1:end-1) <= x(2:end))

% Problem 5: Plot sin(x) versus x for 100 evenly space points in x from 
% 0 to 2pi, using a solid blue line.

x = linspace(0,2*pi,100);
plot(x, sin(x), 'b-');
title('Problem 5')
xlabel('x')
ylabel('sin(x)')

% Problem 6: Plot sin(x) in red, cos(x) in green, over same x as problem 5. 
% Use 'legend' to indicate which function is shown in which color. 

plot(x, sin(x), 'r-', x, cos(x), 'g-');
xlabel('x')
legend('sin(x)','cos(x)')
title('Problem 6')

% Problem 7: Plot y = 5x^2 - 4x - 3 for 40 evenly space points in x from -2 to 2, 
% using a red line and a circle on each data point. Superimpose an x,y grid on 
% the plot.

x = linspace(-2,2,40);
plot(x, 5*x.^2 - 4*x - 3, 'ro-');
grid on
xlabel('x')
ylabel('y = 5*x.^2 - 4*x - 3')
title('Problem 7')

% Problem 8: Make a plot of the unit circle. Make sure it's closed –no gap! 
% Hint: use linspace to specify a range of angles, then cos and sin to produce 
% vectors of the x,y coordinates of points on the unit circle.

theta = linspace(0,2*pi,200);
plot(cos(theta), sin(theta))
axis equal                      % bonus trick to get equally scaled axes
axis square			% bonus trick to plot square axes
grid on
xlabel('x')
ylabel('y')
title('Problem 8')

% Problem 9: Plot  for 100 points x evenly space between -2 and 5. Choose 
% the most appropriate plotting function. Hint: it ain't plot!

x = linspace(-2,5, 100);
semilogy(x,10.^(3*x))
grid on
xlabel('x')
ylabel('10^{3x}')               % bonus trick to produce nicely formatted ylabel
title('Problem 9')

% Problem 10: Plot 3x^5 for 100 points x evenly space between 1 and 4. 
% Choose the most appropriate plotting function. Again, it ain't plot!

x = linspace(1,4, 100);
loglog(x,3*x.^5))
grid on
set(gca, 'xtick', [0 1 2 3 4])  % bonus trick to label the x gridpoints
xlabel('x')
ylabel('3x^5')
title('Problem 10'){{:gibson:teaching:fall-2014:math445:hw2:problem5.png?nolink&300|}}

gibson/teaching/fall-2014/math445/hw2solns.txt · Last modified: 2014/10/07 10:38 by gibson