Skip to main content

Section 15.1 Maclaurin Expansion of \(sin(x)\) - A Tedious Example

Let’s start this discussion by looking at a real-world example (this one comes from the world of numerical methods, a crucial mechanical engineering course you will soon take!). We will be using this one particular example for a considerable percentage of this chapter.
If you are unfamiliar with Maclaurin expansion (it is a special form of a Taylor series expansion) that is OK! For now, you should just understand that it is a way to approximate a function as a polynomial with an infinite number of terms. Khan academy has a great explanation if you would like to understand more (or if you need a quick refresher on infinite sums). If you are shaky on what this paragraph is saying, be sure to take a minute to check out those links and get up to speed.
For our purposes we will just understand that the following is true:
\(\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\)
Recall, this is saying that we can represent \(sin(x)\) as an infinite polynomial and it will equal \(sin(x)\) exactly as long as we have an \(\infty\) number of terms.
Let’s consider the case where we need to approximate \(sin(x)\) with MATLAB and we decide to use the Maclaurin expansion to do so. Clearly, we can’t do an \(\infty\) number of terms, that would take an \(\infty\) amount of time! Instead, let’s truncate the expansion to 3 terms and see if it is good enough.
\(\sin(x) = \sum_{k=0}^{k=2} \frac{(-1)^k x^{2k+1}}{(2k+1)!} = x-\frac{x^3}{3!}+\frac{x^5}{5!}\)
We can now use this approximation to estimate something like (\(sin(2.618)\)). Note, the angle in this case, (2.618), is in radians.
Before we move on…
In order to check our approximation of \(sin(2.618)\) you first need to calculate the analytical value (i.e. true value) of \(sin(2.618\)).

Checkpoint 15.1.1. Evaluating the Sine Function.

What is \(\sin(2.618)\) equal to?
  • 0.5
  • Incorrect. Try evaluating \(\sin(2.618)\) using a calculator or MATLAB.
  • 0.0457
  • Incorrect. Check that your calculator is in radians mode.
  • -0.5
  • Correct. \(\sin(2.618)\) is approximately \(-0.5\text{.}\)
Now we know the correct answer. We are ready to calculate our approximation using a subset of the Maclaurin expansion.
\(sin(2.618) \approx 2.618-frac{2.618^3}{3!}+\frac{2.618^5}{5!}=0.65223\)
Ok, that isn’t a terrible approximation but it isn’t very good. What happens if we increase up to seven terms?
\(\sin(x) = \sum_{k=0}^{k=7} \frac{(-1)^k x^{2k+1}}{(2k+1)!} = x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\frac{x^9}{9!}-\frac{x^11}{11!}+\frac{x^13}{13!}\)
Well, we can be sure that it will be more accurate but it is clear that the more terms that we add, the more tedious this process becomes. Imagine increasing our estimate to 20 terms! Whenever a process is tedious and repetitive, it is a perfect candidate to have a computer do it. In order for a computer to do it though, we need to transfer this process into an algorithm that can solve this problem for an arbitrary (n) number of terms.
Remember our mantra, think, sketch, code, test, repeat.
THINK - You need to stop and think! You should be asking yourself these types of questions (and more!):

Checkpoint 15.1.2. Maclaurin Expansion.

Answer the questions above and add any other thoughts that might be helpful when writing this code.
Think about code flow. What do you want your code to accomplish and in what order?
SKETCH - I will trust that you spent a minute thinking about the problem and making sure that you understand all the moving pieces. You wouldn’t skip a brain workout, would you? The next step is to sketch.
Try It!
Before you move on and look at my flowchart, can you create pseudocode or draw a flowchart that can algorithmically solve this problem for an arbitrary (n) number of terms? It might be good to review what we learned in the thinking algorithmically chapter if you have not done so already.

Checkpoint 15.1.3. Ordering a FOR Loop.

Drag the steps into the correct order to create the pseudocode.
Keep in mind that it is OK if you are confused or stuck as long as you gave it an honest try! Here is my flowchart for this problem:
Figure 15.1.4. My flowchart for the Maclaurin series expansion of sin(x)
Now is a good time to go back to the think step. Look at my flowchart in figure 15.1.4 above and see if you can answer the following questions.
  • Does the flowchart make sense to you?
  • Do you understand how it works? Can you follow it along?
  • Do you understand what the true and false text indicators are referring to?
  • Do you understand why I have the little extra explanation box giving examples of variables?
We have now successfully completed the think and sketch stages of our process. In order to continue with the next step, code, we need to learn how to program these types of structures into MATLAB. Let’s take a break from this particular problem and investigate how to program the necessary functionality into MATLAB.
You have attempted of activities on this page.