MATLAB: How to find the roots of large number of polynomials arranged in matrix array

MATLABpolynomial rootspolynomials in array matrixroots in array

%suppose that
[x1,x2,x3,x4,x5]=meshgrid(linspace(0,1,101)) ; % i have computer with 500 GB RAM
%and we have the following polynomial
P=a*x^4+b*x^3+c*x^2+d*x+e ;
% where a,b,c,d,and e depend on x1,x2,x3,x4,and x5.
% i need to find the roots for all the polynomial when x1,x2,x3,x4,and x5 varied in the region 0 to 1
% the problem is that the "roots()" function does not work on the array matrices and if i use the "for loop" the simulation will take more than 7 days
% any help and suggestion will be approciated

Best Answer

Hi,
you could use cellfun to apply roots to your Matrix:
% Matrix containing 3 polynomials of order 4
A = [1 1 -2 2 -3; 2 3 -2 -2 1; 0 0 -4 2 -7]
% Convert to cell array of single polynomials
B =mat2cell(A,ones(1,size(A,1)),size(A,2))
% calculate roots for every polynomial and save in new cell array
C = cellfun(@roots,B,'UniformOutput',0)
% Access the roots of the first polynomial
C{1,:}
Best regards
Stephan