% Jake Bobowski
% July 24, 2017
% Created using MATLAB R2014a

% You can clear all previous variable assignments using 'clearvars':
clearvars

% To solve a system of equations, we will use the MATLAB function
% "solve".  We firest have to tell MATLAB which variable we will be using.
% In this first trivial example, we will use "x" as the variable.
syms x

% Next, we define our equation(s).
eqn1 = x - 2 == 0;

% Now we can implement solve([...], ...).  In the square brackets,
% insert each equation in your system separated by commas.  After the
% square brackets, list the variables that you wish to solve for (separated
% by commas).
soln = solve([eqn1], x);

% ***IMPORTANT***  The line above works in MATLAB R2014a which is the
% version that I'm currently working with on my computer.  If you're working
% with a more recent version of MATLAB, the variables will need to ne
% enclosed in square brackets also.
% soln = solve([eqn1], [x]); % newer versions of MATLAB

% Access the solution as follows:
soln

% Here's a second trivial example involving a system of two equations and
% two unknowns.  We'll call our unknowns x and y.
syms x y

% Next, we define our equations,
eqn1 = x == 2;
eqn2 = y == x^2;

% and then solve.
soln = solve([eqn1, eqn2], x, y);
% soln = solve([eqn1, eqn2], [x, y]); % newer versions of MATLAB

% This time, access the pair of solutions using:
xSoln = soln.x
ySoln = soln.y

% Here's another, less trivial system of two equations and two unknowns.
syms x y

eqn1 = 10*x - 3*y == 5;
eqn2 = -2*x - 4*y == 7;

soln = solve([eqn1, eqn2], x, y);
% soln = solve([eqn1, eqn2], [x, y]); % newer versions of MATLAB
xSoln = soln.x
ySoln = soln.y

% It is also possible to algebraically solve a system of equations in terms
% of other variables.  Below is a system of 6 complex equations and 6 unknowns.
% The "i" that appears in eqn4, eqn5, and eqn6 is the imaginary number for which
% i^2 = -1.

% The example below solves for the currents in an all-pass filter.
% (Optional experiment in PHYS 231.)

% We much define the six currents that we wish to solve for as variables as
% well as the other relevant variables (R is resistance, C is capacitance,
% L is inductance, v is voltage amplitude, and w is angular freqeuncy.
syms i1 i2 i3 i4 i5 i6 v R w L C
eqn1 = i1 - i2 - i3 == 0;
eqn2 = i2 - i4 - i5 == 0;
eqn3 = i3 + i5 - i6 == 0;
eqn4 = i1*R + i*w*i2*L + i5*R + i*w*i6*L == v;
eqn5 = i1*R + i3/(i*w*C) + i*w*i6*L == v;
eqn6 = i4/(i*w*C) - i5*R - i*w*i6*L == 0;

% In our solve expression we must tell MATLAB which six variables we want4
% to solve for.  In this example, it is the six currents.
soln = solve([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6], i1, i2, i3, i4, i5, i6);
% soln = solve([eqn1, eqn2, eqn3, eqn4, eqn5, eqn6], [i1, i2, i3, i4, i5, i6]); % newer versions of MATLAB
i1soln = soln.i1
i2soln = soln.i2
i3soln = soln.i3
i4soln = soln.i4
i5soln = soln.i5
i6soln = soln.i6

% If you run this script, you'll notice that the MATLAB output is not
% formatted nicely.  It is generally true that MATLAB does not handle
% symbolic math very well.  It was designed to crunch numbers, not do
% alegbra.  I recommend using another mathematics software for symbolic
% math.  Maple is available at UBC Okanagan and Mathematica is another option.

% Symbolic expressions can be displayed a little more neatly using
% pretty().  For example, here is the solution for current i5:
pretty(i5soln)
 
soln =
 
2
 
 
xSoln =
 
2
 
 
ySoln =
 
4
 
 
xSoln =
 
-1/46
 
 
ySoln =
 
-40/23
 
 
i1soln =
 
(v*(- C*L*w^2 + C*R*w*(2*i) + 1))/(2*(R*i - L*w)*(C*R*w - i))
 
 
i2soln =
 
(v*i)/(2*(R*i - L*w))
 
 
i3soln =
 
(C*v*w)/(2*(C*R*w - i))
 
 
i4soln =
 
(C*v*w)/(2*(C*R*w - i))
 
 
i5soln =
 
(v*(C*L*w^2 + 1))/(2*(R*i - L*w)*(C*R*w - i))
 
 
i6soln =
 
v/(2*(R + L*w*i))
 
              2
      v (C L w  + 1)
-------------------------
(R i - L w) (C R w - i) 2