Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

3 Solutions to Systems of Equations

3.1 Systems of Linear Equations

Mathematical formulations of engineering problems often lead to sets of simultaneous

linear equations. A general system of linear equations can be expressed in terms

of a coefficient matrix A, a right-hand-side (column) vector b and an unknown

(column) vector x as

Ax = b,

or, componentwise, as

a1,1 x1 + a1,2 x2 + · · · + a1,n xn = b1

a2,1 x1 + a2,2 x2 + · · · + a2,n xn = b2


..
.

an,1 x1 + an,2 x2 + · · · + an,n xn = bn .

When A is non-singular and square (n ⇥ n), meaning that the number of

independent equations is equal to the number of unknowns, the system has a

unique solution given by

1
x=A b,

where A 1 is the inverse of A. Thus, the solution vector x can, in principle, be

calculated by taking the inverse of the coefficient matrix A and multiplying it on

the right with the right-hand-side vector b.

32
Then we can find the solution of the linear system of equations as

x = inv( A) ⇤ b.

The last command produces the expected result; that is, the result of the inversion

of the matrix. It can also be obtained as follows:

x = linsolve( A, b).

Example

Write a Matlab code to find the solution of the following system of linear equations:

x + 2y = 4

3x + 4y = 5.

Solution:

A = [1 2 ; 3 4];

b = [4 ; 5];

x = inv( A) ⇤ b or x = linsolve( A, b).

3.2 A Non-linear Equation

In order to use the Matlab solvers, you must first be able to write Matlab functions.

There are two different methods to create a function:

1. Anonymous Function.

33
2. Matlab Editor. (we have disscused this type in the first semester)

An anonymous function is a function that is not stored in a program file, but

is associated with a variable whose data type is function-handle. Anonymous

functions can accept multiple inputs and return one output. They can contain

only a single executable statement.

For example, create a handle to an anonymous function that finds the square

of a number:

>> sqr = @( x ) x. ˆ 2;

fzero can be used to solve a single variable nonlinear equation of the form

f ( x ) = 0. The equation must first be programmed as a function (either anonymous

function or m-file).

The following command solves the equation f ( x ) = x3 5x2 x + 2, starting

from an initial guess of x = 4.

>> f = @( x ) x ˆ 3 5x ˆ 2 x + 2;

>> f zero ( f , 4)

Example

Solve cos( x ) = x by Matlab using initial condition x = 1.

Solution:

34
>> f = @( x ) cos( x ) x;

>> f zero ( f , 1)

ans= 0.7391

3.3 Systems of Non-linear Equations

The Matlab command fsolve(function name,initial condition) is used to solve sets

of nonlinear algebraic equations using a quasi-Newton method. The user must

write a system of nonlinear equations as a column vector.

Example

Consider the following system of nonlinear equations, and solve for x1 and x2

using initial condition (1,1):

x1 x2 5x2 + 10 = 0

x13 x22 2=0

Solution:

>> f = @( x ) [ x (1) ⇤ x (2) 5 ⇤ x (2) + 10 ; x (1) ˆ 3 x (2) ˆ 2 2];

>> f solve( f , [1, 1])

ans= 3 5

35

You might also like