Skip to main content

Section 15.3 Finishing the Maclaurin Example>

Hopefully, at this point, you can at least understand how a for-end loop is supposed to work and what it might be used for. You should also start to understand the 3 key concepts you should learn about these loops. It is OK if you are a little shaky in your knowledge, but you should at least know what you need to learn.
Try It!
Returning to our Maclaurin series example, we are now ready for the code phase. Based on what you have learned about for loops, can you write the MATLAB code that accomplishes our programming goal? To remind you, the goal was:
Create a MATLAB script that can return the value of the Maclaurin series expansion of \(sin(x)\) for angle \(x\) in radians, for an arbitrary \(n\) number of terms.
  • Recall the Maclaurin series expansion of \(sin(x)\) is:
    \(\sin(x) = \sum_{k=0}^{\infty} \frac{(-1)^k x^{2k+1}}{(2k+1)!} = x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\cdots\)
  • Remember that although only 4 terms are shown in the bullet point above, that this is an \(\infty\) sum
  • When we say an arbitrary \(n\) number of terms, your script should allow the user to change a variable n to change how many terms are used in the estimation.
  • For example, if the user specifies n = 3, and wants to estimate \(sin(1.15)\) by specifying x = 1.15, the program should calculate: (this is actually a pretty good estimate by the way) Give it your best shot! Try and see if you can come up with this code on your own! I believe in you!
Figure 15.3.1. I know this is difficult but no pain no gain! Give it your best shot!
My solution is shown below in Example 15.3.2:

Example 15.3.2. Maclaurin Solution.

Here is an interactive code box that functions identically to the MATLAB command window.
If you didn’t get this exactly it is OK as long as you tried. Spend some time with the script above in Example 15.3.2 until you understand how it works. Keep practicing, reading, and watching YouTube video tutorials until this is starting to click!

Checkpoint 15.3.3. Successes and Failures.

Compare your code to the answer key. What were some differences in execution and/or organization?
Does your code work the same as the code above? If it doesn’t, what did you have to chang? What was something you have learned about for loops (or coding in general) by completing this example?
You have attempted of activities on this page.