Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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)

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