-3

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; 
} 
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Jack Hsu
  • 17
  • 2
  • [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) – 463035818_is_not_an_ai Mar 31 '22 at 07:22
  • 1
    if you tried for days as you say then you should have something that you can show. If its not working others can help fix it. SO is not a codewriting service, and writing the code for you without explaning it so you can guess, is a very bad strategy for learning – 463035818_is_not_an_ai Mar 31 '22 at 07:24
  • Try with each term separately: Calculate `x`, `x*x/2*1`, `x*x*x/3*2*1`. See what calculation is needed to get from one element to the next. Then calculate them in a loop and sum up. – Gerhardh Mar 31 '22 at 07:25
  • The exponential should be straightforward. The logarithm is a little bit trickier because the limited convergence range. Maybe [this](https://stackoverflow.com/a/70628310/17862371) answer to another question helps. – Jakob Stark Mar 31 '22 at 07:32
  • 1
    I do not get the "They converge for ." Looks like something is missing there. And "They converge for x=1" is weird, they only converge for a single input value? I suspect there should be `<=` or `>=`. But since it is a picture of text (otherwise NOT appreciated here by the way) it seems that your assignment is wonky.... – Yunnosch Mar 31 '22 at 07:34
  • @Yunnosch Look at [wikipedia](https://en.wikipedia.org/wiki/Taylor_series#Natural_logarithm), from the source of where this text is copied. It should mean "They converge for `|x|<1`" but the guy who made the assignment obviously made a copy paste error. – Jakob Stark Mar 31 '22 at 07:44
  • 1
    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 – Spektre Mar 31 '22 at 10:07
  • That `while` loop does not do anything. You should run your program in a debugger and see where your program execution goes along. – Gerhardh Mar 31 '22 at 10:12
  • 2
    Thanks for editing the question. I had the honor to give the final reopen vote. I expect an answer, ideally according to https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions to arrive soon. – Yunnosch Mar 31 '22 at 10:50
  • 1
    Welcome to Stack Overflow! Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare https://stackoverflow.com/help/self-answer – Yunnosch Apr 02 '22 at 11:38

3 Answers3

1

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.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

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.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • This is practically the second hint-step I had in mind. Nice, almost as if we synchronised before answering. ;-) – Yunnosch Mar 31 '22 at 11:30
  • @Yunnosch as the code was way off, I focused on guidance how to do it from scratch. If they don't recognize the rules for each step, they will not be able to put that into proper loops. – Gerhardh Mar 31 '22 at 11:34
1
#include <stdio.h>

int main() {
    float power;
    printf("Enter the power of e\n");
    scanf("%f", &power);
    float ans = 1;
    float temp = 1;
    int i = 1;
    while ((temp * power) / i >= 0.00001) { 
        temp = (temp * power) / i;
        ans = ans + temp;
        i++;    
    }
    
    printf("%.5f", ans);
    return 0;
}

I think I solved the problem But the part about Natural Log is not solved, I will try.

Jack Hsu
  • 17
  • 2