How to find zeros of a polynomial and choose values within a specific range?

Hi,
I have a third order polynomial function of x. P= a*x^3 +b*x^2 +c*x +d. a,b,c,d are known values. P is a given 200x300 matrix. I am trying to find the corresponding x matrix by using fzero. But this gives me x values in the wrong range. The resulting x matrix is quite random even though I use a suitable initial value for fzero. Is there some way I can restrict the x values to be in a range say for example 1<x<1.8.

Answers (2)

See the documentation and scan down to "Roots starting from an interval"

1 Comment

I suggest you consider using roots() instead.
xC = arrayfun(@(p) roots([a, b, c, d-p]), P, 'uniform', 0);
cellfun(@(v) v(imag(v)==0 & LowX < v & v < HighX), xC, 'Uniform', 0)
This will give a cell array of lists of real-valued solutions in the range. If all of the coefficients are real then there will be either 1 or 3 real-valued solutions but without further information we do not know that the solutions will fall within the desired range, and we do not know that there will not be multiple solutions within the desired range. Each entry in the cell array thus might have 0, 1, 2, or 3 values. You could get the smallest of them by applying min() but you need to deal with the possibility that there might not be any in the range. One possibility would be:
cellfun(@(v) min([NaN, v(imag(v)==0 & LowX < v & v < HighX)]), xC)
This will pick the smallest of the entries if there are any, and the NaN will be returned only if the result would otherwise be empty.

Sign in to comment.

v = arrayfun(@(x)reshape(roots([a,b,c,d-x]),1,1,[]),P,'un',0);
out = cell2mat(v);

Asked:

Alt
on 13 May 2015

Edited:

on 13 May 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!