how i can create a handle function(polynomial) or symbolic function (polynomial) with a uncertain vector?
Afficher commentaires plus anciens
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;
Réponse acceptée
Plus de réponses (2)
John D'Errico
le 10 Août 2016
Modifié(e) : John D'Errico
le 10 Août 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
le 6 Juin 2022
0 votes
Maybe this is what you are looking for? : Create symbolic polynomial from vector of coefficients - MATLAB poly2sym - MathWorks Deutschland
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!