how to create a symbolic derivative of f(r(x,y),theta(x,y)) and evaluate it

3 views (last 30 days)
Marko
Marko on 29 Jan 2021
Commented: Marko on 30 Jan 2021
Hello Community,
i have some more or less complicated funtions, e.g. a simple one:
f(r,theta) = r*(pi*cos(theta)+2*sin(theta));
x = x0 +r*cos(theta);
y = y0 +r*cos(theta);
and I need the symbolic derivative of f with respect to x any y: d f(r(x,y),theta(x,y)) / dx = ....
my code look like this:
syms f f1 f2 r theta x x0 y0 y pi
f = r*(pi*cos(theta)+2*sin(theta));
f1 = x == x0 + r*cos(theta);
f2 = y == y0 + r*sin(theta);
dr_dx = diff(solve(f1,r),x)
dtheta_x = diff(solve(f1,theta),x)
dr_dy = diff(solve(f2,r),y)
dtheta_y = diff(solve(f2,theta),y)
df_r = diff(f,r)
df_theta = diff(f,theta)
but how i could create the desired derivative..

Accepted Answer

David Durschlag
David Durschlag on 29 Jan 2021
Let's break down the operations required:
First, solve for theta and r in terms of x and y:
theta_r = solve([x == x0 + r*cos(theta), y == y0 + r*sin(theta)], [theta, r]);
theta_r will be a struct with possible substitutions for theta and r as its properties.
Second, choose the substitutions (in this case, the first ones):
theta_r_subs = [theta_r.theta(1), theta_r.r(1)];
Third, perform the substitution:
fxy = subs(f, [theta, r], theta_r_subs);
Fourth, differentiate:
df_x = diff(fxy, x);
df_y = diff(fxy, y);
Further solutions can be obtained by choosing different substitutions.
--David

More Answers (0)

Community Treasure Hunt

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

Start Hunting!