Skip to main content

# 3.10: for Loop Incremental Development Exercise

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

##### Exercise $$\PageIndex{1}$$ for Loop Incremental Development Exercise

Write a for loop that does the following.

Start with any existing for loop, preferably one that sums a series, such as the one in section 3.8.

Test you code after you code each step in the process.

1. Opens a figure for plotting.
2. Sets the number of terms in the series to be n = 20
3. Initializes series_sum = 0; % (Do not name the variable "sum", becuz that is a built-in MATLAB function.)
4. Creates a for loop with index k = 1:n
5. Computes termk = 3/(k^2)
6. Adds termk to series_sum, and stores the result back into the series_sum variable.
7. For each value of k, plots termk with the '*' marker.
8. For each value of k, plots series_sum with the 's' marker.
9. After the for loop, display the final values of termk and series_sum.
10. Adds a title to the plot.
Answer

Add texts here. Do not delete this text first.

This page titled 3.10: for Loop Incremental Development Exercise is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

• Was this article helpful?