If I have the following symbolic function:
syms f(x,y)
f(x,y) = 2*x^2 + y;
When I try to diff. this function w.r.t. (x) I get:
d = diff(f,x)
d(x, y) =
4*x
Which is a function in both x, and y NOT in (x) only.
How can I perform this and get the real exsisting function input only "i.e. (x) only here"?

 채택된 답변

Star Strider
Star Strider 2021년 4월 9일

0 개 추천

That is the corrct result, since diff is taking the partial derivative with respect to ‘x’ only, and ‘y’ is considered a constant.
To get the derivatives of ‘f’ with respect to both variables, use the jacobian function:
jf = jacobian(f)
producing:
jf(x, y) =
[4*x, 1]
See Differentiation for more information.
Although not appropriate here, explore the functionalDerivative function.

댓글 수: 4

Ammar Taha
Ammar Taha 2021년 4월 9일
First of all thanks for your time. Second, I know that this is a correct answer. What I meant is that for the derivatve function instead of having it as a function of (x), and (y) I need it to be a function of the variable I diff. w.r.t. only, so that when I want to evaluate that derivative function I have to plug in (x) only "rather (x) and 0 for (y)".
My pleasure!
Note that ‘d’ is a funciton of both ‘x’ and ‘y’, even though ‘y’ does not exist in it:
d(x, y) =
4*x
The result is that:
v = d(2,3)
produces:
v =
8
and no errors, while:
v = d(2)
produces:
Error using symfun/subsref (line 189)
Invalid argument at position 2. Symbolic function expected 2 input arguments but received 1.
However if you define it as:
d(x) = diff(f,x)
then:
v = d(2)
produces the expected result and no errors.
Ammar Taha
Ammar Taha 2021년 4월 9일
Ok, thats great. Thnaks a lot.
Star Strider
Star Strider 2021년 4월 9일
As always, my pleasure!

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

추가 답변 (1개)

Chendi Lin
Chendi Lin 2021년 4월 9일

1 개 추천

Hi Ammar,
Without explicitly defining the differentiation variable, "diff" uses the default variable, which is "x" in your case.
To get both derivatives, you can do
[diff(f(x,y),x) diff(f(x,y),y)]
And this should give you the correct result.
Thanks.
CD

카테고리

제품

태그

질문:

2021년 4월 9일

댓글:

2021년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by