Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

How can i use polyval on the input polynomial question?

1 vue (au cours des 30 derniers jours)
gdwn mmbrv
gdwn mmbrv le 28 Oct 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
prompt={'Input Equation:','Value of the variable:'};
dgtitle='Evaluate at a Given Value';
evans=inputdlg(prompt,dgtitle);
eq=str2sym(evans{1});
val=str2double(evans{2});
cf=coeffs(eq);
c=fliplr(cf);
k=polyval(str2double(c),val);
display(k)
k =
NaN
My program should ask for equation and the value of the variable. What did i do wrong?

Réponses (1)

David Hill
David Hill le 28 Oct 2020
Why not just enter the polynomial coefficents instead of an equation?
p=input('Polynomial coefficents, (like [1 2 3 4]):');
val=input('Evaluate at this value');
k=polyval(p,val);
display(k)
  2 commentaires
gdwn mmbrv
gdwn mmbrv le 28 Oct 2020
teacher required to put polynomials. we're supposed to turn polynomials to coefficients then do the polyval.
David Hill
David Hill le 28 Oct 2020
Use regexp() function
y='5x^3+2x^2+3x+5';%requires 1 in front of x's
p=str2double(regexp(y,'(?<!\^)\d*','match'));

Community Treasure Hunt

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

Start Hunting!

Translated by