how i can create a handle function(polynomial) or symbolic function (polynomial) with a uncertain vector?

9 vues (au cours des 30 derniers jours)
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

xosro
xosro le 10 Août 2016
oh i can do this
if true
f=char(DocPolynom([1 2]));
f1='@(x)';
st=strcat(f1,f);
fh = str2func(st);
fh(1)
ans =
3
end
  2 commentaires
John D'Errico
John D'Errico le 11 Août 2016
Ugh. I would point out that DocPolynom is not actually included in MATLAB by default. Then you use str2func to create a function handle, so sort of equivalent to using eval to generate a function from a string.
John D'Errico
John D'Errico le 11 Août 2016
Modifié(e) : John D'Errico le 11 Août 2016
If you want a good reason to NOT use DocPolynom here:
DocPolynom([1/3 1/6 1/7])
ans =
0.33333*x^2 + 0.16667*x + 0.14286
See that DocPolynom did NOT supply the correct coefficients there.
In fact, ANY string version of the coefficient for most floating point numbers will not in fact be the actual number stored in MATLAB. So, try this:
1/7
ans =
0.142857142857143
0.142857142857143 == (1/7)
ans =
0
7*0.142857142857143 - 1
ans =
8.88178419700125e-16
The point is, when you convert coefficients to strings, you lose precision. When you convert them to strings with 5 significant digits, you may well get total crapola.
coeffs = rand(1,10)
coeffs =
Columns 1 through 5
0.399019969034555 0.0474014620291507 0.34237350316074 0.735966158880164 0.794682157333805
Columns 6 through 10
0.544905898244672 0.686223462984041 0.893632695933602 0.0547917899197324 0.303661379717016
>> polyval(coeffs,1:5)
ans =
4.80265847723748 350.98452785182 9714.48513061997 117343.147533061 839032.11859136
>> f=char(DocPolynom(coeffs));
f1='@(x)';
st=strcat(f1,f);
fh = str2func(st)
fh =
@(x)0.39902*x^9+0.047401*x^8+0.34237*x^7+0.73597*x^6+0.79468*x^5+0.54491*x^4+0.68622*x^3+0.89363*x^2+0.054792*x+0.30366
>> fh(1:5)
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
Error in @(x)0.39902*x^9+0.047401*x^8+0.34237*x^7+0.73597*x^6+0.79468*x^5+0.54491*x^4+0.68622*x^3+0.89363*x^2+0.054792*x+0.30366
Note that the function handle created using the output of DocPolynom is NOT vectorized.
>> fh(1)
ans =
4.802653
>> fh(5)
ans =
839031.780245
Ok, so it looks like fh is getting about 5 significant digits correct there. But I can also give an example of a simple polynomial that will fail by far worse.

Connectez-vous pour commenter.

Plus de réponses (2)

John D'Errico
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
Robert Ukrow le 6 Juin 2022

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by