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:
finding complex root from nonlinear equations

Subject: finding complex root from nonlinear equations

From: Mars creature

Date: 3 Jan, 2009 21:21:09

Message: 1 of 16

Hi Matlab users,
  I know fsolve can find the real roots from polynomial equations, but
I don't know how to find complex roots, like x^2+1=0. Also I have a
complicated nonlinear equation with complex coefficients to solve
numerically, anyone give me a hint where to find the instruction?
Thank you very much!
JB wang

Subject: finding complex root from nonlinear equations

From: Roger Stafford

Date: 3 Jan, 2009 22:49:01

Message: 2 of 16

Mars creature <jinbow@gmail.com> wrote in message <046c08c7-878f-4967-bab1-41273a92bcbd@r2g2000vbp.googlegroups.com>...
> Hi Matlab users,
> I know fsolve can find the real roots from polynomial equations, but
> I don't know how to find complex roots, like x^2+1=0. Also I have a
> complicated nonlinear equation with complex coefficients to solve
> numerically, anyone give me a hint where to find the instruction?
> Thank you very much!
> JB wang

  For polynomial equations the matlab function 'roots' will find all roots, complex or real. For general nonlinear equations, Mathworks' advice in their 'fsolve' documentation is "fsolve only handles real variables. When x has complex variables, the variables must be split into real and imaginary parts." Presumably this splitting would also apply to the values in the function F(x) to be zero-ed. Its real and imaginary parts could be brought to zero as separate elements of the F(x) vector. In effect you would be doubling the number of unknowns and the number of equations.

  As to finding all possible roots, 'fsolve' only promises to find one root. However that can be manipulated by selecting differing starting values on different calls to 'fsolve'. That might be easier said than done, though.

Roger Stafford

Subject: finding complex root from nonlinear equations

From: Miroslav Balda

Date: 3 Jan, 2009 23:13:01

Message: 3 of 16

Mars creature <jinbow@gmail.com> wrote in message <046c08c7-878f-4967-bab1-41273a92bcbd@r2g2000vbp.googlegroups.com>...
> Hi Matlab users,
> I know fsolve can find the real roots from polynomial equations, but
> I don't know how to find complex roots, like x^2+1=0. Also I have a
> complicated nonlinear equation with complex coefficients to solve
> numerically, anyone give me a hint where to find the instruction?
> Thank you very much!
> JB wang

Hi,
Root of a complex function y = f(x) is such a complex number x = [real(x); imag(x)], for which real(f(x))=0 and simultaneously imag(f(x))=0. Hence, it may be found by solving a system of two real equations of two real variables:

After defining the function, say by a m-function, say

function y = fx(x)
y = x^3 + 2*x^2 + 3*x + 4; % formula for the function y of the comlex variable x

where x = complex(real(x), imag(x)), the solution can be found by any of functions for solving of nonlinear equations. They may be found in the Optimization Toolbox or in the File Exchange collection. If the function LMFnlsq (from FEX Id 17534) be used, the function describing residuals of those equations should be assembled

function r = res(x)
x = complex(x(1),x(2));
r=[real(fx(x));imag(fx(x))];

The root is obtained by calling

[x,ssq,cnt] = LMFnlsq('res',x0); % see help LMFnlsq
The output is
x =
   -0.1747
    1.5469
ssq =
  1.0343e-023
cnt =
     7

The complex root is

cxroot = complex(x(1),x(2));

Hope, it help.

Mira
         

Subject: finding complex root from nonlinear equations

From: Mars creature

Date: 4 Jan, 2009 16:21:45

Message: 4 of 16

On Jan 3, 5:49 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Mars creature <jin...@gmail.com> wrote in message <046c08c7-878f-4967-bab=
1-41273a92b...@r2g2000vbp.googlegroups.com>...
> > Hi Matlab users,
> > I know fsolve can find the real roots from polynomial equations, but
> > I don't know how to find complex roots, like x^2+1=3D0. Also I have a
> > complicated nonlinear equation with complex coefficients to solve
> > numerically, anyone give me a hint where to find the instruction?
> > Thank you very much!
> > JB wang
>
> For polynomial equations the matlab function 'roots' will find all root=
s, complex or real. For general nonlinear equations, Mathworks' advice in =
their 'fsolve' documentation is "fsolve only handles real variables. When x=
 has complex variables, the variables must be split into real and imaginary=
 parts." Presumably this splitting would also apply to the values in the f=
