How to obtain direct symbolic solutions without introducing intermediate variables like z and z1 in MATLAB's solve function?
22 次查看(过去 30 天)
显示 更早的评论
Hello,
I am working on solving a system of symbolic equations in MATLAB using the solve function. When I solve for the variables theta and phi, the solution introduces intermediate variables such as z and z1 as part of the result. However, I would like to see the solutions directly without these intermediate variables, if possible.
syms theta phi d0 sig real
% Expressions for x1, x2, y1, y2
x1 = sin(theta);
x2 = cos(theta);
y1 = sin(phi);
y2 = cos(phi);
% Dot product and norms
dot_product = x1 * y1 + x2 * y2;
x_squared_norm = x1^2 + x2^2;
y_squared_norm = y1^2 + y2^2;
% V_x definition
V_x = ( ((x1 + x2) * x_squared_norm - (y1 + y2) * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ...
+ (x1 * y_squared_norm - y1 * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ...
+ (x2 * y_squared_norm - y2 * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ) / ...
(x_squared_norm^2 * y_squared_norm^2 - dot_product^2)^2;
% Partial derivatives
dVx_dtheta = diff(V_x, theta);
dVx_dphi = diff(V_x, phi);
% Solve system of equations
eqn1 = dVx_dtheta == 0;
eqn2 = dVx_dphi == 0;
% assume
assume(0 <= theta <= pi/2);
assume(0 <= phi <= pi/2);
% Find solutions
joint_solutions = solve([eqn1, eqn2], [theta, phi], 'ReturnConditions', true, 'Real', true);
Output:
joint_solutions =
struct with fields:
theta: [2×1 sym]
phi: [2×1 sym]
parameters: [k l x z z1]
conditions: [2×1 sym]
>> joint_solutions.theta
ans =
z
2*pi*k - 2*atan(1/z)
>> joint_solutions.conditions
ans =
in((z - 2*atan(x))/(2*pi), 'integer') & in((z1 - 2*atan(x))/(2*pi), 'integer') & in(x, 'real') & z <= pi/2 & z1 <= pi/2 & sig ~= 0 & 0 <= z & 0 <= z1
in(k, 'integer') & in(l, 'integer') & 4*atan(z) + 4*pi*l <= pi & in(z, 'real') & atan(1/z) <= pi*k & 0 <= atan(z) + pi*l & 4*pi*k <= pi + 4*atan(1/z) & sig ~= 0
The solve function works fine, but it introduces variables z and z1 in the solution. I would like to get the solution in terms of the original variables (like theta and phi) without introducing these intermediate variables.
If it can't, I want to see what is the z and z1 in the solution.
I have tried adjusting the MaxDegree option, but I am still getting intermediate variables.
My purpose is to find out exactly what Z and Z1 are referring to in the expression, so that I can represent the result as a formula. To prevent the case there's any misunderstanding, I want to know the specifics of Z, i.e. the formula.
z = Long formula which consists of some of variables
Thank you!
0 个评论
采纳的回答
Torsten
2024-10-21,9:20
编辑:Torsten
2024-10-21,9:36
If it can't, I want to see what is the z and z1 in the solution.
k,l,x,z and z1 are arbitrary numbers that satisfy joint_solutions.conditions.
If you want to get a single valid solution for theta and phi, remove " 'ReturnConditions', true, 'Real', true " in the call to "solve".
syms theta phi d0 sig real
% Expressions for x1, x2, y1, y2
x1 = sin(theta);
x2 = cos(theta);
y1 = sin(phi);
y2 = cos(phi);
% Dot product and norms
dot_product = x1 * y1 + x2 * y2;
x_squared_norm = x1^2 + x2^2;
y_squared_norm = y1^2 + y2^2;
% V_x definition
V_x = ( ((x1 + x2) * x_squared_norm - (y1 + y2) * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ...
+ (x1 * y_squared_norm - y1 * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ...
+ (x2 * y_squared_norm - y2 * dot_product)^2 * (d0^2 * sig^2 + 0.5 * sig^4) ) / ...
(x_squared_norm^2 * y_squared_norm^2 - dot_product^2)^2;
% Partial derivatives
dVx_dtheta = simplify(diff(V_x, theta));
dVx_dphi = simplify(diff(V_x, phi));
% Solve system of equations
eqn1 = dVx_dtheta == 0;
eqn2 = dVx_dphi == 0;
% assume
assume(0 <= theta <= pi/2);
assume(0 <= phi <= pi/2);
% Find solutions
%joint_solutions = solve([eqn1, eqn2], [theta, phi], 'ReturnConditions', true, 'Real', true);
joint_solutions = solve([eqn1, eqn2], [theta, phi]);
joint_solutions.theta
joint_solutions.phi
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!