Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

limit fsolve to converge only to a real solution (not imaginary numbers)

1,229 views
Skip to first unread message

eric

unread,
May 26, 2013, 9:37:11 PM5/26/13
to
Hello there,
I am trying to solve this system of non-linear eqns. by using "fsolve" but as you may notice this may lead to a complex solution since the equations contain some imaginary terms but the solution i am looking for must be real.
Can you suggest me an optimal way solve this problem without including any imaginary terms that may appear in the solution?

F(1) = exp(1i*(x+r)) - exp(1i*A);
F(2) = exp(1i*(y+r)) - exp(1i*B);
F(3) = exp(1i*(z+r)) - exp(1i*C);
F(4) = D*exp(1i*x) + E*exp(1i*y) - F*exp(1i*z);

A,B,C,D,E,F are known.
x,y,z,r are unknown.

Torsten

unread,
May 27, 2013, 2:46:10 AM5/27/13
to
"eric" wrote in message <knudc7$8sc$1...@newscl01ah.mathworks.com>...
I assume by "solve" you mean you want to determine x,y,z and r such that
F(i)=0 for i=1,...,4.
Split F(i) into real and imaginary part and set both to zero separately:
Fr(1)=cos(x+r)-cos(A)=0
Fi(1)=sin(x+r)-sin(A)=0
Fr(2)=cos(y+r)-cos(B)=0
Fi(2)=sin(y+r)-sin(B)=0
Fr(3)=cos(z+r)-cos(C)=0
Fi(3)=sin(z+r)-sin(C)=0
Fr(4)=D*cos(x)+E*cos(y)-F*cos(z)=0
Fi(4)=D*sin(x)+E*sin(y)-F*sin(z)=0
So eight equations in four unknowns - seems your system is overdetermined.

Best wishes
Torsten.

Matt J

unread,
May 27, 2013, 10:33:09 AM5/27/13
to
"Torsten" wrote in message <knuvfi$n51$1...@newscl01ah.mathworks.com>...
> I assume by "solve" you mean you want to determine x,y,z and r such that
> F(i)=0 for i=1,...,4.
> Split F(i) into real and imaginary part and set both to zero separately:

Even easier. Add 1 line to split the original 4 equations into real and imaginary parts

F(1) = exp(1i*(x+r)) - exp(1i*A);
F(2) = exp(1i*(y+r)) - exp(1i*B);
F(3) = exp(1i*(z+r)) - exp(1i*C);
F(4) = D*exp(1i*x) + E*exp(1i*y) - F*exp(1i*z);

F=[real(F(:)), imag(F(:))];

eric

unread,
May 28, 2013, 4:35:12 AM5/28/13
to
Thank you Torsten,
I try your method but the solutions for x,y,z and r look weird (like a noisy images since they are 2D images)
It seems like i need something else...
I am trying to limit the "fsolve" algorithm to give only real values at the solution, is there another suggestion?

eric

unread,
May 28, 2013, 4:37:09 AM5/28/13
to
Thank you Matt J,
I also try your method but the solutions for x,y,z and r also look weird (like a noisy images since they are 2D images)
Do you have another suggestion or should I try another fuction like "lsqnonlin"?

eric

unread,
May 28, 2013, 5:02:08 AM5/28/13
to
I also apply this reformulated version the problem trying to not include any imaginary numbers in the set of equations and in fact, i only obtain real values only but still i just obtain a noisy looking images...
F(1)=x+r-A
F(2)=y+r-B
F(3)=z+r-C
F(4)=D*cos(x)+E*cos(y)-F*cos(z);
F(5)=D*sin(x)+E*sin(y)-F*sin(z);

Torsten

unread,
May 28, 2013, 5:39:07 AM5/28/13
to
"eric" wrote in message <ko1rqg$c7q$1...@newscl01ah.mathworks.com>...
I don't understand this reformulation.
What I wanted to say in my first reply: you can't expect that your system of equations has a solution because you have 8 equations to determine only 4 parameters.
In order to _get_ a solution, you must admit x,y,z and r to be _complex_. Then you have 8 equations in 8 unknowns.