unction F(x) to be zero-ed. Its real and imaginary parts could be brought =
to zero as separate elements of the F(x) vector. In effect you would be do=
ubling the number of unknowns and the number of equations.
>
> As to finding all possible roots, 'fsolve' only promises to find one ro=
ot. However that can be manipulated by selecting differing starting values=
 on different calls to 'fsolve'. That might be easier said than done, thou=
gh.
>
> Roger Stafford

Thanks Roger,
  This is helpful. I can see that polynomial equations can be split
into two equations containing the real and imag parts. However, it
might be a problem for more complicated nonlinear equations, like
including sin/cos and sqrt of x and even more complicated cases, when
the whole equation is not easy to split. I was wondering if there's
solver like fsolve, and can find the complex roots without splitting
the equation. I googled but can not find very useful information.
Thank you very much!!
JB wang

Subject: finding complex root from nonlinear equations

From: John D'Errico

Date: 4 Jan, 2009 16:33:02

Message: 5 of 16

Mars creature <jinbow@gmail.com> wrote in message <f5f45956-b72f-4637-abec-c6ad168c231c@u14g2000yqg.googlegroups.com>...

> This is helpful. I can see that polynomial equations can be split
> into two equations containing the real and imag parts. However, it
> might be a problem for more complicated nonlinear equations, like
> including sin/cos and sqrt of x and even more complicated cases, when
> the whole equation is not easy to split. I was wondering if there's
> solver like fsolve, and can find the complex roots without splitting
> the equation. I googled but can not find very useful information.

The simple answer is not really. Split the problem
as suggested into independent variables, recognizing
the true 2-d nature of the problem.

John

Subject: finding complex root from nonlinear equations

From: Roger Stafford

Date: 4 Jan, 2009 17:43:02

Message: 6 of 16

Mars creature <jinbow@gmail.com> wrote in message <f5f45956-b72f-4637-abec-c6ad168c231c@u14g2000yqg.googlegroups.com>...
> Thanks Roger,
> This is helpful. I can see that polynomial equations can be split
> into two equations containing the real and imag parts. However, it
> might be a problem for more complicated nonlinear equations, like
> including sin/cos and sqrt of x and even more complicated cases, when
> the whole equation is not easy to split. I was wondering if there's
> solver like fsolve, and can find the complex roots without splitting
> the equation. I googled but can not find very useful information.
> Thank you very much!!
> JB wang

  This "splitting" is not as formidable as you make it out, JB. For example suppose your equation is F(z) = sin(z)^2+sqrt(z)-log(z) = 0 and you seek complex roots z = x + y*i. You need to define the equations

 F1(x,y) = real(sin(x+y*i)^2+sqrt(x+y*i)-log(x+y*i)) = 0
 F2(x,y) = imag(sin(x+y*i)^2+sqrt(x+y*i)-log(x+y*i)) = 0

Matlab is perfectly capable of computing these expressions with complex arguments, so that is all there is to it. Anything matlab can compute can be expressed as a function. (For the purposes of this discussion we ignore the fact that 'sqrt' and 'log' have multiple branches.) You now have two equations to solve with two unknowns. The 'fsolve' function will never know that its equations are derived from complex quantities. Everything it sees is real-valued; x, y, F1(x,y), and F2(x,y) are all real.

  I grant you that it would be more converient if 'fsolve' itself could handle complex quantities. I see no fundamental reason why it couldn't. The above reasoning shows that. Perhaps one day Mathworks will decide to extend it in that manner rather than having the users do it.

  As to splitting polynomials, that is quite unnecessary since the 'roots' function is already designed to handle complex values.

Roger Stafford

Subject: finding complex root from nonlinear equations

From: Steven Lord

Date: 5 Jan, 2009 03:55:54

Message: 7 of 16


