User Tools

Site Tools


Math 445 lab 2: vectors, plots, and scripts

Do the problems for parts 1 and 2 at the Matlab prompt, saving your work to a file with the diary command.

Be kind to your teaching assistant!

  • Label each problem with a Matlab comment (e.g. % ==== Problem 1 ====).
  • For questions with free-form response, write your answer as a Matlab comment. Write in complete sentences!
  • Reduce unnecessary whitespace with the format compact command.
  • When you're done, edit the diary file to eliminate errors, so that your diary shows just the correct work.
  • Add and subtract blank lines as necessary to make your work organized and easiy readable.

Part 1: Vector manipulation

Relevant Matlab

[] % square brackets
,  % row separator
;  % col separator
:  % colon operator
'  % apostrophe

1. Assign a row vector with elements 3, 4, 5, 9 to the variable u.

2. Assign the transpose of u to the variable w.

3. Change the third element of w to 10.

4. Assign a column vector with the elements 7, 1, -2, 3 to the variable z.

5. Add w and z. Does the result make sense?

6. Add u and z. Does the result make sense?

7. Create a vector of the even integers between 2 and 14, inclusive.

8. Create a vector of the odd integers between 7 and 19, inclusive.

9. Make x a vector from 0 to $2\pi$ in increments of 0.02.

Part 2: Computing sums

Relevant Matlab syntax

:        % colon operator
sum      % sum function
.* ./ .^ % dot syntax

10. What is the sum of every third number between 3 and 27, inclusive?

11. What is the sum of the integers between 1 and 100, inclusive?

12. The infinite series

\sum_{n=1}^{N} \frac{1}{n}

diverges as $
N \rightarrow \infty$. Demonstrate this in Matlab by computing the sum for several values of $N$, e.g. $N=10$ to $N=10^5$ by powers of ten.

13. The $\sin$ function can be calculated from the infinite series

\sin x = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n+1}}{(2n+1)!}

Of course we have to truncate this infinite sum to a finite number of terms in order to calculate it on a computer. How many terms do you need to keep in order to compute $\sin \: \pi/2 = 1$ to sixteen digits accuracy? Hint: use format long to see all sixteen digits of the computation.

Are you amazed or what?

14. Now use the same infinite series for $\sin x$ to calculate $\sin \: \pi = 0$. How many terms do you need to keep in order get the correct answer to sixteen digits accuracy?

Are you perplexed or what? What is strange about this calculation? Can you explain what happened?

Part 3: Plots

Relevant Matlab

.* ./ .^  % dot syntax
plot      % plot function
axis      % set limits on plot or aspect ratio
xlim      % set x limits
ylim      % set y limits
grid      % turn grid on/off
help      % help function

15. Make a plot of the polynomial $f(x) = x^3 -5x^2 + 2x + 3$ as a blue line. In the same plot, draw a line along the $x$ axis. (Hint: in Matlab create a zero vector of the same length as your $x$ vector with y=0*x, then plot $y$ versus $x$ along with $f$ versus $x$.) Using this plot, estimate the zeros of the polynomial, i.e. the values of $x$ for which $f(x) = 0$. Make sure to find all the zeros of $f$ by adjusting limits of the plot until all intersection of $f$ with the $x$-axis are visible.

16. Make a plot of a unit circle, i.e. a curve that satisfies $x^2 + y^2 = 1$. Draw the circle with a thick red line, label the axes, and give the plot a title. Make sure the circle looks like a circle and not an oval. Hint: don't try to draw the plot using the equation $x^2 + y^2 = 1$. Instead parameterize the curve in terms of the angle $\theta$, i.e. calculate $x$ and $y$ from $\theta$.

(I previously had a problem 17, revisiting the sum of $1/n$…to be written. But on second thought, this problem belongs in lab 3. So expect it there.)

gibson/teaching/spring-2018/math445/lab2.txt · Last modified: 2018/01/31 16:07 by gibson