Solve non-linear symbolic equation
3 views (last 30 days)
Show older comments
i'm trying to find the solution of a non-linear equation using the command solve. the problem is a trigonometric one, where the variables are all dependent of the symbol T.
syms ('T','real');
cth = (1-T^2)/(1+T^2);
sth = 2*T/(1+T^2);
A=[G1 G2; G4 G5];
B=[-G3; -G6]; %Gi for i=1:6 is function of T
X = A\B;
x=X(1); %function of T
y=X(2); %function of T
Eq= (x-b1x)^2+(y-b1y)^2 - d^2;
T_values = solve(Eq,T);
T_values = vpa(T_values);
theta = 2*atan(T)
it shows Warning message: Solutions are only valid under certain conditions. To
include parameters and conditions in the solution, specify the
'ReturnConditions' value as 'true'.
then changing to:
T_values = solve(Eq,T,'ReturnConditions',true)
displays the following error: Error using sym>tomupad (line 1539)
Unable to convert 'struct' to 'sym'.
3 Comments
Maneet Kaur Bagga
on 13 Sep 2023
Hi Mohamed,
It would be easy to reproduce the code and solve the error if you provide the values of the variables.
Walter Roberson
on 9 Nov 2023
syms ('T','real');
cth = (1-T^2)/(1+T^2);
sth = 2*T/(1+T^2);
A=[G1 G2; G4 G5];
B=[-G3; -G6]; %Gi for i=1:6 is function of T
X = A\B;
x=X(1); %function of T
y=X(2); %function of T
Eq= (x-b1x)^2+(y-b1y)^2 - d^2;
sol = solve(Eq,T,'returnconditions', true);
T_values = vpa(sol.T);
theta = 2*atan(T_values)
sol.conditions
vpa(sol.conditions)
We cannot execute because you have not defined the G variables.
Answers (1)
Karan Singh
on 9 Nov 2023
Hi Mohamed,
The “solve” function in MATLAB is used to solve equations. By default, it returns all solutions without any conditions. However, in some cases, the solutions are only valid under certain conditions. The warning message you're seeing is MATLAB telling you that the solutions it found might only be valid under certain conditions. By specifying “ReturnConditions”, “true”, you're asking MATLAB to return these conditions along with the solutions.
However, the error message you're seeing is because you're trying to assign the output of “solve” with “ReturnConditions”, “true” to a single variable “T_values”. When you specify “ReturnConditions”, “true”, “solve” returns three outputs: the solutions, the parameters in the solutions, and the conditions on the parameters.
To fix the error, you should assign the output of “solve” to three variables, like this:
[sol, params, conditions] = solve(Eq, T, 'ReturnConditions', true);
After this, you can use “vpa” to convert “sol” to a numeric approximation:
T_values = vpa(sol);
Attached below are the documentation links that you may find helpful:
- Equations and systems solver - MATLAB solve (mathworks.com)
- Variable-precision arithmetic (arbitrary-precision arithmetic) - MATLAB vpa (mathworks.com)
Hope this helps!
Karan Singh Khati
2 Comments
Walter Roberson
on 9 Nov 2023
When you specify “ReturnConditions”, “true”, “solve” returns three outputs:
syms p
sol = solve(p^2 > 3, p, 'returnconditions', true)
sol.p
sol.conditions
Single output is fine.
However
vpa(sol)
That is because vpa() is not defined on a struct. You would need to vpa() something extracted from the struct
vpa(sol.p)
See Also
Categories
Find more on Symbolic Math 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!