"Mars creature" <jinbow@gmail.com> wrote in message
news:f5f45956-b72f-4637-abec-c6ad168c231c@u14g2000yqg.googlegroups.com...
On Jan 3, 5:49 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> Mars creature <jin...@gmail.com> wrote in message
> <046c08c7-878f-4967-bab1-41273a92b...@r2g2000vbp.googlegroups.com>...
> > Hi Matlab users,
> > I know fsolve can find the real roots from polynomial equations, but
> > I don't know how to find complex roots, like x^2+1=0. Also I have a
> > complicated nonlinear equation with complex coefficients to solve
> > numerically, anyone give me a hint where to find the instruction?
> > Thank you very much!
> > JB wang
>
> For polynomial equations the matlab function 'roots' will find all
> roots, complex or real. For general nonlinear equations, Mathworks'
> advice in their 'fsolve' documentation is "fsolve only handles real
> variables. When x has complex variables, the variables must be split into
> real and imaginary parts." Presumably this splitting would also apply to
> the values in the function F(x) to be zero-ed. Its real and imaginary
> parts could be brought to zero as separate elements of the F(x) vector.
> In effect you would be doubling the number of unknowns and the number of
> equations.
>
> As to finding all possible roots, 'fsolve' only promises to find one
> root. However that can be manipulated by selecting differing starting
> values on different calls to 'fsolve'. That might be easier said than
> done, though.
>
> Roger Stafford

Thanks Roger,
  This is helpful. I can see that polynomial equations can be split
into two equations containing the real and imag parts. However, it
might be a problem for more complicated nonlinear equations, like
including sin/cos and sqrt of x and even more complicated cases, when
the whole equation is not easy to split. I was wondering if there's
solver like fsolve, and can find the complex roots without splitting
the equation. I googled but can not find very useful information.
Thank you very much!!
JB wang



Splitting it up isn't that difficult a problem.


x = fsolve(@myfunction7, [1; 1]);
complexSolution = complex(x(1:2:end), x(2:2:end))


where myfunction7 is:


function y = myfunction7(x)
complexX = complex(x(1:2:end), x(2:2:end));
complexY = complexX.^2 + 1; % to solve x.^2 + 1
y = [real(complexY); imag(complexY)];


using an "interleaved" storage for x, the parameter for which FSOLVE solves,
and [real; imaginary] for y, the output from myfunction7, the function to be
solved.

--
Steve Lord
slord@mathworks.com

Subject: finding complex root from nonlinear equations with complex arguments

From: Ramesh Kudenatti

Date: 8 Jan, 2009 16:51:02

Message: 8 of 16

Hi
Could anyone tell me how to find the roots of equation involving the transcendental functions like Airy function and its derivative with complex argument? My function is given below.
AiryAiPrime(Z)-c1 Exp[I Pi/6] ==0 where Z = I W (6 I k)^(-2/3), W is unknown, c1 and k are known.

Requesting..........

Subject: finding complex root from nonlinear equations with complex arguments

From: Venkat raman Thenkarai Narayanan

Date: 20 May, 2009 16:00:21

Message: 9 of 16

Hi,
       I'd like to know how to pass constants from the command line for the "LMFnlsq" function. For example, you've a function y = x^3 + Á*x^2 + c2*x + c3 and i want to pass the values c1, c2 and c3 from the command line rather than specifying in the file itself. I am finding that the function is not flexible in that regard. Can you tell me what i need to do if i want to play around with different constants input from the command line?

Thanks,
Venkat

"Ramesh Kudenatti" <ramesh@bub.ernet.in> wrote in message <gk5atm$16i$1@fred.mathworks.com>...
> Hi
> Could anyone tell me how to find the roots of equation involving the transcendental functions like Airy function and its derivative with complex argument? My function is given below.
> AiryAiPrime(Z)-c1 Exp[I Pi/6] ==0 where Z = I W (6 I k)^(-2/3), W is unknown, c1 and k are known.
>
> Requesting..........
>
>
>

Subject: finding complex root from nonlinear equations with complex arguments

From: Venkat raman Thenkarai Narayanan

Date: 20 May, 2009 16:00:21

Message: 10 of 16

