How to find zeros of a polynomial and choose values within a specific range?
Show older comments
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)
Walter Roberson
on 13 May 2015
1 vote
1 Comment
Walter Roberson
on 13 May 2015
Edited: Walter Roberson
on 13 May 2015
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.
Andrei Bobrov
on 13 May 2015
v = arrayfun(@(x)reshape(roots([a,b,c,d-x]),1,1,[]),P,'un',0);
out = cell2mat(v);
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!