How to simplify output from solve

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 Comments

Why do you think the result is not about as simple as it can be? Compuations as you have done tend to create a massive mess of terms. That seems about normal to me.
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)
@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).

Sign in to comment.

Answers (1)

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 Comments

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).
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.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Asked:

on 13 Dec 2024

Commented:

on 14 Dec 2024

Community Treasure Hunt

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

Start Hunting!