How can we compute the derivative of polynomials?

How can we compute the derivative of the (m-1) polynomials at a point?
I got the following error:
The following error occurred converting from sym to double:
Unable to convert expression into double array.
Error in untitled (line 42)
p(1)=((x-alpha(1))*p0)/beta(1);
syms x
p0=1;
x0=2;
p(1)=((x-.2)*p0)/.3;
p(2)=((x-.5)*p(1)-.3*p0)/1.3;
dp(1)=eval((subs(diff(p(1),x,1),x,x0)) )
dp(2)=eval((subs(diff(p(2),x,1),x,x0)) )
for j=3:m-1
p(j)=((x-.2)*p(j-1) - .3*p(j-2))/1.3;
dp(j)=eval((subs(diff(p(j),x,1),x,x0)));
end

11 Kommentare

James Tursa
James Tursa am 1 Okt. 2020
Bearbeitet: James Tursa am 1 Okt. 2020
What is m? When I define m, I don't get any errors when I run your code in R2020a. What version are you running?
Omar B.
Omar B. am 1 Okt. 2020
m=6 any number.
I don't get any errors in R2017a either.
Omar B.
Omar B. am 2 Okt. 2020
It dose not work.
If I set m=6 then I do not get any error in R2020a.
You should never eval() a symbolic expression.
eval() of a symbolic expression is equivalent to eval(char()) of the expression. However, char() of a symbolic expression is not generally a MATLAB expression.
>> char(repmat(sym('x'),[2 2 2]))
ans =
'array(1..2, 1..2, 1..2, (1, 1, 1) = x, (1, 1, 2) = x, (1, 2, 1) = x, (1, 2, 2) = x, (2, 1, 1) = x, (2, 1, 2) = x, (2, 2, 1) = x, (2, 2, 2) = x)'
char() of a symbolic expression is also not generally a MuPAD expression.
In context, the subs() are returning symbolic rational numbers, and those only need to be double() not eval() .
Omar B.
Omar B. am 2 Okt. 2020
what should I use insted of eval() ?
James Tursa
James Tursa am 2 Okt. 2020
Bearbeitet: James Tursa am 2 Okt. 2020
double( )
That being said, I don't know what is going on in your actual case since your example code doesn't produce an error at my end.
Omar B.
Omar B. am 2 Okt. 2020
do you mean that we need to use double() insted of eval() with a symbolic expression?
Both eval( ) and double( ) work for me with your example code. What is your actual code?
Omar B.
Omar B. am 2 Okt. 2020
yes, I got the same results. Thank you so much

Antworten (1)

David Hill
David Hill am 1 Okt. 2020
Why not just use polynomial representation
p = [3 0 -2 0 1 5];
pd=polyder(p);
x=1:10;
val=polyval(pd,x);

2 Kommentare

Omar B.
Omar B. am 1 Okt. 2020
I just here wrote an example , but I need to refer the coefficients to another code. Is there another way?
The coedfficients would be in pd

Diese Frage ist geschlossen.

Gefragt:

am 1 Okt. 2020

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by