how i can create a handle function(polynomial) or symbolic function (polynomial) with a uncertain vector?
Show older comments
for example i give a [1 2 3] or every other vector and i would my code convert that to
f=(x)3*x^2+2*x+1;
Accepted Answer
More Answers (2)
John D'Errico
on 10 Aug 2016
Edited: John D'Errico
on 10 Aug 2016
v = [1 2 3];
As a symbolic polynomial...
syms x
sp = x.^(0:(numel(v)-1))*v.'
sp =
3*x^2 + 2*x + 1
As a function handle that uses polyval:
f = @(x) polyval(flip(v),x);
f(0:5)
ans =
1 6 17 34 57 86
As you can see, it is even vectorized. Note that I had to flip the coefficients vector, since polyval presumes the coefficients start from the highest order term, then go in decreasing order.
Of course, you can just use polyval directly too.
If you want to, it is not even that difficult to avoid use of polyval completely, but I'll stop here, as this does answer your immediate question.
Robert Ukrow
on 6 Jun 2022
0 votes
Maybe this is what you are looking for? : Create symbolic polynomial from vector of coefficients - MATLAB poly2sym - MathWorks Deutschland
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!