- •Чисельні методи Використання MatLab
- •Затверджено
- •Contents
- •The purpose and the contents of laboratory works
- •Laboratory work №1 Programming in the environment of Matlab
- •1.1 Purpose of the work
- •1.2 The tasks to laboratory work
- •The basic theoretical knowledge
- •Passing String Arguments to Functions
- •Constructing String Arguments in Code
- •The eval Function
- •Vectorization
- •Global Variables
- •Preallocation
- •Anonymous Functions
- •Switch and case
- •Continue
- •The individual task to laboratory work
- •1.5 The task for self-examination
- •1. For opening the editor of the programs in MatLab it is possible to execute:
- •Laboratory work №2 Solution of Nonlinear Equations by the Bisection method and Chord method
- •2.1 Purpose of the work
- •2.2 Tasks for laboratory work
- •2.3 The basic theoretical knowledge
- •2.3.1 Bisection method
- •Figure 2.1 – Bisection method
- •Chord method
- •Figure 2.4 – Chord method
- •2.3.3 Matlab function fzero and roots
- •2.4 Individual tasks
- •2.5 Control questions
- •Laboratory work №3 Solution of Nonlinear Equations by the newton method and simple iteratIvE method
- •Figure 2.1 – Newton method
- •Figire 2.2 - Dependence of the number of iterations on the accuracy of methods for the bisection (upper line) and the Newton method (bottom line)
- •2.3.2 The method of simple iteration
- •A sufficient condition for the convergence of the iterative process
- •Individual tasks
- •Laboratory work №3 Solution system of Linear Algebraic Equations
- •3.3.1 Direct methods
- •Inverse matrix:
- •3.3.2 Iterative methods
- •Condition number of a
- •3.4 Individual tasks
- •3.5 Control questions
- •Solving systems of nonlinear equations
- •5.1 Purpose of the work
- •5.2 Tasks for laboratory work
- •5.3 The basic theoretical knowledge
- •5.3.1 Newton method to solve systems of non-linear equations
- •5.3.2 Matlab function for Newton method for a system of nonlinear equations
- •5.3.3 The matlab routine function fsolve
- •Input Arguments
- •Individual tasks
- •5.5 Control questions
- •List of the literature
- •Appendix a.
- •Individual tasks to Lab number 1, 2
- •Appendex b. The task for self-examination to Lab number 1, 2
Laboratory work №1 Programming in the environment of Matlab
1.1 Purpose of the work
Acquisition of skills programming in the environment of Matlab
1.2 The tasks to laboratory work
To get acquainted with the maintenance of point 3.3 of methodical instructions and to execute examples of this point.
To carry out the individual task, variants are resulted in table 3.2
Save rezalts of the work (the programs, listing of calculation) at your personal file.
Draw up report.
The basic theoretical knowledge
MATLAB is a powerful programming language as well as an interactive computational environment. Files that contain code in the MATLAB language are called M-files. You create M-files using a text editor, then use them as you would any other MATLAB function or command.
There are two kinds of M-files:
scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace.
functions, which can accept input arguments and return output arguments. Internal variables are local to the function.
To view the contents of an M-file, for example, myfunction.m, use
>>type myfunction
Scripts
When you invoke a script, MATLAB simply executes the commands found in the file. Scripts can operate on existing data in the workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations. In addition, scripts can produce graphical output using functions like plot.
For example, create a file called magicrank.m that contains these MATLAB commands.
% Investigate the rank of magic squares
r = zeros(1,32);
for n = 3:32
r(n) = rank(magic(n));
end
r
bar(r)
Typing the statement magicrank causes MATLAB to execute the commands, compute the rank of the first 30 magic squares, and plot a bar graph of the result. After execution of the file is complete, the variables n and r remain in the workspace.
1.3.2 Functions
Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Functions operate on variables within their own workspace, separate from the workspace you access at the MATLAB command prompt.
A good example is provided by rank. The M-file rank.m is available in the directory
toolbox/matlab/matfun
You can see the file with
>>type rank
Here is the file.
function r = rank(A,tol)
% RANK Matrix rank.
% RANK(A) provides an estimate of the number of linearly
% independent rows or columns of a matrix A.
% RANK(A,tol) is the number of singular values of A
% that are larger than tol.
% RANK(A) uses the default tol = max(size(A)) * norm(A) * eps.
s = svd(A);
if nargin==1
tol = max(size(A)') * max(s) * eps;
end
r = sum(s > tol);
The first line of a function M-file starts with the keyword function. It gives the function name and order of arguments. In this case, there are up to two input arguments and one output argument.
The next several lines, up to the first blank or executable line, are comment lines that provide the help text. These lines are printed when you type
>>help rank
The first line of the help text is the H1 line, which MATLAB displays when you use the lookfor command or request help on a directory.
The rest of the file is the executable MATLAB code defining the function. The variable s introduced in the body of the function, as well as the variables on the first line, r, A and tol, are all local to the function; they are separate from any variables in the MATLAB workspace.
This example illustrates one aspect of MATLAB functions that is not ordinarily found in other programming languages – a variable number of arguments. The rank function can be used in several different ways.
>>rank(A)
>>r = rank(A)
>>r = rank(A,1.e-6)
Many M-files work this way. If no output argument is supplied, the result is stored in ans. If the second input argument is not supplied, the function computes a default value. Within the body of the function, two quantities named nargin and nargout are available which tell you the number of input and output arguments involved in each particular use of the function. The rank function uses nargin, but does not need to use nargout.
