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
``````

theres alot of errors, and im havin alot of trouble on it, so PLEASE HELP

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
4
Views
8 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

Your line:

``while (i<=k)``

is placed before k is properly initialised. I think you need a line inserted to receive the input that you are prompting for. That is similar to your line that reads:

``x=input.nextDouble();``

but for k instead of x (I'll let you figure that out). Not sure if this is your only error, but it will at least allow your loop to run.

Also, to help us to help you, please describe the error you are receiving, don't just say there are lots of errors as that doesn't really tell us much.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.