Department of Chemical Engineering
MATLAB Tutorial
 

Solution of Non-Linear Algebraic Equations                             (last updated   9/9/99)

The information in this tutorial is located in the MATLAB manual. Any page or section numbers refer to the following:

The Student Edition of MATLAB, Version 5, The MATH WORKS Inc. Prentice-Hall, 1995.

======================================================================================

This tutorial contains the following sections;

Introduction

Solution of Non-Linear Equations Using the Symbolic solve Command

Solution of a Single Non-Linear Equation Using the Numerical fzero or fsolve Commands

Example Problem #1 - Single Non-Linear Equation

Example Problem #1 Solution - Using the Numerical fzero or  fsolve Commands

Solution of a System of Non-Linear Equations Using the Numerical fsolve Command

Example Problem #2 - System of Non-Linear Equations

Example Problem #2 Solution - Using the Numerical fsolve Command
=============================================================================================================

Introduction

Solution of a single or system of non-linear equations is one of the most commonly encountered problems in chemical engineering.

We'll look at two cases, and show an example of each.

(1) The solution of a single non-linear equation 

example: T4 - 14,500 = (T -250)
 

(2) The solution of a system of non-linear equations   Note:  If a single equation in a set is non-linear, the entire set is considered non-linear.
 

=============================================================================================================

Solution of a Non-Linear Equations Using the Symbolic solve Command

solve determines the solution equations symbolically if it can isolate the unknown, or numerically if it cannot. If it solves the equations symbolically you can then use the eval command to convert to a numerical solution.

The solve command usage is as follows.

>> solve('equations to be solved','variables to solve for')

The equation can be defined symbolically before executing the solve command.

>> eqn = 'equation to be solved'
>> solve(eqn,'variable to solve for')

Note:  This method can be confusing as is can be difficult to determine when MATLAB will be able to determine a numerical solution.  If you want to try it, see the MATLAB manual or Dr. Gu has several examples on the web.

==============================================================================================================

Solution of a Single Equation Using the Numerical fzero or fsolve Commands

Note:  For a single non-linear equation fzero is preferred because it is in the student edition of MATLAB.  fsolve is part of the Optimization Toolbox. If you have the student version of MATLAB you do not have this routine. You must use a machine that has the professional version.  The advantage of fsolve is that it solves both single equations or sets of equations.  The two routines will be discussed together here because the command line syntax to call the routines is identical.

fzero and fsolve are built-in MATLAB functions which when called by the user, access a function m-file the user provides. The information we provide to fzero or  fsolve in terms of variables and the function must be in a specific form.   Likewise, the information returned to us in a specific form

The statement we use to call the fzero or  fsolve routines looks like:

