Write a script that calculates the ​​user produced polynomial. The polynomial can be input until the user decides to terminate the program.

조회 수: 2 (최근 30일)
Hello,
I've been given an asignment in school and I'm a bit stuck with this task in the title.
Can anyone help me write the script because I don't know how to create a polynomial that is user decided and loop it at the same time?
Thanks in adwance.
  댓글 수: 2
Matt J
Matt J 2020년 1월 29일
편집: Matt J 2020년 1월 29일
Once you've written something, we can help fix things. As a hint, the following would be one way to read in a vector of polynomial coefficients, e.g,
>> p = input('\nEnter polynomial coefficients: ')
Enter polynomial coefficients: [1,2,1]
p =
1 2 1
Ivan Roko Cavka
Ivan Roko Cavka 2020년 1월 29일
Thanks for the tip Matt, but I don't understand how can I make an indipendant equasion based on the users input. For e.g.
p = input('\nEnter polynomial coefficients: ')
x = input ('\nEnter the x coefficient; ')
y = p*x
If I type it like this, the x multiplies each of the matrix components, but I want the input to be read as a polynomial like e.g. x^3 + x^2 - 1 , or any other variation based on the users input. And I guess I should be using a while loop with all this done?
I only know a way to do it if I know how many charachters will the user put in, and it's not very efficient.
Hope you'll help me get this thing done, cuz it's driving me crazy ;)

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

채택된 답변

Matt J
Matt J 2020년 1월 29일
편집: Matt J 2020년 1월 29일
If you have the Symbolic Math Toolbox and wish to manipulate the polynomials in symbolic form, you can use poly2sym,
>> p = input('\nEnter polynomial coefficients: ')
Enter polynomial coefficients: [1,1,0,-1]
p =
1 1 0 -1
>> P=poly2sym(p)
P =
x^3 + x^2 - 1
However, that's often unnecessary. There are many Matlab commands that can analyze polynomials given only a vector representing their coefficients, e.g.,
>> roots(p) %roots of the polynomial
ans =
-0.8774 + 0.7449i
-0.8774 - 0.7449i
0.7549 + 0.0000i

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by