solve x and y with var z,t in matlab

Assuming you have two equations with two variables:

main.m
a1*x + b1*y = t1
a2*x + b2*y = t2
34 chars
3 lines

where a1, a2, b1, b2, t1, and t2 are known values (constants) and x and y are the unknown variables that need to be solved.

You can represent the above equations in matrix format:

main.m
[A]*[x;y] = [T]
16 chars
2 lines

where [A] = [a1 b1; a2 b2], [x;y] is a column vector with the unknown variables [x; y], and [T] = [t1; t2] is a column vector with the known values.

To solve for the unknown variables, you can use the MATLAB backslash operator \. The backslash operator solves the linear equation [A]*[x;y] = [T] for [x;y].

main.m
A = [a1 b1; a2 b2];
T = [t1; t2];
xy = A \ T;
x = xy(1);
y = xy(2);
68 chars
6 lines

The variables x and y will contain the solutions to the equations.

related categories

gistlibby LogSnag