Professional Documents
Culture Documents
MATLAB - Tutorial On Numerical Analysis
MATLAB - Tutorial On Numerical Analysis
a
b(a ) c1 c2e
Nonstiff solvers
ode23: an explicit Runge-Kutta (2,3) formula i.e.
Bogacki-Shampine pair
ode45: an explicit Runge-Kutta (4,5) formula i.e.
Dormand-Prince pair
ode113: Adams-Bashforth-Moulton PECE solver
Stiff solvers
ode15s, ode23s, ode23t and ode23tb
Generic Syntax for ODE Solver
Output:
% function declaration
>> function integrnd_out = integrnd (x,y)
>> integrnd_out = x*sin(x) + y*cos(y);
>> syms a x
>> fx = sin (a*x)
>> dfx = diff(fx)
>> dfx = cos (a*x)*a
% with respect to a
>> dfa = diff(fx, a)
>> dfa = cos (a*x)*x
Variables
function yp = myfunction(y)
%The function you want to check.
global ALPHA
yp = [y-ALPHA*y];
Global Variables
Then in the command prompt
enter the statements
global ALPHA
ALPHA = 0.01
y=myfunction(1:10);
• pi: 3.1415926535897...
Operators
Operator Description
+ Addition
- Subtraction
.* Multiplication
./ Right division
.\ Left division
+ Unary plus
Arithmetic Operators
Operator Description
- Unary minus
: Colon operator
.^ Power
.' Transpose
' Complex conjugate transpose
* Matrix multiplication
/ Matrix right division
\ Matrix left division
^ Matrix power
Arithmetic Operators
/ Slash or matrix right
division.
B/A is roughly the same
as B*inv(A). More
precisely, B/A = (A'\B')'.
Operator Description
Operator Description
~= Not equal to
Logical Operators
Perform element-
wise logical
operations on their
inputs to produce a
like-sized output
array.
Element-Wise Operators and Functions
A and b must
have equal
dimensions,
with each
dimension
being the
same size.
Short-Circuit Operators
if ((A==10)||(B==20))
…..
end
Flow Control
switch can
handle
multiple
conditions in a
single case
statement by
enclosing the
case
expression in a
cell array.
Flow Control
Infinite loop
Exit a while loop at any time using the break statement.
Flow Control
save
•Saves workspace variables on disk.
•As an alternative to the save function,
select Save Workspace As from the File
menu in the MATLAB desktop, or use the
Workspace browser.
File Handling
Syntax
save
save filename
save filename var1 var2 ...
save('filename', ...)
Description
save by itself, stores all workspace variables in a
binary format in the current directory in a file
named matlab.mat. Retrieve the data with load.
File Handling
Description
load - loads all the variables from the MAT-file matlab.mat,
if it exists, and returns an error if it doesn't exist.
load filename X Y Z ... loads just the specified variables
from the MAT-file. The wildcard '*' loads variables that
match a pattern (MAT-file only).
File Handling
Description
M = dlmread(filename,delimiter) reads numeric data from
the ASCII delimited file filename, using the specified
delimiter.
default delimiter - comma (,)
'\t‘ - tab
M = dlmread(filename,delimiter,R,C) reads numeric data
from the ASCII delimited file filename, using the specified
delimiter.
The values R and C specify the row and column where the
upper-left corner of the data lies in the file. R and C are zero
based so that R=0, C=0 specifies the first value in the file,
which is the upper left corner.
File Handling