how i can create a handle function(polynomial) or symbolic function (polynomial) with a uncertain vector?
Ältere Kommentare anzeigen
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;
Akzeptierte Antwort
Weitere Antworten (2)
John D'Errico
am 10 Aug. 2016
Bearbeitet: John D'Errico
am 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
am 6 Jun. 2022
0 Stimmen
Maybe this is what you are looking for? : Create symbolic polynomial from vector of coefficients - MATLAB poly2sym - MathWorks Deutschland
Kategorien
Mehr zu Common Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!