Hi,
       I'd like to know how to pass constants from the command line for the "LMFnlsq" function. For example, you've a function y = x^3 + Á*x^2 + c2*x + c3 and i want to pass the values c1, c2 and c3 from the command line rather than specifying in the file itself. I am finding that the function is not flexible in that regard. Can you tell me what i need to do if i want to play around with different constants input from the command line?

Thanks,
Venkat

"Ramesh Kudenatti" <ramesh@bub.ernet.in> wrote in message <gk5atm$16i$1@fred.mathworks.com>...
> Hi
> Could anyone tell me how to find the roots of equation involving the transcendental functions like Airy function and its derivative with complex argument? My function is given below.
> AiryAiPrime(Z)-c1 Exp[I Pi/6] ==0 where Z = I W (6 I k)^(-2/3), W is unknown, c1 and k are known.
>
> Requesting..........
>
>
>

Subject: finding complex root from nonlinear equations with complex arguments

From: Steven Lord

Date: 20 May, 2009 17:18:33

Message: 11 of 16


"Venkat raman Thenkarai Narayanan" <thavil@yahoo.com> wrote in message
news:gv19el$jnf$1@fred.mathworks.com...
> Hi,
> I'd like to know how to pass constants from the command line for the
> "LMFnlsq" function. For example, you've a function y = x^3 + *x^2 +
> c2*x + c3 and i want to pass the values c1, c2 and c3 from the command
> line rather than specifying in the file itself. I am finding that the
> function is not flexible in that regard. Can you tell me what i need to do
> if i want to play around with different constants input from the command
> line?

After a bit of searching, I found that LMFnlsq refers to this File Exchange
submission:

http://www.mathworks.com/matlabcentral/fileexchange/17534

That looks like it has a fairly standard "function function" style
interface. I'd probably write my M-file something like:


function coeffs = findTheCoefficients(coeffVector)
% Call this like:
% c = findTheCoefficients([2 3 4])
% for the problem y = x^3 + 2*x^2 + 3*x + 4

myObjectiveFunction = @(x) polyval([1 coeffVector], x);
coeffs = LMFnlsq(myObjectiveFunction, ...) % pass in whatever options you
want


POLYVAL accepts a vector of polynomial coefficients and a value and
evaluates the specified polynomial at that value.

--
Steve Lord
slord@mathworks.com

Subject: finding complex root from nonlinear equations

From: Venkat raman Thenkarai Narayanan

Date: 20 May, 2009 17:30:04

Message: 12 of 16

Hi Miroslav,
I'd like to know how to pass constants from the command line for the
"LMFnlsq" function. For example, you've a function y = x^3 + c1*x^2 +
c2*x + c3 and i want to pass the values c1, c2 and c3 from the command
line rather than specifying in the file itself (c1=2, c2=3 and c3=4 in your example). How should i have to change the 'res' function file in this case? Can you tell me what i need to do if i want to play around with different constants input from the command
line?

Thanks,
Venkat

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <gjordt$nsk$1@fred.mathworks.com>...
> Mars creature <jinbow@gmail.com> wrote in message <046c08c7-878f-4967-bab1-41273a92bcbd@r2g2000vbp.googlegroups.com>...
> > Hi Matlab users,
> > I know fsolve can find the real roots from polynomial equations, but
> > I don't know how to find complex roots, like x^2+1=0. Also I have a
> > complicated nonlinear equation with complex coefficients to solve
> > numerically, anyone give me a hint where to find the instruction?
> > Thank you very much!
> > JB wang
>
> Hi,
> Root of a complex function y = f(x) is such a complex number x = [real(x); imag(x)], for which real(f(x))=0 and simultaneously imag(f(x))=0. Hence, it may be found by solving a system of two real equations of two real variables:
>
> After defining the function, say by a m-function, say
>
> function y = fx(x)
> y = x^3 + 2*x^2 + 3*x + 4; % formula for the function y of the comlex variable x
>
> where x = complex(real(x), imag(x)), the solution can be found by any of functions for solving of nonlinear equations. They may be found in the Optimization Toolbox or in the File Exchange collection. If the function LMFnlsq (from FEX Id 17534) be used, the function describing residuals of those equations should be assembled
>
> function r = res(x)
> x = complex(x(1),x(2));
> r=[real(fx(x));imag(fx(x))];
>
> The root is obtained by calling
>
> [x,ssq,cnt] = LMFnlsq('res',x0); % see help LMFnlsq
> The output is
> x =
> -0.1747
> 1.5469
> ssq =
> 1.0343e-023
> cnt =
> 7
>
> The complex root is
>
> cxroot = complex(x(1),x(2));
>
> Hope, it help.
>
> Mira
>
>