Best wishes
Torsten.

Nasser M. Abbasi

unread,
May 28, 2013, 5:44:26 AM5/28/13
to
On 5/28/2013 3:35 AM, eric wrote:

> I am trying to limit the "fsolve" algorithm to give only real values at the solution,
> is there another suggestion?
>

How about getting the complete solution, then just look at the real part of it?

help real()



eric

unread,
May 28, 2013, 9:23:10 AM5/28/13
to
Thank you Nasser,
Taking the real part of the solution is not what i need.
I need an numerical solver that can be limited to give only real values, if i do what you suggest here then, it means i will loose part of the solution since the imaginary part will take part of the solution and i that is the situation i want to avoid; in other words, i don't want to see any imaginary number in the solution output.

eric

unread,
May 28, 2013, 9:35:10 AM5/28/13
to
Hello Torsten, thanks so much again.
I don't want to confuse you, let's consider again the first formulation.
I can not accept the unknowns x,y,z and r to be complex, they are inside an exponential terms multiplied by the imaginary number "i" but as you see they are not complex itselfs.
If I accept 8 unknowns as output considering that I obtain a real and imaginary part then somehow at the end i should make them to be only 4 real uknown variables as they supposed to be but in that case i can not just delete of the imaginary part since i will be deleting part of the solution. Any Idea about this? :)

Torsten

unread,
May 28, 2013, 9:57:07 AM5/28/13
to
"eric" wrote in message <ko2bqe$pr3$1...@newscl01ah.mathworks.com>...
If you want to obtain x,y,z and r real-valued, they can only approximately satisfy your four equations from above.
If this fits your needs, try a solution in the least-squares sense using lsqnonlin by defining the functions f1,...,f8 as I did in my first response.

Best wishes
Torsten.

eric

unread,
May 30, 2013, 4:57:07 AM5/30/13
to
Thank you Torsten,
Still trying your recommendation with lsqnonlin for 8 equations and 4 unknowns but still the weird noisy results appear...

Matt J

unread,
May 30, 2013, 10:10:06 AM5/30/13
to
"eric" wrote in message <ko7493$1ul$1...@newscl01ah.mathworks.com>...
> Thank you Torsten,
> Still trying your recommendation with lsqnonlin for 8 equations and 4 unknowns but still the weird noisy results appear...
=========

Some observations. If I define

ex=exp(1i*x)
ey=exp(1i*y)
ez=exp(1i*z)
er=exp(1i*(-r))

Ka=- exp(1i*A)
Kb=- exp(1i*B)
Kc=- exp(1i*C)

then your equations can be rewritten

0= ex +Ka*er
0= ey +Kb*er
0= ez +Kc*er
0 = D*ex + E*ey - F*ez

this is a linear system of equations in ex, ey, ez, er. If it is a nonsingular system, then inverting it leads to

ex=ey=ez=er=0

which is impossible to satisfy since ex, ey, ez, er all lie on the unit circle in the complex plane. A minimum norm solution would therefore be any x,y,z,r. This may be why you get non-sensical-looking solutions.

eric

unread,
Jun 1, 2013, 3:25:13 AM6/1/13
to
Hello Matt J,
Thank you for your detailed explanation.
I guess you are beyond my skills in math and your observations are convincing so now i am worry about my system of equations that seems to make no sense.
I guess i should reformulate my problem from the begining.
Just for curiosity, have you write any book or publications where i can read your work. I guess I have a lot of things to learn from you.
Thank you so much!

dominik

unread,
Jun 18, 2013, 10:30:10 AM6/18/13
to
I have the same problem
what i did and it worked for me so far is

a) define the function over the real part of x (where x is the unknowns)

function F=myfun(x)
x=real(x)
F=...x...
end

b) in the main script, use only the real part of the solution found

x_solution=fsolve(myfun,guess)
x_solution=real(x_solution)

This way u shouldn't find any imaginary solutions.

Anyway, if anyone knows a better way id be happy to hear.
0 new messages