Home > Net >  Why does my approximation of Exponential using Taylor Series expansion return "inf"?
Why does my approximation of Exponential using Taylor Series expansion return "inf"?

Time:03-31

This is my homework:

problem statement

I haven't tried to write the part of Natural Logarithm because I can't solve the part of Exponential.

This is the the approximations of Exponential in C using Taylor Series expansion I wrote.

However, it returns inf. What did I do wrong?

#include <stdio.h> 

// Returns approximate value of e^x  
// using sum of first n terms of Taylor Series 
float exponential(int n, float x) 
{ 
    float sum = 1.0f; // initialize sum of series  
    for (int a = n; a >= 0;   a ) {
        while (x * sum / a < 0.00001) {
            break;
        } 
        sum = 1   x * sum / a; 
        return sum; 
    }
} 

int main() 
{ 
    int n = 0;
    float x = 1.0f; 
    printf("e^x = %.5f", exponential(n, x)); 
    return 0; 
} 

CodePudding user response:

With How do I ask and answer homework questions? in mind, I will give you a few things to have a careful look at.

From comment by Spektre:

from a quick look you are dividing by zero in while (x * sum / a < 0.00001) during first iteration of for loop as a=n and you called the function with n=0 ... also your code does not match the expansion for e^x at all

Have a look at the for loop:

for (int a = n; a >= 0;   a )

What is the first value of a? The second? The third?
Keep in mind that the values are determined by a.

When will that loop end? It is determined by a >= 0. When is that false?

What is this loop doing?

while (x * sum / a < 0.00001) {
            break;
        } 

I suspect that you programmed "English to C", as "do the outer loop while ...", which is practically from the assignment.
But the loop does something else. Apart from risking the division by 0 mentioned above, if the condition is true it will stay true and cause an endless loop, which then however is immediatly canceled in the first iteration.

The head of your function float exponential(int n, float x) expects n as a parameter. In main you init it with 0. I suspect you are unclear about where that value n is supposed to come from. In fact it is unknown. It is more a result of the calculation than an input.
You are supposed to add up until something happens.
You do not actually ever need the value of n. This means that your for loop is meaningless. The inner loop (though currently pointless) is much closer to your goal.

I will leave it at this for now. Try to use this input.
Feel free to edit the code in your question with improvements.
(Normally that is not appreciated, but in case of homework dialog questions I am fine with it.)

CodePudding user response:

Your current implementation attempt is quite a bit off. Therefore I will describe how you should approach calculating such a series as given in your quesiton.

Let's look at your first formula:

You need to sum up terms e(n) = x^n / n! To check with your series: 1 == x^0 / 0! - x == x^1 / 1! - ...

To calculate these terms, you need a simple rule how to get from e(n) to e(n 1). Looking at the formula above we see that you can use this rule:

e(n 1) = e(n) * x / (n 1)

Then you need to create a loop around that and sum up all the bits & pieces.

You are clearly not supposed to calculate x^n/n! from scratch in each iteration.

Your condition to stop the loop is when you reach the limit of 1e-5. The limit is for the new e(n 1), not for the sum.

For the other formulas you can use the same approach to find a rule how to calculate the single terms. You might need to multiply the value by -1 in each step or do something like *x*n/(n 1) instead of *x/(n 1) etc.

Maybe you need to add some check if the formula is supposed to converge. Then maybe print some error message. This part is not clear in your question.

As this is homework, I only point into the direction and leave the implementation work to you.

If you have problems with implementation, I suggest to create a new question.

  • Related