Subject: finding complex root from nonlinear equations

From: Venkat raman Thenkarai Narayanan

Date: 20 May, 2009 17:30:05

Message: 13 of 16

Hi Miroslav,
I'd like to know how to pass constants from the command line for the
"LMFnlsq" function. For example, you've a function y = x^3 + c1*x^2 +
c2*x + c3 and i want to pass the values c1, c2 and c3 from the command
line rather than specifying in the file itself (c1=2, c2=3 and c3=4 in your example). How should i have to change the 'res' function file in this case? Can you tell me what i need to do if i want to play around with different constants input from the command
line?

Thanks,
Venkat

"Miroslav Balda" <miroslav.nospam@balda.cz> wrote in message <gjordt$nsk$1@fred.mathworks.com>...
> Mars creature <jinbow@gmail.com> wrote in message <046c08c7-878f-4967-bab1-41273a92bcbd@r2g2000vbp.googlegroups.com>...
> > Hi Matlab users,
> > I know fsolve can find the real roots from polynomial equations, but
> > I don't know how to find complex roots, like x^2+1=0. Also I have a
> > complicated nonlinear equation with complex coefficients to solve
> > numerically, anyone give me a hint where to find the instruction?
> > Thank you very much!
> > JB wang
>
> Hi,
> Root of a complex function y = f(x) is such a complex number x = [real(x); imag(x)], for which real(f(x))=0 and simultaneously imag(f(x))=0. Hence, it may be found by solving a system of two real equations of two real variables:
>
> After defining the function, say by a m-function, say
>
> function y = fx(x)
> y = x^3 + 2*x^2 + 3*x + 4; % formula for the function y of the comlex variable x
>
> where x = complex(real(x), imag(x)), the solution can be found by any of functions for solving of nonlinear equations. They may be found in the Optimization Toolbox or in the File Exchange collection. If the function LMFnlsq (from FEX Id 17534) be used, the function describing residuals of those equations should be assembled
>
> function r = res(x)
> x = complex(x(1),x(2));
> r=[real(fx(x));imag(fx(x))];
>
> The root is obtained by calling
>
> [x,ssq,cnt] = LMFnlsq('res',x0); % see help LMFnlsq
> The output is
> x =
> -0.1747
> 1.5469
> ssq =
> 1.0343e-023
> cnt =
> 7
>
> The complex root is
>
> cxroot = complex(x(1),x(2));
>
> Hope, it help.
>
> Mira
>
>

Subject: finding complex root from nonlinear equations with complex arguments

From: Venkat raman Thenkarai Narayanan

Date: 20 May, 2009 18:40:17

Message: 14 of 16

It didnt work out since LMFnlsq is written a bit differently (example given in the thread). LMFnlsq expects an equation file input ('res' in the example) But, what if the function isnt a polynomial and suppose i had y = c1*log(x) + c2/sinh(x) + c3*cosh(x).. I cant use polyval here.. My question is how would you have to modify the 'res' file in the example given by Miroslav to get c1, c2 and c3 from the command prompt rather than them being defined in the file itself. Kindly let me know.

Regards,
Venkat

