Solve non-linear symbolic equation

5 Ansichten (letzte 30 Tage)
mohamed faida
mohamed faida am 30 Mai 2022
Kommentiert: Karan Singh am 9 Nov. 2023
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 Kommentare
Maneet Kaur Bagga
Maneet Kaur Bagga am 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
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Karan Singh
Karan Singh am 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:
Hope this helps!
Karan Singh Khati
  2 Kommentare
Walter Roberson
Walter Roberson am 9 Nov. 2023
When you specify “ReturnConditions”, “true”, “solve” returns three outputs:
syms p
sol = solve(p^2 > 3, p, 'returnconditions', true)
sol = struct with fields:
p: [2×1 sym] parameters: x conditions: [2×1 sym]
sol.p
ans = 
sol.conditions
ans = 
Single output is fine.
However
vpa(sol)
Error using sym>tomupad
Unable to convert 'struct' to 'sym'.

Error in sym (line 273)
S.s = tomupad(x);

Error in vpa (line 46)
ss = sym(s);
That is because vpa() is not defined on a struct. You would need to vpa() something extracted from the struct
vpa(sol.p)
Karan Singh
Karan Singh am 9 Nov. 2023
Thanks Walter for the correction!

Melden Sie sich an, um zu kommentieren.

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by