Homework help

Up Vote 0 Down Vote

``````  #1
24 Minutes Ago | Add to kulrik's Reputation | Flag Bad Post
``````

Im having trouble with this problem:

The Maclaurin series for arctan(x) is a formula which allows us to compute an approximation to arctan(x) as a polynomial in x. The formula is:

``````arctan(x) = x - x3/3 + x5/5 - x7/7 + x9/9 - x11/11 + . . .
``````

Write a method called calculateArctan in the class Mymath . The method reads in a double x, and a positive integer k, and prints out the partial sum from the first k terms in this series. Write a main method to test the method calculateArctan.

Sample output

``````Enter x:
0.5
Enter integer k:
3
The partial sum from the first k terms: 0.4645833333333333

The arctan of 0.5 : 0.4636476090008061
``````

I started it out and this is all I got:

``````import java.util.Scanner;

public class MyMath
{
public void calculateArctan()
{

double x;
double sum;
double z;
int i=1;
Scanner input = new Scanner(System.in);
System.out.println("input x");
x=input.nextDouble();
double k;
System.out.println("input k");
z=0;
sum=0;
while (i<=k){

{ sum=sum + Math.pow(x, i)/i;
if(i%2==0)
sum=sum-Math.pow(x,i)/i;

z = z + 1;
}
}//end while
}

public static void main(String[]args)
{
MyMath m;
m = new MyMath();
m.calculateArctan();
}

}//end method
``````

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by darkagn

Come on, this is already about the third time you post this question, you better spend your time in figuring out a solution to your problem, double posting is not going to help you.

Edited by mvmalderen: n/a

ive been at it for a week already. im not that good with while loops

``while (i<=k)``
``x=input.nextDouble();``