"Steven Lord" <slord@mathworks.com> wrote in message <gv1e10$a7t$1@fred.mathworks.com>...
>
> "Venkat raman Thenkarai Narayanan" <thavil@yahoo.com> wrote in message
> news:gv19el$jnf$1@fred.mathworks.com...
> > Hi,
> > I'd like to know how to pass constants from the command line for the
> > "LMFnlsq" function. For example, you've a function y = x^3 + *x^2 +
> > c2*x + c3 and i want to pass the values c1, c2 and c3 from the command
> > line rather than specifying in the file itself. I am finding that the
> > function is not flexible in that regard. Can you tell me what i need to do
> > if i want to play around with different constants input from the command
> > line?
>
> After a bit of searching, I found that LMFnlsq refers to this File Exchange
> submission:
>
> http://www.mathworks.com/matlabcentral/fileexchange/17534
>
> That looks like it has a fairly standard "function function" style
> interface. I'd probably write my M-file something like:
>
>
> function coeffs = findTheCoefficients(coeffVector)
> % Call this like:
> % c = findTheCoefficients([2 3 4])
> % for the problem y = x^3 + 2*x^2 + 3*x + 4
>
> myObjectiveFunction = @(x) polyval([1 coeffVector], x);
> coeffs = LMFnlsq(myObjectiveFunction, ...) % pass in whatever options you
> want
>
>
> POLYVAL accepts a vector of polynomial coefficients and a value and
> evaluates the specified polynomial at that value.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: finding complex root from nonlinear equations with complex arguments

From: Miroslav Balda

Date: 20 May, 2009 19:26:01

Message: 15 of 16

"Venkat raman Thenkarai Narayanan" <thavil@yahoo.com> wrote in message <gv1iqh$84n$1@fred.mathworks.com>...
> It didnt work out since LMFnlsq is written a bit differently (example given in the thread). LMFnlsq expects an equation file input ('res' in the example) But, what if the function isnt a polynomial and suppose i had y = c1*log(x) + c2/sinh(x) + c3*cosh(x).. I cant use polyval here.. My question is how would you have to modify the 'res' file in the example given by Miroslav to get c1, c2 and c3 from the command prompt rather than them being defined in the file itself. Kindly let me know.

SNIP

Hi,
Since the requirement to solve complex root of nonlinear equation had occured, I prepared and submitted the function cxroot in FEX Id 22661. The function uses also function LMFnlsq, however, the function cxroot simplifies its calling. Look at help cxroot. If you have any other question, write me.
Mira

Subject: finding complex root from nonlinear equations

From: Thomas

Date: 15 Jun, 2010 15:10:22

Message: 16 of 16

"Steven Lord" <slord@mathworks.com> wrote in message <gjs0cb$e03$1@fred.mathworks.com>...
>
> Splitting it up isn't that difficult a problem.
>
>
> x = fsolve(@myfunction7, [1; 1]);
> complexSolution = complex(x(1:2:end), x(2:2:end))
>
>
> where myfunction7 is:
>
>
> function y = myfunction7(x)
> complexX = complex(x(1:2:end), x(2:2:end));
> complexY = complexX.^2 + 1; % to solve x.^2 + 1
> y = [real(complexY); imag(complexY)];
>
>
> using an "interleaved" storage for x, the parameter for which FSOLVE solves,
> and [real; imaginary] for y, the output from myfunction7, the function to be
> solved.
>
> --
> Steve Lord
> slord@mathworks.com
>

When I try my example:

x = fsolve(@BetaZeroForRoot, [1.0, 1.0]);
ret = complex(x(1:2:end), x(2:2:end));

with:

function res = BetaZeroForRoot(x)
    n = 1;
    lambda = 1.064; % in mu
    a = 8.5; % in mu
    Ncore = 1.33;
    Nclad = 1.45;

    %%% dependent variables
    k = 2*pi/lambda;
    t2 = a^2 * k^2 * (Ncore^2 - Nclad^2);

    X = complex(x(1:2:end), x(2:2:end));
    Y = BetaZero(X,t2,n,Ncore,Nclad);
    res = [real(Y), imag(Y)];
end

I get the following error message:

"??? Input argument "XDATA" is undefined.

Error in ==> sfdnls at 44
if ~isempty(XDATA)

Error in ==> trustnleqn at 109
  [JACfindiff,numFDfevals] = sfdnls(x,Fvec,JACfindiff,group,[], ...

Error in ==> fsolve at 378
    [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg]=...

Error in ==> test at 21
x = fsolve(@BetaZeroForRoot, [1.0, 1.0]);"
 
What does this mean?

Thomas

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