MATLAB Tutorial
MATLAB Tutorial
Annie Fournier-Gagnoud
Phelma 2016-2017
OUTLINE
● Introduction
● Programming with Matlab
Introduction
Algorithm
Recommandations
Matlab software
● Ordinary Differential Equations ODE
● Partial Differential Equations PDE
Calculations
…..
….. Calculation 1
.….
Calculation 2
…
Saving results ….
…
Graphic representation
MATrix LABoratory
Command window
Variable Window
Commands history
Getting started
Introduction
Matrices and Array
Graphics
Programming
…….
>>a=[1 4 8 ]
a=
1 4 8
>>b=[3 ; 2 ]
b=
3
2
>>c=pi
>>A=[1 1 1; 2 4 8; 3 9 27 ]
A=
1. 1. 1. Matrix definition
2. 4. 8.
Each componant is separated by space
3. 9. 27.
Each line by ;
>>A' '
1. 2. 3.
Transposed Matrix
1. 4. 9. ' is the tranposition operator
1. 8. 27.
>>a=[1 4 8 ]
>>b=[3 ; 2 ;5]
>>a*c
??? Undefined function or variable ‘c’
>>a*b
ans=54
>>A=b*a
A=
3 12 24 3 3
2 8 16 a 1 4 8 b 2 A b a 2 1 4 8
5 20 40
5 5
Arithmetic operators + - * /
--> b= [1 0 0]’;
transposed
--> x = A \ b
Resolution of A*x = b
x =
[] [ ]
1 1 2 3
Defining a vector V: V = −2 a Matrix A:A= 2 −3 5
3 4 −3 1
• Display the 2nd component of V
• Calculate norm of V (3 methods)
• Build the vector W such as W=A . V and G=V ^ W
•Calculate determinant of A
•Solve A X = V (2 methods)
A. Fournier-Gagnoud Numerical Methods 2016-2017 24
Matrix and Vectors building
>> A=[1 2 3;2 -3 5;4 -3 1]
>> V=[1 -2 3]’ or V=[1;-2;3]
>> A(2,3)
>> V(2) ans =
ans = 5
-2
>> A(:,1)
>> norm(V) ans =
>> sqrt(V’*V) 1
>> sqrt(dot(V,V)) 2
ans = 4
3.7417
>> A(2,:)
ans =
2 -3 5
>> W=A*V
W=
6
23
13
>> G=cross(V, W)
-30
-6
6
2 -1 0 0 .... 0
-1 2 -1 0 0
0 -1 2 -1 0
T= 0 0
0 -1
0 ... .. -1 2
binary operators
A<B
A>B
Operators : A && B
A <= B
A || B
A >= B
Arithmetic operators A == B
Logical operators A ~= B
Relational operators
Condition :
if
Enter
true i & j
i<j ?
no break instruction in a
block case as in C++!
A. Fournier-Gagnoud Numerical Methods 2016-2017 35
Loops
loop1.m
loop2.m
loop3.m sum1.m
What is calculated ?
|x| abs(x)
arctg x atan(x)
cos x cos(x)
exp x exp(x)
ln x log(x)
log x log10(x)
x sqrt(x)
y
C&
x x^y C++
i ≡ sqrt(-1.)
x = resolution(A, b)
A. Fournier-Gagnoud Numerical Methods 2016-2017 40
Writing a function
function linsolver linsolver is the principal function
A=[1 2 3;2 -3 5;4 -3 1]
calling the resolution function
b=[1 -2 3]'
[x, ier]=resolution(A, b)
if (ier)
error('Pb resolution');
end;
% solving a linear system Conditional instruction
function [x, res]=resolution(A, b) if (condition)
if (det(A)==0) bloc;
res=1; determinant else
x=0; bloc;
else end
res=0;
x=A\b; What happens if det(A)=0 ?
end
A. Fournier-Gagnoud Numerical Methods 2016-2017 41
Graphical functions
x = linspace(-50,50,100);
y = x*sin(x);
plot(x,y)
hold on
title('Graph of f=x*Sine')
xlabel(' x ') % x-axis label
ylabel('f(x)=x*sin(x)') % y-axis label
Example n°1
Example n°2
nb_lignes=1;
val_defaut={'293'};
txt = inputdlg('temperature' ,‘window_title, ...
nb_lignes, val_defaut);
T=str2double(txt) % convert a string into a real
Point in 2D coordinates
y disp(M);
Point in 2D coordinates
fich = fopen(nomfich);
if ( fich == -1 )
errordlg([‘Invalid file name : ' nomfich]); % !!does not stop
err = 1;
else
% reading the comment
comment = fscanf(fich, '%s', [1])
curve= curve.NP = 63
NP: 63
M: [1x63 struct] curve.M =
fich = fopen(nomfich);
if ( fich == -1 )
errordlg([‘incorrect file name : ' nomfich]); % !! Does not stop
err = 1;
else
% reading a comment
comment = fscanf(fich, '%s', [1])
% reading a comment
comment = fscanf(fich, '%s', [1])
function tab_point2
% reading points coordinates clc
for np=1:curve.NP nom='tab.dat'
n = fscanf(fich,'%d', [1]) [curve, ier]=reading(nom);
x = fscanf(fich,'%g', [1]) if (ier==1)
y = fscanf(fich,'%g', [1]) error(‘error reading’);
curve.M(n).x=x; end;
curve.M(n).y=y;
end; for i=1:curve.NP
err = 0; x(i)=curve.M(i).x;
end; y(i)=curve.M(i).y;
fclose(fich); end;
A. Fournier-Gagnoud plot(x, y)
Numerical Methods 2016-2017 51
MATLAB
Website:
http://www.mathworks.com/academia/student_center/t
utorials/starting_matlab.html
On this web site read the different tutorials
and stop after: Creating scripts with
MATLAB Editor/Debugger
Two important points:
Visualizingdata
Creating scripts with MATLAB Editor/Debugger