Equations Solver
Equation
To solve the equation, we first need to create a vector to represent the polynomial, then find the roots using roots(p)
. r = roots(p)
returns the roots of the polynomial represented by p as a column vector. For example, p = [2, -2, -3] represents the polynomial 2x2 − 2x − 3.
To solve the equation 2x4 − 45x2 − 8x − 16 = 0, we need to create a vector to represent the polynomial, then find the roots.
System of linear equations
In mathematics, a system of linear equations (or linear system) is a collection of one or more linear equations involving the same set of variables. It can be represented as
a11x1 + a12x2 + … + a1nxn = b1
a21x1 + a22x2 + … + a2nxn = b2
…
an1x1 + an2x2 + … + an**nxn = bn
It can also be represented with the matrix: AX=B. To solve it,we can use the inverse of matrix.
For example, to solve
3x + 2y - z = 1
2x - 2y + 4z = -2
-x + 1/2 y - z = 0,
we could use the following syntax
a = [3, 2, -1; 2, -2, 4; -1, 1/2, -1]; % create the matrix of coefficients
b = [1; -2; 0] % create column vector
x = inv(a) * b % inv(X) computes the inverse of square matrix X
Nonlinear system of equations
A nonlinear system is a collection of equations that may contain some equations of a line, but not all of them. fsolve
is used to solve system of nonlinear equations. [x, fval, exitflag, output, jacobian] = fsolve(___) returns the Jacobian of fun at the solution x.
The following example shows how to solve two nonlinear equations in two variables. The equations are
2x1 - x2 - ex1 = 1
-x1 - 2x2 - ex2 = 2
Convert the equations to the form F(X)=0, we have
2x1 - x2 - ex1 - 1 = 0
-x1 - 2x2 - ex2 - 2 = 0.
Write a function that computes the left-hand side of these two equations and solve the system of equations starting at the point [0, 0].
Linear programming problems
Finds the minimum of a problem specified by
min f = -x1 - x2 - x3
f, x, b, beq, lb and ub are vectors, and A and Aeq are matrices.
Subject to
7x1 + 3x2 + 9x3 ≤ 1
8x1 + 5x2 + 4x3 ≤ 1
6x1 + 92 + 5x3 ≤ 1
x1, x2, x3 ≥ 0
We could use the following syntax to solve the problem.
f = [-1, -1, -1]; % coefficients of objective function
A = [7, 3, 9; 8, 5, 4; 6, 9, 5]; % the coefficients of matrix for unequal equations
b = [1, 1, 1] % constraints for unequal equatioins
Aeq = [] % the coefficients of matrix for equations
Beq = [] % constraints for equations
lb = [] % lowbound for variables
ub = [] % upbound for variables
x0 = [] % initial values
[x,fval,exitflag,output,lambda] = linprog(f, A, b, Aeq, Beq, lb, ub, x0)