Home > Back-end >  Of new don't know what wrong
Of new don't know what wrong

Time:11-25

Legendre polynomial

Title description:

Make up a program to input x, n, computing Legendre (Legendre polynomial of n items,

Input: a floating point number and an integer corresponding x and n (0 & lt;=n<=20),

Output: a floating point number, namely the Legendre polynomial n value, attention to the second decimal,


Sample 1:

Input: 3.4 2
Output: 16.84
Example 2:

Input: 3.4 to 10
Output: 30143685.82
Sample 3:

Input: 3.4 21
Output: 23525972077722828.00
#include
# define N 90
Double fuxc (double x, int n) {
int i;
Double f [N], s;
If (n==0) {
S=1;
}
If (n==1) {
S=x;
}
If (n & gt; 1) {
F [0]=0;
F [1]=x;
For (I=2; I & lt;=n; I++) {
[I]=f (2.0 * I - 1) * 1.0/I * x * f [I - 1) - (I * 1) * 1.0/I * f [I - 2);
}
S=f [n].
}
Return s;
}
Int main () {
Double x, z;
int n;
Lf the scanf (" % % d ", & amp; X, & amp; N);
Z=fuxc (x, n);
Printf (" % 2 lf ", z);
return 0;
}

CodePudding user response:

No grammatical errors, it must be logic errors,
You also didn't what's posted polynomial, who can help you???

CodePudding user response:

F [0]=0; To f [0]=1;
Otherwise,
I * f [2] I - divided by zero error

CodePudding user response:

You give the definition of Legendre polynomial

CodePudding user response:

The original poster should be want to write a recursive algorithm, modified as follows, for reference:
 # include 
Double fuxc (double x, int n)
{
Double s;
If (n==0)
return 1;
Else if (n==1)
Return the x;
The else
{
S=((2 * n - 1) * x * fuxc (x, n - 1) - (n - 1) * fuxc (x, n - 2))/n * 1.0;
Return s;
}
}


Int main (int arg c, char * argv [])
{
Double x, z;
int n;
Lf the scanf (" % % d ", & amp; X, & amp; N);
Z=fuxc (x, n);
Printf (" % 2 lf \ n ", z);
system("pause");
return 0;
}

CodePudding user response:

#include
# define N 90
Double fuxc (double x, int n)
{
int i;
Double f [N], s;
If (n==0)
{
S=1;
}
If (n==1)
{
S=x;
}
If (n> 1)
{
F [0]=1;
F [1]=x;
for(i=2; i<=n; I++)
{
[I]=f (2.0 * I - 1)/I * x * f [I - 1) - (I - 1)/I * f [I - 2);
}
S=f [n].
}
Return s;
}

Int main ()
{
Double x, z;
int n;
Lf the scanf (" % % d ", & amp; X, & amp; N);
Z=fuxc (x, n);
Printf (" % 2 lf ", z);
return 0;
}
  • Related