MATLAB Answers

How can I plot a symbolic function or determine a finite and real starting guess for fzero?

4 views (last 30 days)
K on 5 Sep 2012
I have a very long and somewhat complicated complex function that I need to find the roots of. It is a function of "s", which is imaginary, and which I had to define as a symbol in order to work through previous steps and get the function (Functionofs). For those interested in the background math, I am trying to solve the characteristic equation from a determinant of a 4x4 matrix made from a transfer function made from the equations of motion of a beam. When I try starting guesses that I think would make sense using the fzero command, I keep getting the error "Function value at starting guess must be finite and real." I have tried the following:
1) Tried substituting values using subs(Functionofs,s,1*i) to see if I could happen upon something that would work. It just finished (after several minutes) and gave me an extremely long (output truncated) expression with fractions, etc. Does not seem to be of any use. I used simplify(ans) and got it down to a manageable (sort of) size, but it is still a symbol- can I convert this back to a decimal number or expression somehow?
2) Tried to plot Functionofs, but received an error that SYM could not be converted to DOUBLE to plot.
Does anyone have any ideas on what else I could try or how to get around these errors? I have not found a way to convert a symbol to a decimal number- is there one?
Thank you! It's a pretty interesting problem! -Kaitlin


Sign in to comment.

Accepted Answer

Matt Fig
Matt Fig on 5 Sep 2012
Edited: Matt Fig on 5 Sep 2012
If you have a function of one variable, you can use my newtzero to try to find some roots. You will have to convert to a function handle first.
For example:
syms s
f = s.^(2*cos(s)) -s.^3 - sin(s)+1; % Say this is your function.
% Now to find the roots.
G = matlabFunction(f); % Convert to func handle.
newtzero(G) % Can use no or real or imaginary guess.
ans =
0.4192 - 0.7229i
0.4192 + 0.7229i
-0.4221 - 1.2644i
-0.4221 + 1.2644i
-0.2607 - 1.8053i
1.7397 - 1.5257i
-4.5851 - 0.7860i
As far as the plotting goes, just use the function handle returned in G above.


Show 1 older comment
K on 5 Sep 2012
Very interesting- I get a wide variety of solutions, from a few real roots, to TONS of imaginary roots, to: "Empty matrix: 0-by-1". I will go back to a slightly simpler problem and play with it for a while, but this definitely seems to be a step in the right direction! Thank you!
As to the plotting, the error message I get if I try to use plot(G) is "Conversion to double from function_handle is not possible." I'm not sure if I need to define something additionally, but it still seems to be unhappy about the form of the function. Ideas?
K on 5 Sep 2012
Newtzero appears to work very well for most of the programs I was using fzero for, but I need to ask if you have any idea what might return the empty matrix as a solution? I know that the root "s" is representing an imaginary number, so I usually make an imaginary guess (i.e. 90*i). Should that be avoided? Thanks!
Matt Fig
Matt Fig on 6 Sep 2012
NEWTZERO, like all other numerical root finders, has limitations. It uses Newton's method with a vector of initial guesses (to take advantage of MATLAB's vectorization). I wrote it when I was in grad school because I needed to find complex roots and was tired of having to plot a complex function to get the initial guess with FZERO. If you are getting many roots, your function may be periodic or just have many roots. Without seeing the function I can't tell. It is up to you to check that indeed the roots returned are zeros and to interpret their physical meaning.
An empty return just means none of the initial guesses converged. You can play around with the tolerance if you are sure there is a root somewhere that NEWTZERO isn't finding. Any guess that evaluates to a finite, non-nan value is valid.
Good luck!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 5 Sep 2012
Use vpa() to reduce the constant expressions.
Use matlabFunction() to convert a symbolic expression to a function handle.

  1 Comment

K on 5 Sep 2012
Yes, I forgot to add that everything I tried was on the matlabFunction(Functionofs) and it still wasn't working- I will try vpa out too. Thanks!

Sign in to comment.

Sign in to answer this question.