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!.