Solving Nonlinear Equation(s) in MATLAB, Study notes of Dynamics

This tutorial helps you use MATLAB to solve nonlinear algebraic equations of single or multiple variables. 2 Writing MATLAB functions. In order to use the ...

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

ekaatma
ekaatma ๐Ÿ‡บ๐Ÿ‡ธ

4.2

(34)

266 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHEE 222: PROCESS DYNAMICS AND NUMERICAL METHODS
Prepared by Kunal Karan 1
Solving Nonlinear Equation(s) in MATLAB
1 Introduction
This tutorial helps you use MATLAB to solve nonlinear algebraic equations of single or
multiple variables.
2 Writing MATLAB functions
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 - (a) inline command, and (b)
Matlab editor
2.1 The โ€˜inlineโ€™ command
The inline command can be used for simple, one-line functions. For example, to create
f(x) = x
3
- 5x
2
-x +2 :
>> f = inline(โ€˜x^3 -5*x^2 - x+2โ€™)
f =
Inline function:
f(x) = x^3-5*x^2-x+2
You can now evaluate the function value at any given x. For example, to evaluate the
function value at x = 4, simply type โ€˜f(4)โ€™ at Matlab command line.
EDU>> f(4)
ans =
-18
2.2 The MATLAB editor
The editor allows the user to write functions of any length and/or complexity.
1. Set the current working directory to your diskspace
e.g. โ€œc:\CHEE222\Matlab\Iamhappy\Temp\โ€
pf3
pf4
pf5

Partial preview of the text

Download Solving Nonlinear Equation(s) in MATLAB and more Study notes Dynamics in PDF only on Docsity!

Solving Nonlinear Equation(s) in MATLAB

1 Introduction

This tutorial helps you use MATLAB to solve nonlinear algebraic equations of single ormultiple variables.

2 Writing MATLAB functions

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 - (a) inline command, and (b) Matlab editor

2.1 The โ€˜ inline โ€™ command

The inline command can be used for simple, one-line functions. For example, to create f ( x ) = x (^3) - 5 x (^2) -x + 2 :

>> f = inline (โ€˜ x^ 3 -5x^2 - x + 2โ€™) _f = Inline function: f(x) = x^3-5x^2-x+_ You can now evaluate the function value at any given x. For example, to evaluate the function value at x = 4, simply type โ€˜ f (4)โ€™ at Matlab command line. EDU>> f(4) ans =

-

2.2 The MATLAB editor

The editor allows the user to write functions of any length and/or complexity.

  1. Set the current working directory to your diskspace e.g. โ€œc:\CHEE222\Matlab\Iamhappy\Temp\โ€
  1. (a) type \โ€edit funโ€ at the command prompt - enter yes to create file ALTERNATIVELY (b) go to โ€œFileโ€, select โ€œNewโ€; select โ€œM-Fileโ€. type the following: function y = fun ( x ) y = x^3 - 5 x^2 -x + 2 ; NOTE have chosen โ€˜: The filename and the function name should be the same. In the previous example, we fun โ€˜ as the filename and the function name.
  2. Save the file as โ€œ fun.m โ€ in the working directory
  3. MATLAB function FZERO 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 inline or m-file).

3.1 Using FZERO for a function defined by inline command

The following command solves the equationinitial guess of x = 4. y = f ( x ) = x^3 - 5 x^2 -x + 2 ;, starting from an

EDU>> fzero(f,4) MATLAB returns the answer: ans = 5. Changing the initial guess to x = 2 EDU>> fzero(f,2) gives ans =

f 2 ( x 1 , x 2 ) = 2 x 2 โˆ’ x^22 โˆ’ 3 x 1 x 2

f 1 ( x 1 , x 2 ) = x 1 โˆ’ 4 x 12 โˆ’ x 1 x 2

xo = [ 1 1 ]^ '

NOTE: In utilizing ROOTS function, all coefficients of the polynomial must bespecified.

e.g. f ( x ) = x^4 - 3 x^2 + 2. The function in the full polynomial form must be expressed as: f ( x ) = 1w x^4 - 0wx^3 +3w x^2 -0 w x + 2. Accordingly, the polynomial must be defined in MATLAB as follows: p = [1 0 -3 0 2]:

5 FSOLVE The MATLAB routine fsolve is used to solve sets of nonlinear algebraic equations usinga quasi-Newton method. The user must supply a routine to evaluate the function vector. Consider the following system of nonlinear equations, and solve for x 1 and x 2 :

The m-file used to solve the above problem using fsolve is:

which is placed in a m-file called nle.m. Enter the initial guess

Note: x (^) o is the TRANSPOSE of a row vector Now, solve with x = fsolve ( โ€˜ nleโ€™ ; x 0 )

which gives us the results x = [0_._ 25 0_._ 00] โ€™.