Partial Derivatives Chain Rule

4 次查看(过去 30 天)
Sstuti Mehra
Sstuti Mehra 2020-12-4
回答: Adarsh 2025-1-27
Why isn't my subs function working in this code? Variables x and y have to be substituted. It gives the final output with x and y
Code
syms f x y a b r theta g h f1 f2 final fi x1 y1
f= (x-y)/(x+y)
a= simplify(diff(f,x))
b= simplify(diff(f,y))
x= r*exp(r)*cos(3*theta)
y= r*exp(r)*sin(3*theta)
a1= subs(a,{x,y},{r*exp(r)*cos(3*theta),r*exp(r)*sin(3*theta)})
b1= subs(b,{x,y},{r*exp(r)*cos(3*theta),r*exp(r)*sin(3*theta)})
g= simplify(diff(x,r))
h= simplify(diff(y,r))
f1= a1*g
f2= b1*h
fi= f1 + f2

回答(1 个)

Adarsh
Adarsh 2025-1-27
Based on the code given I understand that you are trying to replace the x& y symbols with their respective polar coordinates in the expressions a & b prior to differentiation, using the “subs” function.
The issue of x & y variables not properly being substituted lies in the usage of the “subs” function, when you declare a symbolic expression for example,
syms f x y a b r theta
f = (x-y)/(x+y)
and then use assignment operator “=” to update the value of x to polar form as shown,
x = r*exp(r)*cos(3*theta)
the value of x is substituted in the workspace, however the occurrences of x in the expressions defined previously will be unaffected by this operation, so the “subs” function is used in the following way,
a1 = subs(a)
this updates the values of symbols in expression a, which are assigned values previously outside this expression.
An alternate workaround would be using cell array of characters instead of a cell array of symbols in the 'old variables' argument of “subs” function, here is a demonstration of using cell array of characters,
a1 = subs(a,{x,y},{ r*exp(r)*cos(3*theta), r*exp(r)*sin(3*theta)})
This change in behaviour of function is accounted to the replaced value of symbol x in the workspace after using the assignment operator “=” to update the value x, since that point the value of x obtains the value to which it is assigned.
So when x is passed as a symbol to the argument it takes the assigned value, hence the inconsistencies occur as described.
Here is the link to the MATLAB documentation section regarding the usage of “subs”:
I hope it helps!.

Community Treasure Hunt

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

Start Hunting!

Translated by