MATLAB: Multiplying a polynomial with a vector.

multiplicationstr2funcvectorvectorize

Hi
Is there any way i can transform 2*x^4+3x-4 into 2.*x.^4+3.*x-4 in a script? Without typing it in manually.
Thanks!

Best Answer

Include all the operators, and use the vectorize function:
y = str2func(['@(x)' vectorize('2*x^4+3*x-4')]) % Vectorize & (Optionally) Create Anonymous Function
producing:
y =
function_handle with value:
@(x)2.*x.^4+3.*x-4
The documentation states that vectorize is not recommended, however gives no alternative function or approach. I have no idea what the reason is for this, since vectorize can be useful for creating string expressions as anonymous functions.