Title: Introduction to Matlab 7 Lesson IV
1Introduction to Matlab 7Lesson IV
- Marco Lattuada
- Swiss Federal Institute of Technology - ETH
- Institut für Chemie und Bioingenieurwissenschaften
- ETH Hönggerberg/ HCI F135 Zürich (Switzerland)
- E-mail lattuada_at_chem.ethz.ch
- http//www.morbidelli-group.ethz.ch/education/inde
x
2Example of Function
- The M-File the "mysum" function (saved in
mysum.m)
3Exercise
- Create a new function called 'my_second_function'
- Scope of the function
- Compute the function f(x) 2x2-exp(x)3
- Parameters of the function
- Input a generic array x with n components
- Output an array n?1 with the values of the
function in x - Evaluate the function for 40 x values ranging
from -5 to 5 - Plot f(x) versus x
4Exercise
- Create the function 'bisection_root' that
- Computes the zero of a function f(x) by the
'Bisection' method - The objective function is f(x) 2x2-exp(x)3
- The interval within the search for the zero is
carried out is given to the function as input
parameter (xmin, xmax) - The function returns as output the zero (x0) and
the corresponding value of the function (f(x0)) - Iterate the search for the zero until the search
interval is less than 10-4 - Provide to the function the desired precision e
- Add a preliminary check on the supplied
uncertainty interval - If f(xmin)f(xmax) gt 0 return an error (use the
command error)
- Compute x0
- Compute f(x0)
- Change the interval x1, x2
- Iterate 1-gt3 until (x2-x1) lt e
5How to Pass a Function to a Function
How can I use the function 'bisection_root' to
compute the root of a generic function fobj(x)?
6Simulation of a Batch Reactor
- Hp T const V const
- Variables CA, CB , CC
- Equations
R1 A ? 2B with R1 k1A R2 B ? C with R2 k2B
- Create the function 'batch_reactor' which
- Receives the time t and the column vector C with
the concentration of the three components - Returns a vector dCdt with the derivatives dCi/dt
- k1 and k2 are fixed values in the function
7Integration of Systems of ODEs
- Function call
- t, dydt ode15s (_at_fcn_dydt, t_grid, y_init)
- The function ' fcn_dydt' must have the following
header - function dydt fcn_dydt (t, y)
Let us use the built in Matlab function ode15s
8Simulation of a Batch Reactor
- Hp T const V const
- Variables CA, CB , CC
- Equations
R1 A ? 2B with R1 k1A R2 B ? C with R2 k2B
- Modify the function 'batch_reactor' so that
- It receives the time t and the column vector C of
the concentration of the three components - It returns a vector dCdt with the derivatives
dCi/dt - It receives k1 and k2 as input parameters
9Integration of Systems of ODEs
The new function header does not correspond to
that required for the subroutine ode15s function
dydt fcn_dydt (t, y)
Alternative call 1 t_out,C ode15s(_at_(t,
C) batch_reactor(t, C, k),t_in,C_init)
Alternative call 2 t_out,C
ode15s(_at_batch_reactor,t_in,C_init,,k)
Alternative call 3 global k t_out,C
ode15s(_at_batch_reactor2,t_in,C_init)
batch_reactor_an _at_(t, C) batch_reactor(t, C, k)
The value of k becomes part of the "body" of
batch_reactor_an
Function "handle"
10Anonymous Functions
11Input-output functions
- To enter date from the keyboard during the
execution of a program, use the command input - Variable_nameinput(Message)
- Stops the execution of the program until
something is typed in. The value that is typed in
is stored in the variable variable-name - To retrieve data from a file, there are different
possibilities - Load loads vectors and matrices from a binary
file - Dataload(file_name.ext)
- Will store the values in the file file_name.ext
in Data. Remember the values in the file need to
be stored in matrix form (some number of rows and
columns everywhere) and the file has to be ASCII
12Read from - and write to - a file
- fidfopen(filename.ext,w) creates a file
named filename.ext - or it overwrites it if already existing.
- Instread of w, the permission can be
r(read), ora (append) - A fread(fid) similar to load, reads data
and stores them in A - A fscanf(fid, format) reads formatted data
(specified by format) and stores it in A - tl fgetl(fid) reads one line from the file and
stores it in tl - fwrite(fid, A, precision) writes matrix A as a
column vector with specified precision - fprintf(fid,14.10e\n,a(1,1)) writes a(1,1) in
the file with specified format (in this case,
exponential notation) - Fclose(fid) closes file
File identifier
Permission
13Exercise
- Create the function 'my_derivative' that
- Computes sin(x) in x
- Computes the derivative of sin(x) in x
- Uses a step Dx 1e-8 to compute the derivative
- Compare the result produced by the function with
the analytical solution of the derivative - Provide the step Dx as an input parameter
- Select a value of Dx and run the function for x
00.1pi - Select x 1 and run the function of decreasing
Dx - Compare previous result with the analytical
solution - For which Dx the derivative is best approximated?
- Repeat points 5 to 7 for the function F(x) a
exp(-b x) c x2 where (a, b, c) (2, 3, -4)
14My Derivative