Function to implement
Code
public class arctan {
public static double arctan(double x) {
double sum = 0;
int k = 0;
double arctan1 = (Math.pow(-1, k) * (Math.pow(x, 2 * k 1) / (2 * k 1)));
for (int i = k; i < 100; i ) {
sum = arctan1;
}
return (double) arctan1;
}
}
Issue
My program just gives back my x
as output. I don't see the mistake I am doing.
CodePudding user response:
You have to put double arctan1 = (Math.pow(-1, k) * (Math.pow(x, 2 * k 1) / (2 * k 1)));
in the loop as well since that is what the Σ is doing in the formula.
You also didn't need to have a new variable i
in the for loop in this case. Using k
like the formula is fine.
So it would be like this instead:
public class arctan {
public static double arctan(double x) {
double sum = 0;
for (int k = 0; k < 100; i ) {
sum = (Math.pow(-1, k) * (Math.pow(x, 2 * k 1) / (2 * k 1)));
}
return sum;
}
}
CodePudding user response:
What does the letter Σ (sum over) mean?
See Wikipedia about the mathematical sum-symbol (uppercase sigma in Greek alphabet): Σ.
In you case it is the sum over a range from k = 0
until k = infinite
.
The sum of the term following the sigma.
Define it as function instead variable
The term following is implemented correctly by:
double arctan1 = (Math.pow(-1, k) * (Math.pow(x, 2 * k 1) / (2 * k 1)));
Extract it as function of k
and x
:
public static double arctan1(int k, double x) {
return ( Math.pow(-1, k) * (Math.pow(x, 2 * k 1) / (2 * k 1) ));
}
Because the calculation is depending on inputs k
and x
now, you can use it in your sum-over range of k:
// Note: the limit is 99 here, not infinite
for (int k = 0; k < 100; k ) {
sum = arctan1( k,x ); // increasing k used as input
}
Put it all together
// your "term following the sum-over" implemented by function arctan1(int k, double x)
public static double arctan(double x) {
double sum = 0;
// your loop to sum-up for increasing k
return sum;
}