====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
gibson:teaching:spring-2018:math445:lab2 [2018/01/30 03:47] gibson [Part 2: Computing sums] |
gibson:teaching:spring-2018:math445:lab2 [2018/01/31 16:07] (current) gibson [Part 3: Plots] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Math 445 lab 2: plotting and scripts====== | + | ====== 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. | Do the problems for parts 1 and 2 at the Matlab prompt, saving your work to a file with the ''diary'' command. | ||
Line 15: | Line 15: | ||
<code Matlab> | <code Matlab> | ||
[] % square brackets | [] % square brackets | ||
+ | , % row separator | ||
+ | ; % col separator | ||
: % colon operator | : % colon operator | ||
' % apostrophe | ' % apostrophe | ||
Line 20: | Line 22: | ||
</code> | </code> | ||
- | 1. Assign a row vector with elements 3, 4, 5, 9 to the variable u. | + | **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. | + | **2.** Assign the transpose of u to the variable w. |
- | 3. Change the third element of w to 10. | + | **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. | + | **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? | + | **5.** Add w and z. Does the result make sense? |
- | 6. Add u 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. | + | **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. | + | **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. | + | **9.** Make x a vector from 0 to $2\pi$ in increments of 0.02. |
==== Part 2: Computing sums ==== | ==== Part 2: Computing sums ==== | ||
- | 10. What is the sum of every third number between 3 and 27, inclusive? | + | Relevant Matlab syntax |
+ | <code matlab> | ||
+ | : % colon operator | ||
+ | sum % sum function | ||
+ | .* ./ .^ % dot syntax | ||
+ | </code> | ||
+ | |||
+ | |||
+ | |||
+ | **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? | + | **11.** What is the sum of the integers between 1 and 100, inclusive? |
- | 12. The infinite series | + | **12.** The infinite series |
\begin{equation*} | \begin{equation*} | ||
Line 53: | Line 64: | ||
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. | 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 | + | **13.** The $\sin$ function can be calculated from the infinite series |
\begin{equation*} | \begin{equation*} | ||
Line 65: | Line 76: | ||
- | 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? | + | **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? | Are you perplexed or what? What is strange about this calculation? Can you explain what happened? | ||
Line 72: | Line 83: | ||
==== Part 3: Plots ==== | ==== Part 3: Plots ==== | ||
+ | Relevant Matlab | ||
+ | <code 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 | ||
+ | </code> | ||
- | 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 intersectionf of $f$ with the $x$-axis are visible. | + | **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$. | + | **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.) | ||