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.

p = [2 0 -45 -8 -16];
      
      r = roots(p)
r =
      
         4.8642 + 0.0000i
        -4.6916 + 0.0000i
        -0.0863 + 0.5858i
        -0.0863 - 0.5858i

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
x =
      
          1.0000
         -2.0000
         -2.0000

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

fun = @root2d;
      x0 = [-5,5];
      [optx,fval] = fsolve(fun,x0)
function  F = root2d(x)
      
      F = [2*x(1)-x(2)-exp(-x(1))-1; -x(1)+2*x(2)-exp(-x(2)-2)];
      
      end
      
optx =
      
          0.9511    0.5160
      
      
      fval =
      
         1.0e-07 *
      
         -0.4106
         -0.0331

Linear programming problems

Finds the minimum of a problem specified by

min f = -x1 - x2 - x3

[x, fval, exitflag, output, lambda] = linprog(f, A, b, Aeq, lb, ub, x0, options)

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)
x =
      
          0.0870
          0.0356
          0.0316


Previous | Up | Next