Error writing recursive function for Bonnet's recursion formula in C

In summary: I'm glad you were able to figure out the issue and find a solution. Good luck with your future coding!In summary, Bonnet's recursion formula for Legendre polynomials can be written as P(n,x)=1 for n=0, P(n,x)=x for n=1, and P(n,x)= (2n-1)/n*x*P(n-1,x)-(n-1)/n*P(n-2,x). A recursive function in C was attempted to calculate the value of the polynomial for a given n and x, but it did not give the correct result because of the use of integer division. By forcing floating point division to occur, the function can be fixed and give the expected output.
  • #1
PrakashPhy
35
0
Bonnet’s recursion formula for Legendre polynomials is
P(n,x)=1 for n=0
P(n,x)=x for n=1 and
P(n,x)= (2n-1)/n*x*P(n-1,x)-(n-1)/n*P(n-2,x)

I tried to write recursive function in C to calculate the value of polynomial for a given n and x
Code:
float legpol(int n, float x)
    {
        if(n==0)
            return 1;
        else if(n==1)
            return x;
        else
            return(2*n-1)/n*x*legpol(n-1,x)-(n-1)/n*legpol(n-2,x);
    }
But this doesn't give the correct result. What's wrong with the function??

Thanks in advance
 
Technology news on Phys.org
  • #2
Can you give a sample of the output vs. what the correct answer should be?
 
  • #3
The actual legendre's polynomials are
for n = 0 1
for n= 1 x
for n = 2
82722ebdf5cc1c9deb3475a7ec25bd3d.png

for n= 3
b8e8e3c74d685b1d03f1d11dd2b0a78d.png

for n =4
1b43b5affe459365b174311803a08ad6.png

and so onif n= 2 and x= .1 the output must be -0.485 but my recursive function gives 0.01 which is wrong
 
  • #4
The problem is, I believe, that your formula is using int division in a couple of places, and they give you incorrect function values. Inexperienced C and C++ programmers often don't realize that there are two kinds of division - integer and floating point.

Your code has this line:
Code:
return(2*n-1)/n*x*legpol(n-1,x)-(n-1)/n*legpol(n-2,x);

If n == 3, for example, the (2*n - 1)/n expression evaluates to 5/3 == 1. The other int term is multiplied by 2/3 == 0.

An easy fix is to change the line above to
Code:
return(2*n-1.0)/n*x*legpol(n-1,x)-(n-1.0)/n*legpol(n-2,x);

This change causes the numerators of both fractions to be promoted to double before the division actually occurs, forcing floating point division to be performed, rather than integer division.
 
  • #5
Thank you! This change worked fine.

I tried to figure out the error and checked the value of 'n' after each function call and thought there might have been changes in the value of 'n' within the recursive function call. So i tried to preserve the value of 'n' by creating another variable 'b' just to use it in nested function call. I modified the above function like this::
Code:
float legpol(int n, float x)
    {
        int b;
        if(n==0)
            return 1;
        else if(n==1)
            return x;
        else
        {
             b=n;
             return(2*b-1)/b*x*legpol(b-1,x)-(b-1)/b*legpol(b-2,x);
         }
     }

Amazingly it worked. In this case why shouldn't i write (2*b-1.0) ??
 
  • #6
I don't know why your revision should work. For some int divisions you get the answer you would expect, such as 6/3 or 10/2. It might be that what got evaluated was something like this.

In any case, I would get rid of the b variable and use code that forces floating point division, such as 2nd example in my previous post.
 
  • #7
I'm sorry for the second revision i posted. I had actually typed
Code:
float b;
in the third line of my function not
Code:
 int b
.

I now know what the problem was. Thank you very much for the solution. It really helped me.
 
  • #8
PrakashPhy said:
I'm sorry for the second revision i posted. I had actually typed
Code:
float b;
in the third line of my function not
Code:
 int b
.

I now know what the problem was. Thank you very much for the solution. It really helped me.
Yes, that would have made a difference, by causing floating point division to be performed instead of integer division.
 

Related to Error writing recursive function for Bonnet's recursion formula in C

1. How does Bonnet's recursion formula work?

Bonnet's recursion formula is a mathematical equation used to calculate the coefficients of a polynomial function. It involves repeatedly applying a specific formula to the coefficients until the desired result is reached.

2. What is the purpose of using a recursive function for Bonnet's recursion formula?

A recursive function is used to simplify the process of applying Bonnet's recursion formula. It allows for a more efficient and organized way of calculating the coefficients of a polynomial function.

3. What are some key considerations when writing a recursive function for Bonnet's recursion formula in C?

When writing a recursive function for Bonnet's recursion formula in C, it is important to consider the base case, which is the stopping point for the recursion. Additionally, the function should handle error cases and be optimized for efficiency.

4. Are there any potential challenges when implementing Bonnet's recursion formula in C?

Yes, there can be several challenges when implementing Bonnet's recursion formula in C. Some common challenges include managing memory, handling overflow/underflow, and ensuring the function is correctly written to avoid infinite recursion.

5. How can I test the accuracy of my recursive function for Bonnet's recursion formula in C?

You can test the accuracy of your recursive function by comparing the results to a known solution or using a mathematical proof. Additionally, you can test the function with different inputs and compare the results to a manual calculation. Debugging tools can also be helpful in identifying any errors in the function.

Similar threads

  • Programming and Computer Science
Replies
11
Views
829
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
795
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
Back
Top