Introduction To Matlab: Hasitha Nayanajith
Introduction To Matlab: Hasitha Nayanajith
HASITHA NAYANAJITH
Department of Mechanical Engineering
ATI- Labuduwa
What is Matlab
MATLAB (for matrix laboratory) is a numerical
computing environment and fourth-generation
programming language. Developed by MathWorks,
MATLAB allows matrix manipulations, plotting
of functions and data, implementation of algorithms,
creation of user interfaces, and interfacing with
programs written in other languages,
including C, C++, and Fortran.
Desktop Tools (Matlab v6)
• Command Window
– type commands
• Workspace
– view program variables
– clear to clear
– double click on a variable
to see it in the Array Editor
• Command History
– view past commands
– save a whole session using
diary
Operators (arithmetic)
+ addition
- subtraction
* multiplication .* element-by-element mult
/ division ./ element-by-element div
^ power .^ element-by-element power
‘ complex conjugate .‘ transpose
transpose
Operators (relational, logical)
== equal pi 3.14159265…
~= not equal j imaginary unit, 1
< less than i same as j
<= less than or equal
> greater than
>= greater than or equal
& AND
| OR
~ NOT
EXAPLE -01
• a=10
• b=5
+ addition
- subtraction
* multiplication
/ division
^ power
• a*5+(3*b)*2((b-6)^(10/b))
Useful Notations
• >> pi
• >> format long
• >> format short
• e=exp(1)
• sin(a), cos(a), tan(a)
• asin(a), acos(a), atan(a)
• sinh(a), cosh(a), tanh(a)
• asinh(a), acosh(a), atanh(a)
• log2(e)
• log(e)
• log10(e)
• c =0.5+0.866025i
• abs(c)
• angle(c)
• real(c)
• imag(c)
Vectors
• v = [ 1 3, sqrt(5)]
• v3 = [3 +4 5]
• v + v3
• v4 = [v , v3]
• sort(v4)
• v4(2)
• v4(2)=5
Array
• 1:4
• 1:2:10
Plot a Graph
y yellow . point
m magenta o circle
c cyan x x-mark
r red + plus
g green - solid
b blue * star
w white : dotted
k black -. dashdot
-- dashed
Matlab Graphics
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the
Sine Function')
Multiple Graphs
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on
plot(t,y1,'go',t,y2,'rx')
Graph Functions (summary)
• A=[1 2 3 6 2 12 8 1 25 2]
• max(A)
• min(A)
• sort(A)
• mean(A)
• median(A)
• std (A)
single 1st-order ode
x'(t)=sin(tx(t)) with x(0)=1 for 0 < t < 20.
function xpr=firstode(t,x);
%This M-file is used with ode45 to
solve the differential equation
% x'(t)=sin(tx(t))
%
xpr=sin(t*x);
» [t,x]=ode45('firstode',[0,20],1);
» plot(t,x)
» grid on
» title('Solution to x''=sin(tx), x(0)=1')
• Thakyou