Problem with numerical solution of non-linear equation


I am trying to solve a complex non-linear equation through the command “fsolve” in OCTAVE but I can’t figure out how the problem should be correctly defined.
In details, I have a function f(p1,…,pn | ZZ) depending on some known real parameters (p1, …, pn) and a complex variable (ZZ). I am looking for an approximated solution of the equation f=0 for several combinations of the parameters.

I guess the command “fsolve” could be the one to be used but I can’t get any meaningful solution. I tried to go through the manual but I didn’t work it out.

eq = @(ZZ) (coth(pi*(ZZ)*l_1/L) + coth(pi*(ZZ)*l_2/L) + pi*(ZZ)/L*M/m*(1+2*xd*(ZZ)/k)/(1+2*xd*(ZZ)/k+((ZZ)/k)^2));
XX = fsolve (eq, 0.0001+0.0001*i, optimset ("ComplexEqn", "on"));

Could you please help me with this?


The forum messed up the syntax. Please, surround code blocks with triple grave accents ```.

fsolve is used for systems of equations. As far as I can see, this function has only one variable.

Are you trying to find where the function crosses the X-axis? For that you should try fzero, although it expects a real-valued objective function.

Are you trying to find the minimum value of the function? For that you might try fminbnd, fminunc, fminsearch. Each of these has some different criteria so you will need to match the minimization function to the properties of your objective function. For example, if you have bounds on where the minimum occurs then fminbnd will be the best because it takes advantage of that extra knowledge.

Hi Rik, thanks for your feedback.

Actually, I am trying to find the roots of the function f(p1,…,pn | ZZ) = 0, that has a single variable only, as you were underlining. The point is that ZZ is a complex number and I don’t know how to deal with this case.
I would appreciate any help you can give.


Roots of a real-valued function are where it crosses the X-axis (and Y == 0). For that, you should use fzero. Try help fzero to see the documentation. In particular, if your function has more than one root than you will want to call fzero repeatedly and give an initial starting point near each different root.

As an example, assume you have the downward pointing parabola y = 4 - x^2. This is trivial to solve analytically and the answer are -2, +2. But, we’ll assume that your function is much harder to understand. The first thing to do is get a rough idea of what it looks like by plotting it.

f = @(x) 4 - x.^2;
x = linspace (-5, 5, 100);
y = f(x);
plot (x,y)

The plot looks like this

Now, I want to identify the left zero. It certainly occurs at a value < 0 so I pick as a starting point -1.

fzero (f, -1)
ans = -2

That was easy. Now I want to locate the right-hand zero. I know that it occurs at a value > 0, and +4 is certainly well to the right of it.

fzero (f, +4)
ans = 2

For fzero to work the objective function needs to be real-valued, that is, the output needs to be a real number. The input can be complex, as long as the output is real.

The use of fzero is pretty clear to me.
The point is that the function I’m dealing with has a complex variable (ZZ) and its output is complex as well, at least in general terms. Then, the real part and the imaginary part of the solution may be used to obtain some other results in physical terms.
So, I am wondering whether there is a function or a strategy in OCTAVE I can follow to obtain the complex root.

Can you define mathematically what you mean by “root” for your problem?

I mean the complex number ZZ* (that is closer to the tentative value) satisfying the equation F(p1,…,pn | ZZ) = 0.

So that sounds like fzero because it attempts to find a solution to f(x) == 0.

For example, take f(z) = z^2 + 1. Analytically, the roots are where z^2 = -1 or z = +/- i.

In Octave,

f = @(z) z.^2 + 1
fzero (f, 2i)
ans =  0 + 1i

Sometimes fzero will fail if the starting point is not close enough to one of the zeros, but if that happens you can get close enough by plotting the function and looking at the graph.