Below I described a complex polynomial which is a part of my newton method. When I define the complex number [x = new ComplexNumber (0,0)] it only returns 0 or 1. Also I have overriden the basic string format to make it look like a complex number, but it doesn't show. As a beginner I am clueless.

btw I have defined all the methods which are used in the code below and tested them, they all work and the output is like expected.

note: the polynomial is in fractions(part+part...). First I split up the polynomial into a real and an imaginary bit, but then i had problems with defining a complex root.

public static ComplexNumber p(ComplexNumber x){
    	ComplexNumber a = new ComplexNumber(1,0);
    	ComplexNumber b = new ComplexNumber(0,1);
    	ComplexNumber c = new ComplexNumber(2,0);
    	ComplexNumber d = new ComplexNumber(0,3);
    	return ((a.add(b.mul(x))).add((c.add(d))).mul(x.power(6)));
	}

thanks in advance

Recommended Answers

All 8 Replies

anyone?

You don't specify what is your problem.
And if this doesn't work:

return ((a.add(b.mul(x))).add((c.add(d))).mul(x.power(6)));

there is no way to understand what is wrong.
Better try splitting them and printing the result separately to see where the error is

Try to debug and see the values of the variables.
Or add "System.out.println" at several places in your code.

If you have a big piece of code then explain which part doesn't work and how you have coded that part. Maybe I will understand from your explanation what is the problem.

Try to split the code into small pieces, put them into methods and test them separately.

I did, the above part doesn't work. The complex arithmic operations I defined like below, tested them with println and they work.

ComplexNumber a + compelxNumber b = a.add(b)

You didn't have to PM me the code. I was in the middle of writing this and the error is very obvious and very common.
Let's take these lines:

result.setRe(Math.pow(result.modulus(),n)*Math.cos(n*result.arg()));
result.setIm(Math.pow(result.modulus(),n)*Math.sin(n*result.arg()));

Firstly this: result.setRe(Math.pow(result.modulus(),n)*Math.cos(n*result.arg())); You correctly take the power of modulus, multiply it with the cos of the angle and you get the real part. BUT then you set it to the "result" complex number (WRONG). Therefor you change the real part of the original number (result). So when you try to calculate at the second line the "im" part and you call:
Math.pow(result.modulus(),n)
the result.modulus will not return the value that the previous line returned. Since you changed the real part of the result, you will return a modulus that has the new real part and the old im part at the second line.
So try this:

double a = Math.pow(modulus(),n) * Math.cos(n*arg());
double b = Math.pow(modulus(),n) * Math.sin(n*arg());

return new ComplexNumber(a,b);

Do not modify and use to your calculations the same object. Modify the new Complex object, but use the values of the existing number to set the results.

Also at the toString method maybe you need this:

String im_sign=(im < 0)?"-" : "+";

Also I haven't looked the rest of the code but this seems to be ok:

return a.add(b.mul(x)).add((c.add(d)).mul(x.power(6)));

I can't thank you enough for your help! I overlooked it, now I have rewritten the toString method and some flaws and now it works perfectly. :)

Have you corrected the other methods as well?

-----------------------------------------------------------
Also:
Just to make something clear to the other readers of this thread.

I didn't look at the code you sent me via PM. I found the error at the part of the code you posted just before you edited it and removed it.

This has nothing to do with you Frostra. You have written your code, had a question and asked it with respect to the forum rules.

I just don't want the other new guys thinking that they can send me their questions via PM and I will send them the answers. I only pay attention to what is posted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.