Matlab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 41

MATLAB

Operators

+ Plus; addition : Colon


- Minus; , Comma
subtraction ; Semicolon
*Scalar and matrix & AND
multiplication | OR
/ division ~ NOT
MATLAB as a calculator
At x=1
Find the value of

𝑥2
y = (ln(x+2)+ 4𝑥 + sin π )(𝑒 /6x)
Help :( For more info type help followed by the name of
the function).
Who :(Returns a list of all variables in the current
workspace).
whos :(Returns the same list with more detailed
information about each variable.
Clear :(Delete the workspace).
Clc :(Delete the command window).

Basic commands
3 6
A= .
1 9
4
B=
7
C= 4 7 5 1

D= 3 5 7 9

Matrices and Vectors


A’ max(B).
C(1,3). B(1,1)=0.
E=A(1,:) w=A(:,2).
F=[A B]. F(:,[1 3])=[]
A*A = A^2. A.*A. =A.^2.
X=eye (4,4). Y=zeros(3,2). Or ones(3,2).
A=rand() triu(A)
tril(A)
plotting

2D: 3D:
(plot (x,y), (surf(x,y,z),
ezplot(f,[xi xf]) mesh(x,y,z))
2D plot
2D plot
2D EZplot
3D surf
3D mesh
For : Loop Control

While : Loop Control

If : Conditional control

Controls
FOR Loop
A program to print the numbers from 1 to 10
A program to calculate the sum of squares of numbers from 1 to n.
While Loop
A program to print the numbers from 1 to 10
A program to display powers of 2 till x=15 (f(x)=2x)
IF(Conditional control)
A program to make a discount of 20% if the no. of books are greater than
5,the cost of the book is 25.
How to write functions
tank draining
a) The tank is initially half full with ℎ 0 = 2 𝑚
𝑄𝑖𝑛 = 0.065 𝑚3 /𝑠𝑒𝑐
𝐴𝑤 = 𝜋 𝑚 2
𝐴ℎ = 0.0025 𝜋 𝑚2 .
t= 0:4000s
Determine ℎ(𝑡) and find its steady state
value numerically.

𝒅𝒉 𝑨𝒉 𝑸𝒊𝒏 𝒕
=− 𝟐𝒈𝒉 +
𝒅𝒕 𝑨𝒘 𝑨𝒘
physics
The angle y of an undamped pendulum with a driving force sin(5𝑡)
satisfies the differential equation:
𝒚′′ = − 𝒔𝒊𝒏 𝒚 + 𝒔𝒊𝒏 𝟓𝒕 .

Let the initial conditions be 𝒚 𝟎 = 𝟏, 𝒚′ 𝟎 = 𝟎


, t=0:20.
Transform the second order differential equation into a
system of two first order differential equations:
𝒚′′ = − 𝒔𝒊𝒏 𝒚 + 𝒔𝒊𝒏 𝟓𝒕
Let 𝒚𝟏 = y
Let 𝒚𝟏 ′ = 𝒚𝟐 (1)
Then, y′ ′ = 𝒚𝟏 ′ ′ = 𝒚′𝟐 = − 𝒔𝒊𝒏 𝒚𝟏 + 𝒔𝒊𝒏(𝟓𝒕) (2)
Euler function(Numerical method)
𝑦 ′ = 𝑦 − 𝑦𝑒 𝑥 ,y(0)=1, h=0.1, 𝑥𝑓 = 0.5
Simulink is used to model systems and test them,
as it can simulate a system to see how it
behaves without writing thousands of lines code

Simulink
s

You might also like