My First Derivative is not correctly calculated in matlab

5 views (last 30 days)
syms yApprox(X1) a0 a1 a2 a3 a4 a5 a6
yApprox = a0 + a1.*X1 + a2.*(X1).^2 + a3.*(X1).^3+ a4.*(X1).^4 + a5.*(X1).^5 + a6.*(X1).^6;
BC1= subs(yApprox,X1,0)
BC2 =subs((diff(yApprox)),X1,0) %The answer is suppose to be a1 but instead of that i got 1

Accepted Answer

Bruno Luong
Bruno Luong on 21 Mar 2024
Edited: Bruno Luong on 21 Mar 2024
syms yApprox(X1) a0 a1 a2 a3 a4 a5 a6
yApprox = a0 + a1.*X1 + a2.*(X1).^2 + a3.*(X1).^3+ a4.*(X1).^4 + a5.*(X1).^5 + a6.*(X1).^6;
BC1= subs(yApprox,X1,0)
BC1 = 
BC2 =subs((diff(yApprox,X1)),X1,0) % pay attention to diff(..)
BC2 = 

More Answers (1)

Manikanta Aditya
Manikanta Aditya on 21 Mar 2024
Hey,
Looks like the issue you are encountering is due to the differentiation operation. When you differentiate 'yApprox' with respect to X1, the coefficient a1 is treated as a constant, and the derivative of X1 with respect to X1 is 1. That’s why you’re getting 1 instead of a1.
syms X1 a0 a1 a2 a3 a4 a5 a6
yApprox = a0 + a1.*X1 + a2.*(X1).^2 + a3.*(X1).^3+ a4.*(X1).^4 + a5.*(X1).^5 + a6.*(X1).^6;
BC1= subs(yApprox,X1,0);
diff_yApprox = diff(yApprox, X1);
BC2 = subs(diff_yApprox, X1, 0);
In this code, 'diff_yApprox' is the derivative of 'yApprox' with respect to X1. When X1=0 is substituted into 'diff_yApprox', the result should be a1 as expected.
Thanks!

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!