Solve Taylor Series for Exponential function(ex) using C7 Jan 2025 | 4 min read Taylor Series is a mathematical representation of different functions introduced by Brook Taylor in the 18th century. Every function, when represented using Taylor series, is extended as an infinite sum of terms. General form: f(x) = f(a) + f'(a)(x - a) + f''(a)(x - a)^2/2! + f'''(a)(x - a)^3/3! + ...n terms When the exponential function (ex) is represented in this form: ex = 1 + x/1 + x2/2! + x3/3! +….+ n terms This tutorial explains different ways using which we can write a program that can give the value of the function given the values of x and n: Inputs: x and n For example, given x = 4 and n = 2, we need to return the value after calculating the expansion on e4 till 2 terms. e4 = 1 + 4/1 + 42/2! Observe that there are two functions in every term:
Approach 1: Implementing two functions for both m power n and factorial:Approach: For x = 3 and n = 2: power(3, 2)/fact(2) + taylor(3, 1) 9/2 + power(3, 1)/fact(1) + taylor(0) 4.5 + 3 + 1 8.5 Generalization: Taylor(x, n) = power(x, n)/factorial(n) + Taylor(x, n -1) Output: Enter the value of x: 3 Enter the value of n: 2 e to the power 3.0 till 2 terms: 8.500000 Too many recursive functions; let us simplify this: Approach 2: Only one function:We need the functionalities of power and factorial in just one function that calculates the value of the Taylor series: Let's take an example: x = 3, n = 2 For n = 0 -> pow(3, 0) = 1, fact(0) = 1, Taylor(3, 0) = 1 For n = 1 -> pow(3, 1) = 3, fact(1) = 1, Taylor(3, 1) = 3 For n = 2 -> pow(3, 2) = 9, fact(2) = 2, Taylor(3, 2) = 8.5 Observe that in every recursive call, pow(m, n) = m*pow(n-1) and fact(n) = n*fact(n-1) should be variables instead of separate functions. The catch here is that the values need to be persisted amid recursive calls of the Taylor function, for which we need to use the Static Variables. Output: Enter the value of x: 3 Enter the value of n: 2 e to the power 3.0 till 2 terms: 8.500000 We need to multiply for power and factorial in every recursive call. The number of multiplications performed has to be decreased. Approach 3 (Horner's rule):Horner's rule can be used to solve polynomial expressions without too many multiplications, making the program faster. We keep taking the variable common and find the pattern that we can use in loops or in recursive calls. Approach: ex = 1 + x/1 + x2/2! + x3/3! + …. + n terms Let's assume that n = 4: ex = 1 + x/1 + x2/2! + x3/3! + x4/4! = 1 + x/1(1 + x/2 + x2/2*3 + x3/2*3*4) = 1 + x/1(1 + x/2(1 + x/3 + x2/3*4) = 1 + x/1(1 + x/2(1 + x/3(1 + x/4))) Generalization: For n = 4: Assume sum = 1 Calculate 1 + x/4 Multiply sum = 1 + (x/4) * sum Which means: Initially, sum = 1 sum = 1 + (x/n) * sum Output: Enter the value of x: 3 Enter the value of n: 2 e to the power 3.0 till 2 terms: 8.500000 Iterative approach: Output: Enter the value of x: 3 Enter the value of n: 2 e to the power 3.0 till 2 terms: 8.500000 Next TopicC Programming Test |
? In this article, we will discuss how to initialize the char array in struct in C. Before initialization, we must know about the struct and character arrays in C. What are Structs in C? In C, a struct is a user-defined data type that enables us to...
4 min read
In computer science, trees are among the most common thing that is used in data structures. They offer speedy insertion, deletion, and search functions together with an effective method of storing hierarchical data. Trees are used in many different contexts, such as database indexing, sorting...
7 min read
The pthread library is an essential tool for multi-threaded programming in C, allowing for the creation and control of threads. Threads, as lightweight processes, enable parallel execution of code within a single process, allowing for better resource utilization and performance. The pthread_getcpuclockid() is an important...
5 min read
C, a powerful and versatile programming language, offers developers various tools to structure their code efficiently. Two fundamental elements in C, structs and enums, play a crucial role in organizing and managing data. In this article, we will discuss the difference between Struct and Enum...
8 min read
In this article, we will discuss Tokens and Terminals in C. But before discussing their differences, we must know about the Tokens and Terminals. What are Tokens? Tokens are some of the most significant components used in the C programming language. Tokens in C are the smallest...
3 min read
<complex.h> header file in C The complex.h header file is used by most C programs to perform complex number operations and conversions. This header file is included in the C99 Standard. The C++ standard library includes a header, complex<T>, that implements complex values as a template class,...
3 min read
In this article, you will learn about the difference between Sentinel and Counter Controlled Loop in C. But before discussing their differences, you must know about the Sentinel and Counter Controlled Loop. What is the Sentinel-Controlled Loop? A loop in which the execution continues until a particular...
6 min read
In the world of C programming, developers are often faced with terms such as identifiers and variables. Although related to one another, it's crucial that we take care not to overlook distinctions between them when writing logical code in a concise manner. In this article, we...
6 min read
A process is the execution of an instruction in C programming. When you execute a C program, it turns into a process. During its execution, a process has its own space for memory, resources, and state. It begins, executes its instructions, and then exits. The operating...
3 min read
In this article, we will discuss about the Array and Union in C. But before discussing their differences, we must know about the Array and Union in C. What is the Array? An array is a collection of equivalent data elements that may be referred to by...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India