Why do I receive different answers from the derivative with and without using chain rule?

Hi, I'm new to MATLAB. I am trying to get the derivative of exp(-i*phi) using syms but I receive different answers after using the chain rule. I use vpa(a1(1,1)) vpa(a2(1,1)) to check.
syms x y
f(x,y) = exp(-1i*x/sqrt(3))*(1+2*exp(1i*sqrt(3)*x/2)*cos(y/2));
phi(x,y) = angle(f);
a1(x,y) = diff(exp(-1i*phi),y);
a2(x,y) = -1i*exp(-1i*phi)*diff(phi,y);

 Accepted Answer

I think possibly you have to express it in terms of more fundamental functions,
syms x y
f(x,y) = exp(-1i*x/sqrt(3))*(1+2*exp(1i*sqrt(3)*x/2)*cos(y/2));
phi(x,y) = atan(imag(f(x,y))/real(f(x,y)));
a1(x,y) = diff(exp(-1i*phi),y);
a2(x,y) = -1i*exp(-1i*phi)*diff(phi,y);
double(a1(1,1))
ans = -0.0011 + 0.0575i
double(a2(1,1))
ans = -0.0011 + 0.0575i

5 Comments

I'm still not sure if I understand what's happening. At least it doesn't seem to be producing consistent answers, even if x and y are restricted to be real (which wasn't a constraint in the original question).
syms x y real
f(x,y) = exp(-1i*x/sqrt(3))*(1+2*exp(1i*sqrt(3)*x/2)*cos(y/2));
phi0(x,y) = angle(f);
phi1(x,y) = atan(imag(f(x,y))/real(f(x,y)));
phi2(x,y) = atan2(imag(f(x,y)),real(f(x,y)));
figure;fsurf(abs(phi0-phi1));
figure;fsurf(abs(phi2-phi1));
It looks like all three formulations of phi are the same inside a disk of radius approximately 4.
In fact, Matlab knows that phi and phi2 are identical.
isAlways(phi0 == phi2)
ans = logical
1
Now compute the partials of each phi both ways
a10(x,y) = diff(exp(-1i*phi0),y);
a20(x,y) = -1i*exp(-1i*phi0)*diff(phi0,y);
a11(x,y) = diff(exp(-1i*phi1),y);
a21(x,y) = -1i*exp(-1i*phi1)*diff(phi1,y);
a12(x,y) = diff(exp(-1i*phi2),y);
a22(x,y) = -1i*exp(-1i*phi2)*diff(phi2,y);
vpa([a10(1,1) a20(1,1) a11(1,1) a21(1,1) a12(1,1) a22(1,1)]).'
ans = 
Maybe there is something subtle about differentating a function of atan2? But the fact that a10 and a12 are so different from each other is troubling, given that phi0 and phi2 are identical.
I think it has something to do with what we see in the following example. The two expressions should be equivalent, but when expressed in terms of complex exponentials, angle() does not know how to simplify down to a real-valued expression.
syms x real
angle(cos(x)+1i*sin(x))
ans = 
angle(exp(1i*x))
ans = 
But Matlab knows they are equivalent:
syms x real
f(x) = cos(x) + 1i*sin(x);
g(x) = exp(1i*x);
angle(f(x))
ans = 
angle(g(x))
ans = 
isAlways(angle(f(x))==angle(g(x)))
ans = logical
1
And both have the same derivative
simplify(diff(angle(f(x))))
ans = 
1
simplify(diff(angle(g(x))))
ans = 
1
Thank you, the atan() one seems to produce the correct result. I forgot to state that x,y are real in my problem.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!