how i can create a handle function(polynomial) or symbolic function (polynomial) with a uncertain vector?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
xosro
el 10 de Ag. de 2016
Respondida: Robert Ukrow
el 6 de Jun. de 2022
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;
0 comentarios
Respuesta aceptada
xosro
el 10 de Ag. de 2016
2 comentarios
John D'Errico
el 11 de Ag. de 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
el 11 de Ag. de 2016
Editada: John D'Errico
el 11 de Ag. de 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.
Más respuestas (2)
John D'Errico
el 10 de Ag. de 2016
Editada: John D'Errico
el 10 de Ag. de 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.
0 comentarios
Robert Ukrow
el 6 de Jun. de 2022
Maybe this is what you are looking for? : Create symbolic polynomial from vector of coefficients - MATLAB poly2sym - MathWorks Deutschland
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!