>>   options = [ ];
>>   grad  =  [ ];
>>  z = fzero(function_name',initial_guess, options, grad, p1, p2, ...)
>> z = fsolve('function_name', initial_guess, options, grad, p1, p2, ...)
 
Where
    z = the values of the unknowns returned by fzero or  fsolve.
    function_name = the name of the function m-file containing the equation or equations.   See below for the form of the function file.
    initial_guess = the initial guess of the unknown variable.   The closer you can get to the value the better chance of finding the correct root.
   options and grad are parameters that allow you too change the default settings that fsolve uses to solve the problem.  The empty brackets tell MATLAB to use
        the default settings.  For almost any problem you will encounter the default settins will work.
   p1, p2, etc are parameters that the user wants to pass to the function file.  Each parameter may be a single element or an array.  If no parameters need to be
      passed to the function file the command can be shortened to:
     >>  z = fsolve('function_name',initial_guess)
 

Sometimes fsolve cannot find a solution.  In this case try one of the following.
-  a different initial guess
-  rearranging the equation  (It still must be in the form f(x) = 0)

=============================================================================================================

Example Problem #1 - Single Non-Linear Equation

Solve the following equation from heat transfer for the unknown temperature T.

where beta and T_air are parameters set by the programmer or program user.

==============================================================================================================

Example Problem #1 Solution - Using the Numerical fzero or fsolve Commands
_______________________________________________________________________________________

Here is the function file heat.m.  Note the form of the function. Put everything on the right side so the equation is in the form f(x) = 0. We are searching for the T where y = 0.
_______________________________________________________________________________________

%   Darin Ridgway
%   Chemical Engineering
%   Last updated  -  July 8, 1998

function y = heat(T, beta, T_air)

%   y  represents the function in the form f (x)  =  0
%   T  is the independent variable you are solving for
%   beta and T_air are parameters passed to the function file
y = beta*T^4 + T - T_air;
______________________________________________________________________________________

Here is the main m-file that calls fsolve.
____________________________________________________________________________________________________________

%   Darin Ridgway
%   Chemical Engineering
%   Last updated  -  July 8, 1998

%   Make an initial guess as to the root. 
T0 = 300;

%   Set the parameters
beta = 5.173e-10;
T_air = 477.7;
options = [ ];
grad = [ ];

%   Call fzero
T = fzero('heat',T0, options, grad, beta, T_air) ;

%   Create the output
disp('   Darin Ridgway')
disp('   ChE XXX')
disp('   July 8, 1998')
disp('   Example Problem')
fprintf('\n\n\n  The temperature is % 5.1f  Kelvin \n\n',T)
____________________________________________________________________________________________________________

Here is the response in the Command Window
____________________________________________________________________________________________________________

   Darin Ridgway
   ChE XXX
   July 8, 1998
   Example Problem
 

  The temperature is 455.4 Kelvin
_____________________________________________________________________________________________________________
 
If you do not get an answer, try a different initial guess or rearrange the equation.  If you get an answer that doesn't make sense;
first check the value of the function value at the answer. If it is approximately equal to zero then you have found another root.
Try a different initial guess.

==============================================================================================================

Solution of a System of Non-Linear Equations Using the Numerical fsolve Command

The equations go in a function m-file. All the equations must be in the form f(x) = 0.

function z = filename(x)

z(1) = f(x)
:
z(n) = f(x)

where x is a vector of length n containing all the unknowns.
 

The statement used to call the fsolve routine is as follows.

z = fsolve('function_name', initial_guess, options, grad, p1, p2, ...)
 
Where   z is a vector containing the solution for each unknown
             function_name = the name of the function m-file containing the equation or equations.   See below for the form of the function file.
              initial_guess = a vector which contains an initial guess for each variable.   The closer you can get to the value the better chance of finding the correct root.
          options and grad are parameters that allow you too change the default settings that fsolve uses to solve the problem.  The empty brackets tell MATLAB to
                     use the default settings.  For almost any problem you will encounter the default settins will work.
            p1, p2, etc are parameters that the user wants to pass to the function file.  Each parameter may be a single element or an array.  If no parameters need to be
                      passed to the function file the command can be shortened to:
                      >>  z = fsolve('function_name',initial_guess)
 
=============================================================================================================

Example Problem #2 - Solution of System of Non-Linear Equations

The following problem is from Eide, Jenison, Mashaw, and Northrup, Engineering Fundamentals and Problem Solving, McGraw-Hill, 1979.
 
Ether is used in the process of extracting cod liver oil from livers. The following information is known about the oil extraction process. Livers enter the extractor at 1,050 kg/h and consist of 33 w% oil and 67 w% inert material. The solvent enters the extractor at 2,150 kg/h and consists of 1.5 w% oil and 98.5 w% ether. The extract leaves the process at 1,850 kg/h and consists of 19 w% oil and 81 w% ether. Determine the flow rate and composition of the leaving processed livers.

=============================================================================================================

Example Problem #2 Solution - Using the Numerical fsolve Command

____________________________________________________________________________________

Here is the function file, liver.m, that contains the functions.
____________________________________________________________________________________

%   Darin Ridgway
%   ChE XXX
%    July 8, 1998

%    x(1) = the rate of processed livers
%   x(2) = the weight fraction inert material
%   x(3) = the weight fraction of oil
%   x(4) = the weight fraction of ether
 
function z = liver(x, flow)

z(1) =  flow(1)  +  flow(2)  -  flow(3)  -  x(1);
z(2) = 0.67*flow(1)  -  x(1)*x(2);
z(3) = 0.985*flow(2)  -  0.81*flow(3)  -  x(4)*x(1);
z(4) = x(2)  +  x(3)  +  x(4)  -  1;
_____________________________________________________________________________________

Here is the main m-file the calls the fsolve routine.
_____________________________________________________________________________________

%   Darin Ridgway
%   ChE XXX
%    July 8, 1998

%   Make an initial guess for each unknown. These must be in an array.
x0 = [ 1000  0.5  0.5  0.5 ];

%   Set the parameter values for the variable flow
flow  =  [ 1050,  2150,  1850];
options  = [ ];
grad  =  [ ];

%   Call fsolve
y = fsolve('liver',x0,options, grad, flow);
 
%   Create the output
disp('   Darin Ridgway')
disp('   ChE XXX')
disp('   July 8, 1998')
disp('   Example Problem 2')
fprintf('\n\n\n   The flowrate of livers is %6.1f lbs/hr \n',y(1))
fprintf('   The wgt fraction of inerts is %5.3e \n',y(2))
fprintf('   The wgt fraction of oil is %5.3e \n',y(3))
fprintf('   The wgt fraction of ether is %5.3e \n',y(4))
______________________________________________________________________________________________

Here is the response in the Command Window.
______________________________________________________________________________________________

   Darin Ridgway
   ChE XXX
   July 8, 1998
   Example Problem 2
 

   The flowrate of livers is 1350.0 lbs/hr
   The wgt fraction of inerts is 5.211e-001
   The wgt fraction of oil is 2.019e-002
   The wgt fraction of ether is 4.587e-001

=============================================================================================================