Wrong symbolic solution for trigonometry

syms x
solve(sin(x)/cos(x)==-3.0)
ans = 
and
syms x
solve(tan(x)==-3.0)
ans = 
Produces different result instead of Wolfwram and ti-89 who produces same result for these operations.

4 Kommentare

Torsten
Torsten am 7 Nov. 2023
Bearbeitet: Torsten am 7 Nov. 2023
Both are solutions, only written more or less complicated:
syms x
sol1 = double(solve(sin(x)/cos(x)==-3.0))
sol1 = 2×1
1.8925 -1.2490
tan(sol1)
ans = 2×1
-3.0000 -3.0000
sol1(2) + pi
ans = 1.8925
and
syms x
sol2 = double(solve(tan(x)==-3.0))
sol2 = -1.2490
tan(sol2)
ans = -3
Boris
Boris am 7 Nov. 2023
but only second one correct in math equation.
Torsten
Torsten am 7 Nov. 2023
Both are correct as you can see when tan is applied to the results.
As you can see from the calculation, -log(-sqrt(-16/5-12/5*i)/2)*i equals atan(-3) in the interval [0 pi], -log(sqrt(-16/5-12/5*i)/2)*i equals atan(-3) in the interval [-pi 0].
No. Both solutions returned are absolutely valid.
syms x
xsol = solve(sin(x)/cos(x)==-3.0)
xsol = 
vpa(sin(xsol)./cos(xsol))
ans = 
vpa(xsol)
ans = 
Now, compare that result to
atan(-3) + [pi,0]
ans = 1×2
1.8925 -1.2490
As you can see, both are perfectly valid, mathematically correct solutions.
solve generated two solutions as it was used there, probably because sin(x)/cos(x) can reduce to an implicit quadratic polynomial problem. So it looks like solve did the transformation, then solved the resulting quadratic, with the result being two valid solutions from the general solution locus:
atan(-3) + k*pi

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 8 Nov. 2023
syms x
sol = solve(sin(x)/cos(x)==-3.0)
sol = 
sola = rewrite(sol, 'atan')
sola = 
simplify(sola, 'steps', 100)
ans = 
simplify(sola, 'steps', 5000)
ans = 
At the moment I do not know why it is able to identify atan(3) in the first row at the bottom, but not identify atan(3) in the second row, even though with steps 100 it was able to identify that the two are the same except + pi for one of them.

Weitere Antworten (1)

Les Beckham
Les Beckham am 7 Nov. 2023
It looks like your second solve construct only returns one of the solutions. Someone who knows the symbolic toolbox better than I do may be able to explain why.
syms x
s1 = solve(sin(x)/cos(x)==-3.0)
s1 = 
format long
double(s1)
ans = 2×1
1.892546881191539 -1.249045772398254
syms x
s2 = solve(tan(x)==-3.0)
s2 = 
double(s2)
ans =
-1.249045772398254

14 Kommentare

syms x
solve(tan(x) == -3,'returnconditions',true)
ans = struct with fields:
x: pi*k - atan(3) parameters: k conditions: in(k, 'integer')
On problems with infinitely many solutions, solve returns one solution if you just ask for a solution. 'returnconditions" gets solve to try to generate the complete family of solution, as it did here. On polynomial problems, it is sometimes able to return all solutons, but this is not a polynomial, even though any school kid SHOULD be able to give the complete solution locus. So you need to push it to get the complete set of solutions.
Boris
Boris am 7 Nov. 2023
Bearbeitet: Boris am 7 Nov. 2023
It looks MATLAB symbolic wrong on simply scholar trigonometric exercise.
Torsten
Torsten am 7 Nov. 2023
Why do you say "wrong" ? All solutions x from the symbolic toolbox so far satisfy tan(x) = -3.
Steven Lord
Steven Lord am 8 Nov. 2023
MATLAB may not return the answer you expected. That does not mean the answer MATLAB returned is incorrect.
If you asked me "What number, when squared, equals 4?" the answer you may expect is 2. But -2 is an equally valid answer.
Boris
Boris am 8 Nov. 2023
The solutions are different for the same equations. That means one is "wrong". If other instrument give me same solutine for both variants and MATLAB give different It to enough to say that MATLAB give wrong result.
Torsten
Torsten am 8 Nov. 2023
As @Steven Lord says: If you have the equation x^2 = 4 and one tool gives you the solution x = 2 and another the solution x = 2 or x = -2, then neither tool is wrong. The solution of the first tool is incomplete, but not wrong.
Boris
Boris am 8 Nov. 2023
So why second one is different with first of these?
Torsten
Torsten am 8 Nov. 2023
Bearbeitet: Torsten am 8 Nov. 2023
Because all angles of the form angles = k*pi - atan(3) for k integer lead to tan(angles) = -3:
k = -5:5;
angles = k*pi - atan(3)
angles = 1×11
-16.9570 -13.8154 -10.6738 -7.5322 -4.3906 -1.2490 1.8925 5.0341 8.1757 11.3173 14.4589
tan(angles)
ans = 1×11
-3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000 -3.0000
Boris
Boris am 8 Nov. 2023
I understud, but my question is why MATLAB provide two different kind of solution for same form (causing tan(x)==sin(x)/cos(x)).
Torsten
Torsten am 8 Nov. 2023
Bearbeitet: Torsten am 8 Nov. 2023
Because it most probably uses two different methods to solve it.
In the first case, it can directly apply the atan function and returns atan(-3).
In the second case, it maybe squares the equation and uses sin(x)^2 + cos(x)^2 = 1 to get a quadratic equation either in sin(x) or cos(x). And a quadratic usually has two solutions. But this is speculation - I don't have insight into the internals of the symbolic toolbox.
The second one is the application of the inverse function.
The first one, MATLAB probably rearranges the equation
syms x
xsol = solve(sin(x) + 3*cos(x) == 0)
xsol = 
xsol = double(xsol)
xsol = 2×1
1.8925 -1.2490
y = @(x) sin(x) + 3*cos(x);
fplot(y, [-1.5 2]), hold on
plot(xsol(1), 0, 'x', 'markersize', 12, 'linewidth', 3), grid on
plot(xsol(2), 0, 'o', 'markersize', 12, 'linewidth', 3)
Torsten
Torsten am 8 Nov. 2023
This looks better than my guess. Thank you, Sam.
Boris
Boris am 8 Nov. 2023
@Torsten @Sam Chak thank you for clarification.
syms x
eqn = sin(x)/cos(x)==-3.0
eqn = 
LS = lhs(eqn);
RS = rhs(eqn);
fplot([LS, RS], [-2*pi 2*pi])
Each intersection of a blue line with the red line is a solution. You can see that there are an infinite number of solutions.
sol = solve(eqn, 'returnconditions', true)
sol = struct with fields:
x: [2×1 sym] parameters: k conditions: [2×1 sym]
simplify(sol.x, 'steps', 500)
ans = 
sol.conditions
ans = 
Infinite number of solutions π apart.
MATLAB probably could have generated the unified series but that does not mean it was wrong to generate the forms that it did generate. It is a common approach to analyze in terms of full cycles

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by