Evaluating derivative using symbolic toolbox

조회 수: 24 (최근 30일)
WILLIAM WENGER
WILLIAM WENGER 2022년 2월 9일
댓글: WILLIAM WENGER 2022년 2월 11일
Hello all, I am trying to evaluate a derivative using the symbolic tool box.
I chose a simpler exquation to show, but if I can get this to work my code will work.
Here's the issue:
syms x
f_of_x = x^2 * sin(x)
der = diff(f_of_x)
der_2 = der(2)
As you can see I'm trying to evaluate the function @x = 2; but I'm getting the error "index exceeds number of array elements"
I checked the documentation and I literally abandoned my code, and wrote the example code direct from the documentation, still doesn't work.

채택된 답변

Paul
Paul 2022년 2월 10일
Two ways to do this:
syms x
f_of_x = x^2 * sin(x);
der = diff(f_of_x,x) % I like to be explicit on the variable of differentiation
der = 
der_2 = subs(der,x,2)
der_2 = 
Or
f(x) = x^2 * sin(x);
der(x) = diff(f(x),x)
der(x) = 
der_2 = der(2)
der_2 = 
  댓글 수: 2
Paul
Paul 2022년 2월 10일
"Thank you! For the second example does that work because of the parenthesis on f(x)?"
Yes. f_of_x and f(x) are different types of objects:
syms x
f_of_x = x^2 * sin(x);
f(x) = x^2 * sin(x);
whos
Name Size Bytes Class Attributes f 1x1 8 symfun f_of_x 1x1 8 sym x 1x1 8 sym
A symfun works very similarly to a mathemetical function. I think there was a thread here not too long ago that discussed differences between sym and symfun objects, but I can't find it.
WILLIAM WENGER
WILLIAM WENGER 2022년 2월 11일
I’ll look for another thread, or the documentation. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

WILLIAM WENGER
WILLIAM WENGER 2022년 2월 10일
Thank you! For the second example does that work because of the parenthesis on f(x)?

카테고리

Help CenterFile Exchange에서 Symbolic Variables, Expressions, Functions, and Settings에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by