File Exchange

image thumbnail


version (9.93 KB) by Ben Petschel
manipulate and solve systems of multivariate polynomial equations by computing the groebner basis


Updated 02 Nov 2010

View Version History

View License

Example: simplify the system of equations
{x^2+2xy^2=0, xy+2y^3=1}

>> groebner({'x^2+2*x*y^2','x*y+2*y^3-1'},'lex',{'x','y'})

returns {'y^3-0.5','x'}

Solve equations:

>> polynsolve({'x^2+2*x*y^2','x*y+2*y^3-1'},'',{'x','y'})

returns [0, -0.3969 + 0.6874i; 0, -0.3969 - 0.6874i; 0, 0.7937]

>> polynsolve({'x^2-x','x*y-1'},'',{'x','y'})

returns [1, 1]

>> polynsolve({'x^2-x','x*y'},'',{'x','y'})

returns [0, NaN; 1, 0]

(infinite possibilities represented by NaN)

NOTE: calculation of Groebner bases in floating-point arithmetic can be numerically unstable. See the help text for more details.

Cite As

Ben Petschel (2021). groebner (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

培林 王

Ben Petschel


Aytan kamienny

Does this implementation support finite fields like gf(2)?

This is an excellent work.
I need to solve various systems of third-degree multivariate polynomial equations. Could you please specify the differences of this package with the builtin MatLab function
groebner::gbasis() since they do not provide the same results.


excellent work! thanks.I've found this kind of thing for a longtime.wish I can understand it.

Rongfu Tang

excellent work! thanks.

Ben Petschel

Christophe, you're welcome to try. I'm not enthusiastic about continuing to work on it when more appropriate tools are readily available and more pressing matters are at hand.

If you must, try adding "deg=double(deg);" to line 391 of groebner.m. However then the functions str2poly and poly2str currently don't parse fractions directly so you could try something like the following example:

eqnfr = cellfun(@(x)fr(vpi(x)),str2poly(eqns,vars),'unif',false);
gbfr = groebner(eqnfr,ord,vars);
gbs = poly2str(cellfun(@double,gbfr,'unif',false))
% returns gbs = {'x2^3-0.5','x1'}

For this to work you'll need both the Fractions and VPI toolboxes and to have fixed line 391 of groebner.m. It should work with other integer-coefficient polynomials (i.e. multiply the fractions by a big integer) but no guarantees.

Good luck with it!

Christophe Lauwerys

Thanks for the quick fix Ben.

Regarding floating point accuracy, could a solution be to work with rational coefficients using your fractions toolbox?


Ben Petschel

Christophe, thanks for pointing that out. It's one of several bugs that were introduced with the change in data structures in the last update. I've submitted a new version but there still remains the problem with floating-point accuracy for which I don't know a simple solution.

Christophe Lauwerys

Ben, thanks for your answer.

Could you have a look at another feature/bug:

P = str2poly({'x1*x2*x3'});


ans =

1 1 1 1

??? Index exceeds matrix dimensions.

Error in ==> poly2str at 68
str = [str,varnames{k}]; % print variable name



Ben Petschel

Hi Christophe, thanks for that example, it highlights several issues with the current implementation. Firstly, lex tends to produce longer bases than grlex and the code's reduction algorithms are not particularly efficient. Secondly, groebner base computation is tricky in floating point and while the code does allow a tolerance parameter its checking mechanism is far from perfect. So you'll find that the example does eventually return but with the wrong answer, even with tol set to non-zero. I plan to include more documentation on the limitations in the next update and strongly recommend comparing the results with other programs.

Regarding the use of syms, if you can get it to work that's good, though it's not clear to me how assumptions could be treated robustly even for simple examples like {'a*x'} assuming a<1.

Christophe Lauwerys

Dear Ben, seems a little trick, overloading the LE operator for the SYM class, makes your code available for syms quite straightforwardly after all, at least a previous version of your code which did not use SORTROWS yet.

An unrelated remark:

how come




in less then a second, while


does not return at all ?

Ben Petschel

Hi Christophe, thanks for pointing that out, looks like the case with non-numeric coefficients is more complicated than I first thought. I don't know of a simple way to handle the symbolic assumptions BUT if you have the symbolic toolbox it's worth looking at the groebner package that comes with MuPAD (see


Christophe Lauwerys

The first problem I encounter trying to use symbolic expressions is on line 305 in groebner.m

if all(abs(P(:))<=tol)

where P is a symbolic array.

??? Undefined function or method 'le'
for input arguments of type 'sym'.

Not sure how to get around this if possible at all.

Symbolic variables can be anything or can be defined under certain assumptions (see

I guess the LE operator for symbolic variables should check these assumptions in order to come up with an answer.

Ben Petschel

Thanks John, both are good suggestions. Until I can provide an update, a workaround for complex coefficients is to input the coefficients directly, e.g. your example (1+3*j)*x^2*y+(2+4*j)*x*y has coefficient array [1+3*j,2,1; 2+4*j,1,1]. Let me know if there are any problems.


like (1+3*j)*x^2*y+(2+4*j)*x*y+.....


It will be a very useful one!
It will be much better if you modify the algorithm for handling 'brackets' in str2poly.m.
That is also my desire for finding roots of polynomials with complex number coefficients.
Would you modify the algorithm for handling those kind of polynomials?

Ben Petschel

There's no reason the polynomial coefficients can't be symbolic variables, or any objects for which basic arithmetic is defined. Just create the coefficient arrays directly instead of inputting the polynomials as strings. I don't have the symbolic toolbox so let me know how it goes.

Also I've submitted an updated version that uses a more efficient array representation.

Christophe Lauwerys

Would it be very difficult to include the possibility of calculating the Groebner basis for a system of parameterized mutivariable polynomials? Singular ( is able to do this, so I assume the algorithms are well know.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!