How to simplify output from solve
Show older comments
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
John D'Errico
on 13 Dec 2024
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:');
disp(solution);
Simon
on 14 Dec 2024
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);
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:');
disp(solutions);
.
Categories
Find more on Mathematics in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!