m-file function for solving high order polynomial equation

I want to build for solving polynomial equation for high order (example x^202+x^102+x^52+100) and apply other problem.
I write m-file in this form.
function [eig] = solvepoly(chareq)
eq = 'chareq';
eig = solve(eq);
end
Then I use this function. The answer isn't correct.
>> [eig] = solvepoly(@(x) x^2+2*x+1)
eig =
0
But I use this command in command window. I get this answer.
>> eq = 'x^2+2*x+1'; >> eig = xolve(eq)
eig =
-1 -1
Help me please.

 채택된 답변

Star Strider
Star Strider 2014년 12월 1일
This is easier:
syms x
p = x^202+x^102+x^52+100;
r = roots(sym2poly(p));
The ‘r’ variable will have all 202 roots.

댓글 수: 4

I follow your suggestion in this m-file
function [r] = solvepoly(chareq)
syms x;
p = chareq;
r = roots(sym2poly(p));
end
and try to command window
>> [r] = solvepoly(@(x) x^2+2*x+1)
Undefined function 'sym2poly' for input arguments of type 'function_handle'.
Error in solvepoly (line 5) r = roots(sym2poly(p));
I didn’t use an anonymous function in my code.
Since you did, all you need to do is to cast it as a symbolic function (using the sym function) and my code works just as well.
Try this:
p = @(x) x^202+x^102+x^52+100;
ps = sym(p);
r = roots(sym2poly(ps));
I try to follow your suggestion. It's work.
Thank you very much for advice.
My pleasure!
The most sincere expression of thanks here on MATLAB Answers is to Accept the Answer that most closely solves your problem.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2014년 12월 1일

댓글:

2014년 12월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by