Find the exponent of a variable in a symbolic expression

I have a symbolic expression lets say:
e=sym('g*e/w*N');
How can i find the exponent of N (in this case it's 1). Exponent can vary from -10 to 0 to 10.
thanks in advance

 Accepted Answer

Please be cautious about using the same name ('e') for the expression variable and inside the expression. You will run into problems if you use subs().
Anyhow, please see coeff()

2 Comments

Thanks for the coeff() tip. It led me to the solution.
Just in case, maybe anyone will be interested in the future:
expression = sym('t*r*g/N/r');
variable = sym('N');
[num den] = numden(expression);
[~, b]=coeffs(num,variable);
[~, d]=coeffs(den,variable);
temp = [simple(evalin(symengine,['log(' char(b) ')/log(' char(variable) ')']));
simple(evalin(symengine,['log(' char(d) ')/log(' char(variable) ')']))];
r = temp(1) - temp(2);
'r' equals the exponent value of variable 'N'
I believe you should be able to skip the evalin's:
temp = simplify(log(b) / log(variable));
temp2 = simplify(log(d) / log(variable));
I noticed that you repeat r in your expression in such a way that the r cancel out. Was that intended?
Your formula will fail (partly because your requirements are not well defined) if either the numerator or denominator (or both) are polynomials in the variable.
My reference to coeff() rather than coeffs() was correct: see http://www.mathworks.com/help/toolbox/mupad/stdlib/coeff.html
But even easier would be to use
expression = sym('t*r*g/N/r');
variable = sym('N');
r = double(feval(symengine,'degree', numer(expression), N) - feval(symengine, 'degree', denom(expression), N));
You _might_ even be able to use
expression = sym('t*r*g/N/r');
variable = sym('N');
r = double(feval(symengine,'degree', expression, N));

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!