Simplify outputs huge result for diff symbolic expression with hyperbolic functions

Matlab R2019 Update 3
syms x y k real;
syms f(x,y);
syms a b;
xd = -a*tanh(k*f)+b*sech(k*f);
yd = -b*tanh(k*f)-a*sech(k*f);
chid = atan2(yd, xd);
d_chid_dx = diff(chid, x);
simplify(d_chid_dx, 'Steps', 100, 'Criterion', 'preferReal')
produces result
ans(x, y) =
(((real(b*k*diff(f(x, y), x)*(tanh(k*f(x, y))^2 - 1)) - imag((b*k*sinh(k*f(x, y))*diff(f(x, y), x))/cosh(k*f(x, y))^2) + real((a*k*sinh(k*f(x, y))*diff(f(x, y), x))/cosh(k*f(x, y))^2) + imag(a*k*diff(f(x, y), x)*(tanh(k*f(x, y))^2 - 1)))/(imag(b*tanh(k*f(x, y))) - real(a*tanh(k*f(x, y))) + imag(a/cosh(k*f(x, y))) + real(b/cosh(k*f(x, y)))) - ((imag((a*k*sinh(k*f(x, y))*diff(f(x, y), x))/cosh(k*f(x, y))^2) - real(a*k*diff(f(x, y), x)*(tanh(k*f(x, y))^2 - 1)) + real((b*k*sinh(k*f(x, y))*diff(f(x, y), x))/cosh(k*f(x, y))^2) + imag(b*k*diff(f(x, y), x)*(tanh(k*f(x, y))^2 - 1)))*(imag(a*tanh(k*f(x, y))) + real(b*tanh(k*f(x, y))) - imag(b/cosh(k*f(x, y))) + real(a/cosh(k*f(x, y)))))/(imag(b*tanh(k*f(x, y))) - real(a*tanh(k*f(x, y))) + imag(a/cosh(k*f(x, y))) + real(b/cosh(k*f(x, y))))^2)*(imag(b*tanh(k*f(x, y))) - real(a*tanh(k*f(x, y))) + imag(a/cosh(k*f(x, y))) + real(b/cosh(k*f(x, y))))^2)/((imag(a*tanh(k*f(x, y))) + real(b*tanh(k*f(x, y))) - imag(b/cosh(k*f(x, y))) + real(a/cosh(k*f(x, y))))^2 + (imag(b*tanh(k*f(x, y))) - real(a*tanh(k*f(x, y))) + imag(a/cosh(k*f(x, y))) + real(b/cosh(k*f(x, y))))^2)
while calculation by hand gives
-a*k*sech(k*f(x,y))
which is correct.
I tried options for simplification (rewrite, steps, criterion) but with no luck.
Any answer will be greatly appreciated.

 Accepted Answer

Are you sure about your hand-calculated answer? Let's substitute one simple f(x, y) function into both the answer from Symbolic Math Toolbox and your hand-calculated answer and compare the results. I've run the code you wrote above before running any of these commands.
answer2 = -a*k*sech(k*f(x, y));
subs(yd, f, 0) % yd with f(x, y) = 0
subs(xd, f, 0) % ditto for xd
The values of xd and yd with this substitution are constants. Therefore so is chid, it has no dependency on x or y. The derivative of a constant should be 0.
subs(d_chid_dx, f, 0) % It is 0
What happens when we substitute f(x, y) = 0 into your answer?
subs(answer2, f, 0) % -a*k
As other checks, substituing f(x, y) = x into both d_chid_dx and answer2 gives answers that differ by a factor of a.
>> subs(answer2, f(x, y), x)
ans =
-(a*k)/cosh(k*x)
>> simplify(subs(d_chid_dx, f(x, y), x))
ans(x, y) =
-k/cosh(k*x)
Substituting f(x, y) = y also gives different answers.
>> subs(answer2, f(x, y), y)
ans =
-(a*k)/cosh(k*y)
>> simplify(subs(d_chid_dx, f(x, y), y))
ans(x, y) =
0
For this last check, you're taking the derivative with respect to x of a function that doesn't include x at all so it seems reasonable that the result ought to be 0.

2 Comments

Using the information from the follow-up, I told MATLAB that a, b, and c are all real.
syms x y k real;
syms f(x,y);
syms a b c real;
f(x, y) = a*x + b*y + c;
xd = -a*tanh(k*f)+b*sech(k*f);
yd = -b*tanh(k*f)-a*sech(k*f);
chid = atan2(yd, xd);
d_chid_dx = diff(chid, x);
>> simplify(d_chid_dx)
ans(x, y) =
-(a*k)/cosh(k*(c + a*x + b*y))
That is in agreement with your result, albeit expressed in terms of 1/cosh rather than sech.
>> answer2 = -a*k*sech(k*f(x, y));
>> answer1 = simplify(d_chid_dx);
>> isAlways(answer1 == answer2)
ans =
logical
1
Great, thanks! So the point was to specify that a and b are also real and not just x and y.

Sign in to comment.

More Answers (1)

Yes I am sure. Sorry, my bad and thanks for the good catch, but I forgot to mention that in my particular case
f(x,y) = a*x + b*y + c
so
diff(f, x) = a
and
diff(f,y) = b
and therefore if
f(x,y) = 0
then
a = 0
and
b = 0
and so
d_chid_dx = 0
just as you noticed. I should have pointed out this precisely of course. So the problem remains.

Categories

Find more on Phased Array System Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!