How to simplify output from solve

조회 수: 32 (최근 30일)
Simon
Simon 2024년 12월 13일 21:12
댓글: Star Strider 2024년 12월 14일 2:00
I execute the following commands:
% Define symbolic variables
syms a b c d e f g h i
syms x1 y1 x2 y2 x3 y3 x4 y4
syms x y
% Define the formula
denom = g*x + h*y + i;
f1 = (a*x + b*y + c)/denom;
f2 = (d*x + e*y + f)/denom;
% Define the constraint
constraint = [subs(f1, [x, y], [1, 1]) == x1,
subs(f1, [x, y], [-1, 1]) == x2,
subs(f1, [x, y], [1, -1]) == x3,
subs(f1, [x, y], [-1, -1]) == x4,
subs(f2, [x, y], [1, 1]) == y1,
subs(f2, [x, y], [-1, 1]) == y2,
subs(f2, [x, y], [1, -1]) == y3,
subs(f2, [x, y], [-1, -1]) == y4];
% Solve for a to i
vars = [a, b, c, d, e, f, g, h, i];
solution = solve(constraint, vars);
% Display the solution
disp('Solution:');
disp(solution);
Which outputs:
a: (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
b: -(x1*x2*y3 - x2*x3*y1 - x1*x2*y4 + x1*x4*y2 - x1*x4*y3 + x3*x4*y1 + x2*x3*y4 - x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
c: -(x1*x2*y3 - x1*x3*y2 - x1*x2*y4 + x2*x4*y1 + x1*x3*y4 - x3*x4*y1 - x2*x4*y3 + x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
d: (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
e: -(x1*y2*y3 - x3*y1*y2 - x2*y1*y4 + x4*y1*y2 - x1*y3*y4 + x3*y1*y4 + x2*y3*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
f: -(x2*y1*y3 - x3*y1*y2 - x1*y2*y4 + x4*y1*y2 + x1*y3*y4 - x4*y1*y3 - x2*y3*y4 + x3*y2*y4)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
g: (x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
h: -(x1*y2 - x2*y1 - x1*y4 + x2*y3 - x3*y2 + x4*y1 + x3*y4 - x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
i: 1
What commands should I run in order to simplify these expressions and find common factors between these formulas?
Thanks!
  댓글 수: 3
Torsten
Torsten 2024년 12월 13일 23:45
편집: Torsten 2024년 12월 13일 23:46
You should normalize variable i to 1 right at the beginning so that you have 8 equations in 8, not 9 unknowns.
% Define symbolic variables
syms a b c d e f g h
syms x1 y1 x2 y2 x3 y3 x4 y4
syms x y
% Define the formula
denom = g*x + h*y + 1;
f1 = (a*x + b*y + c)/denom;
f2 = (d*x + e*y + f)/denom;
% Define the constraint
constraint = [subs(f1, [x, y], [1, 1]) == x1,
subs(f1, [x, y], [-1, 1]) == x2,
subs(f1, [x, y], [1, -1]) == x3,
subs(f1, [x, y], [-1, -1]) == x4,
subs(f2, [x, y], [1, 1]) == y1,
subs(f2, [x, y], [-1, 1]) == y2,
subs(f2, [x, y], [1, -1]) == y3,
subs(f2, [x, y], [-1, -1]) == y4];
% Solve for a to i
vars = [a, b, c, d, e, f, g, h];
solution = solve(constraint, vars);
% Display the solution
disp('Solution:');
Solution:
disp(solution);
a: (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) b: -(x1*x2*y3 - x2*x3*y1 - x1*x2*y4 + x1*x4*y2 - x1*x4*y3 + x3*x4*y1 + x2*x3*y4 - x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) c: -(x1*x2*y3 - x1*x3*y2 - x1*x2*y4 + x2*x4*y1 + x1*x3*y4 - x3*x4*y1 - x2*x4*y3 + x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) d: (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) e: -(x1*y2*y3 - x3*y1*y2 - x2*y1*y4 + x4*y1*y2 - x1*y3*y4 + x3*y1*y4 + x2*y3*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) f: -(x2*y1*y3 - x3*y1*y2 - x1*y2*y4 + x4*y1*y2 + x1*y3*y4 - x4*y1*y3 - x2*y3*y4 + x3*y2*y4)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) g: (x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) h: -(x1*y2 - x2*y1 - x1*y4 + x2*y3 - x3*y2 + x4*y1 + x3*y4 - x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)
Simon
Simon 2024년 12월 14일 0:29
@John D'Errico I know from this thread that it can be simplified https://math.stackexchange.com/questions/186286/get-transformation-matrix-from-points. However as I want to extend the original formulas, having such simplifications automated would be helpful (and useful to know about in general).

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2024년 12월 13일 22:22
I can’t be certain that this is a significant improvement, however it is the best I can do with your data —
% Define symbolic variables
syms a b c d e f g h i
syms x1 y1 x2 y2 x3 y3 x4 y4
syms x y
% Define the formula
denom = g*x + h*y + i;
f1 = (a*x + b*y + c)/denom;
f2 = (d*x + e*y + f)/denom;
% Define the constraint
constraint = [subs(f1, [x, y], [1, 1]) == x1,
subs(f1, [x, y], [-1, 1]) == x2,
subs(f1, [x, y], [1, -1]) == x3,
subs(f1, [x, y], [-1, -1]) == x4,
subs(f2, [x, y], [1, 1]) == y1,
subs(f2, [x, y], [-1, 1]) == y2,
subs(f2, [x, y], [1, -1]) == y3,
subs(f2, [x, y], [-1, -1]) == y4];
% Solve for a to i
vars = [a, b, c, d, e, f, g, h, i];
solution = solve(constraint, vars);
Warning: Solutions are only valid under certain conditions. To include parameters and conditions in the solution, specify the 'ReturnConditions' value as 'true'.
sc = struct2cell(solution);
solutions = cell(size(sc));
for k = 1:numel(sc)
solutions{k,:} = [vars(k) simplify(sc{k}, 500)];
end
% Display the solution
disp('Solution:');
Solution:
disp(solutions);
{[a (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) ]} {[b -(x1*x2*y3 - x2*x3*y1 - x1*x2*y4 + x1*x4*y2 - x1*x4*y3 + x3*x4*y1 + x2*x3*y4 - x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)]} {[c -(x1*x2*y3 - x1*x3*y2 - x1*x2*y4 + x2*x4*y1 + x1*x3*y4 - x3*x4*y1 - x2*x4*y3 + x3*x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)]} {[d (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) ]} {[e -(x1*y2*y3 - x3*y1*y2 - x2*y1*y4 + x4*y1*y2 - x1*y3*y4 + x3*y1*y4 + x2*y3*y4 - x4*y2*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)]} {[f -(x2*y1*y3 - x3*y1*y2 - x1*y2*y4 + x4*y1*y2 + x1*y3*y4 - x4*y1*y3 - x2*y3*y4 + x3*y2*y4)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3)]} {[g (x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) ]} {[h -(x1*y2 - x2*y1 - x1*y4 + x2*y3 - x3*y2 + x4*y1 + x3*y4 - x4*y3)/(x1*y2 - x2*y1 - x1*y3 + x3*y1 + x2*y4 - x4*y2 - x3*y4 + x4*y3) ]} {[i 1 ]}
.
  댓글 수: 2
Simon
Simon 2024년 12월 14일 0:33
Thanks! I did try similar things already without seeing too much difference. I was hoping there was a way for matlab to identify common factors between the outputted formulas, like here https://math.stackexchange.com/questions/186286/get-transformation-matrix-from-points but maybe it's not capable of doing that (as I was going to extend the original equations).
Star Strider
Star Strider 2024년 12월 14일 2:00
My pleasure!
In my experience, simplify uses every method (such as collect) at its disposal to provide the best result.
You could use coeffs to see if you can isolate common factors, however I doubt that the result would be in any way illuminating.

댓글을 달려면 로그인하십시오.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by