How do I use a vector to evaluate a polynomial function?

1 回表示 (過去 30 日間)
Josh
Josh 2013 年 2 月 16 日
I have a vector of x values that I need to plug into a cubic polynomial. And I keep getting errors
how exactly do I do this? The function is a polynomial and I keep getting errors
??? Undefined function or variable 'y'.
Error in ==> at 21 f(x)=y
OR
??? Error using ==> mpower Inputs must be a scalar and a square matrix.
Error in ==> @(x)x^3-2*x^2-3*x+1
Error in ==> at 21 y=feval(f,x) >>

回答 (1 件)

Shashank Prasanna
Shashank Prasanna 2013 年 2 月 16 日
You haven't show us how you are calling this function, that would give this question more clarity. In any case if you are calling the function with a vector input you will have to use the "." (dot) notation as below
x = rand(10,1); % some random numbers
f = @(x)x.^2 - 2*x.^2-3*x+1
y = f(x);
  1 件のコメント
Shashank Prasanna
Shashank Prasanna 2013 年 2 月 16 日
Josh, please share the exact implementation of your function, it will be easier to assist you if the above doesn't work.

サインインしてコメントする。

カテゴリ

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