Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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

From: eric

Date: 27 May, 2013 01:37:11

Message: 1 of 15

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.

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

From: Torsten

Date: 27 May, 2013 06:46:10

Message: 2 of 15

"eric" wrote in message <knudc7$8sc$1@newscl01ah.mathworks.com>...
> 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.

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.

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

From: Matt J

Date: 27 May, 2013 14:33:09

Message: 3 of 15

"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(:))];

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

From: eric

Date: 28 May, 2013 08:35:12

Message: 4 of 15

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?

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

From: eric

Date: 28 May, 2013 08:37:09

Message: 5 of 15

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"?

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

From: eric

Date: 28 May, 2013 09:02:08

Message: 6 of 15

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);

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

From: Torsten

Date: 28 May, 2013 09:39:07

Message: 7 of 15

"eric" wrote in message <ko1rqg$c7q$1@newscl01ah.mathworks.com>...
> 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);

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.

Subject: limit fsolve to converge only to a real solution (not imaginary

From: Nasser M. Abbasi

Date: 28 May, 2013 09:44:26

Message: 8 of 15

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()

Subject: limit fsolve to converge only to a real solution (not imaginary

From: eric

Date: 28 May, 2013 13:23:10

Message: 9 of 15

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.

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

From: eric

Date: 28 May, 2013 13:35:10

Message: 10 of 15

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? :)

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

From: Torsten

Date: 28 May, 2013 13:57:07

Message: 11 of 15

"eric" wrote in message <ko2bqe$pr3$1@newscl01ah.mathworks.com>...
> 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? :)

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.

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

From: eric

Date: 30 May, 2013 08:57:07

Message: 12 of 15

Thank you Torsten,
Still trying your recommendation with lsqnonlin for 8 equations and 4 unknowns but still the weird noisy results appear...

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

From: Matt J

Date: 30 May, 2013 14:10:06

Message: 13 of 15

"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.

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

From: eric

Date: 1 Jun, 2013 07:25:13

Message: 14 of 15

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!

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

From: dominik

Date: 18 Jun, 2013 14:30:10

Message: 15 of 15

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.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us