Can a function in matlab return a polynomial or an expression?

1 回表示 (過去 30 日間)
Sachi
Sachi 2015 年 1 月 9 日
編集済み: Guillaume 2015 年 1 月 9 日
Say I want (3x-3) returned when 3 is given as an input parameter. How can I do this?

採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 1 月 9 日
Hi,
if you are looking for polynomials only: polynomials of form a_n*x^n+ ...+a_1*x+a_0 are represented as vectors [a_n ... a_1 a_0], so in your case simply [3 3].
Take a look at the functions working with polynomials (at the bottom of):
doc polyfun
Titus

その他の回答 (2 件)

Torsten
Torsten 2015 年 1 月 9 日
編集済み: per isakson 2015 年 1 月 9 日
if Input == 3
f=@(x)(3*x-3);
end
Best wishes
Torsten.

Guillaume
Guillaume 2015 年 1 月 9 日
編集済み: Guillaume 2015 年 1 月 9 日
Your question is too open ended to be answered properly. What do you want to do with the return value? What does (3x-3) mean to you? How is it even constructed (do you always want a polynomial of degree 1 with all coefficients equal to the input?). Depending on the answer to these questions
  • You could return a function handle, as in Torsten's answer.
  • You could return a vector of polynomial coefficients to be evaluated with polyval.
  • You could just return the string '3x-3', which you could construct any number of ways (with sprintf or num2str or ...)
  • You could return something else

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by