leftimage for poohprod.ru

Maclaurin series - some mathematical experiments with Matlab

Maclaurin series are fast approximations of functions, and they offer more accurate function approximations than just linear ones. You have to consider only one general formula and you can approximate even complicated function values.

Maclaurin series are simpler than Taylor’s, but Maclaurin’s are, by definition, centered at x = 0.

If you’re approximating a function value for an x-value far from 0, you’ll have to use the slightly more complicated Taylor series, which work almost exactly like Maclaurin series, except that you can center them at any x-value.

Mathematically, we say the Maclaurin series approximates values of f(x)
very accurately, as long as those x-values are close to 0.

When you work with these sequences, you’ll actually be generating Maclaurin polynomials, which are some finite terms of the series. Before you start generating the terms to be added, make sure you understand the formula.

Maclaurin series formula

In case you don’t know what f(n)(0) means, it is the nth derivative of f(x) evaluated at 0. For example, f(4)(0) is the fourth derivative of f(x) with x = 0.

The Maclaurin series generates good approximations of f(x), as long as
x is close to 0. You won’t use an infinite series to calculate the approximation. The more terms you use, however, the better your approximation will be.

As an example, let’s use the Maclaurin polynomial (with just four terms in the series) for the function f(x) = sin(x) to approximate sin(0.1).

We first write the terms of the series from n = 0 to n = 3.

Maclaurin series - example

And we’ll need to take four derivatives of the function and then evaluate them at x = 0.

Definition 1: f(0)(x) is the function itself. There’s no derivative.
Definition 2: 0! = 1.

f(0)(x) = sin(x) f(0)(0) = sin(0) = 0
f(1)(x) = cos(x) f(1)(0) = cos(0) = 1
f(2)(x) = -sin(x) f(2)(0) = -sin(0) = 0
f(3)(x) = -cos(x) f(3)(0) = -cos(0) = -1

Let’s create a script in Matlab to evaluate the series just with 4 terms (0 to 3):

clear, clc

% Let's see more decimals
format long

% We go from n = 0 to n = 3
n = 0 : 3;

% This is the point for evaluation
x = 0.1;

% These are the derivatives for each term
d = [0 1 0 -1];

% We form the sequence, following the formula
seq = d .* x.^n ./(factorial(n))

% We add-up to get the Maclaurin approximation
approx = sum(seq)

% Let's compare with the official number
real_value = sin(x)

The response is:

seq = 0 0. 0 -0.67
approx = 0.33
real_value = 0.83

We’ve got almost the exact value by using only 4 terms of the series (and two values are zero, because the derivative is zero for those terms). If we had used n = 8, it would have been an even better approximation.

Now, let’s develop (code) an automated series to express the sine function (centered at 0) using the Maclaurin expansion and let’s compare the results with different number of terms included.

function smp = maclaurin_sin(x, n)
% x = a vector with values around 0
% n = number of terms in the series

% Start the series with 0
smp = 0;

% Consider all the possible derivatives
deriv = [0 1 0 -1]';

% Iterate n times (from 0 to n-1)
for i = 0 : n-1

% Implement the Maclaurin expansion
t(i+1, :) = deriv(1) * x.^(i) / factorial(i);

% Find the derivative for the next term
deriv = circshift(deriv, -1);


% Add-up the calculated terms
smp = sum(t);

Note: the above code is not very efficient, because we’re calculating and adding terms even when their derivative is zero, which is a waste of 50% of the time, more or less. We’ll leave it as-is just to keep and show the natural flow of the formula...

Now, let’s create a script to test and use the function above.

clear, clc, close all

% Work with two full periods
x = -2*pi : .1 : 2*pi;
y = sin(x);

% Plot the goal
plot(x, y, 'g', 'Linewidth', 3)
'Study of Maclaurin series')
'sin(x) with different number of terms')
axis([-2*pi 2*pi -3 3])
hold on

% Consider 4 terms
smp = maclaurin_sin(x, 4);
plot(x, smp,

% Consider 6 terms
smp = maclaurin_sin(x, 6);
plot(x, smp,

% Consider 10 terms
smp = maclaurin_sin(x, 10);
plot(x, smp,
'k', 'linewidth', 3)

% Label the calculated lines
legend('sin(x)', '4-term series', ...
'6 terms', '10 terms')

The results are:

Maclaurin series expressed with different terms

We see that all of the expansions work well when we are close to 0. More terms approximate better a larger portion of the sine curve.

From 'Maclaurin Series ' to home

From 'Maclaurin Series ' to Calculus Problems


Taylor Series


Harmonic Series

footer for maclaurin series page

Related pages

ascii code azhow to figure out depreciation ratematlab loops tutorialhome amortization tablerecursivity5 band resistor calculatordecimal to binary converterdecimal to binary algorithmmatlab polarmatlab program for unit step functionfibonacci series generatorascii range of alphabetsrc circuit chargingroot finding in matlab3d animation matlabmatlab help polyfitusing matlab to solve simultaneous equationsbinary octal converterprogram lagrange interpolation matlabamoratization tableexpansion of sine functioncalculating factorialshow to solve piecewise defined functionstwo point slope form calculatorbinary to octal conversion formulaloop pascalmatlab input matrixde morgan theorem examplesannual depreciation rate for carsmathworks forumradioactive decay formula half lifequi square calculatormatlab exponential curve fithow to draw bell curvecalculation for bmibernoulli trials calculatorhow to plot a circle in matlabtaylors expansionmatlab gaussbisection method examplesascii char chartanalysis of rc circuithow to sketch a piecewise functionwhat are octal numberspiece wise function examplesparabolic interpolationbinary to decimal algorithmexamples of pythagoras theoremrecursive power functionhistogram matlab codematlab scilabequation of lines calculatorhow to solve smith chart problemscholesky decomposition matlabonline definite integrationmatlab regressionmatlab imaggui in matlab examplesprime factorization of 49theorem calculatormatlab log10online bell curve generatorgauss jordan inverse matrixhow to find the sum of infinite geometric seriescosine infinite serieshow to draw a 3d cuboidmatlab sample codessas trigmatlab inverse functiondatafitaddition of two numbers in matlabseries that converge to piharmonic series equationdecimal to gray code convertercompound interest continuously calculatorhow to find half life calculus