[go: up one dir, main page]

0% found this document useful (0 votes)
57 views8 pages

Matlab Tricks

- The document provides Matlab tricks and commands for cleaning the workspace and command window, using semicolons, forming polynomials from roots, working in the command window and scripts, trigonometric functions and inverses, exponential and logarithmic functions, vectors, matrices, for loops, and if/while loops. - Key Matlab commands covered include clear, clc, poly, roots, sin, cos, exp, log, size, transpose, submatrices, and loops. - Matrices can be multiplied, elements can be accessed via indexing, and operations like inverse, transpose, and extracting submatrices are demonstrated.

Uploaded by

Abdul Majeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views8 pages

Matlab Tricks

- The document provides Matlab tricks and commands for cleaning the workspace and command window, using semicolons, forming polynomials from roots, working in the command window and scripts, trigonometric functions and inverses, exponential and logarithmic functions, vectors, matrices, for loops, and if/while loops. - Key Matlab commands covered include clear, clc, poly, roots, sin, cos, exp, log, size, transpose, submatrices, and loops. - Matrices can be multiplied, elements can be accessed via indexing, and operations like inverse, transpose, and extracting submatrices are demonstrated.

Uploaded by

Abdul Majeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Matlab tricks

 Clear all (to clean workspace)

 Clc (to clean command window)

 Use of Semi colon ‘’;’’ at the end of command

(to perform the command but not show in the command window) 0r (to hide)

 To form polynomial from roots


Example
>> x=[1 2 3] press enter
Then
>> poly (x) then press enter
You get
Ans = 1 -6 11 -6 ( which means x^3 -6x^2 +11x-6 )

Work in command window

x=5

x=

>> y=[1 2 3]

y=

1 2 3

>> poly(y)

ans =

1 -6 11 -6

>> roots(y)

ans =

-1.0000 + 1.4142i
-1.0000 - 1.4142i
Work in SCRIPT
clc
clear all
x=[1 2 3]
poly(x)
roots(x)

out put in C W
x=

1 2 3

ans =

1 -6 11 -6

ans =

-1.0000 + 1.4142i
-1.0000 - 1.4142i

Trignomatric functions and their inverses

Matlab consider the command of these function in radian

Examples:

A=Sin(90)
= 0.8940
B=Sin (pi/2)
=1

Inverse
(using a in start of trignomatric function which shows the inverse )

I= a cos(1)
=0

Square root

J=sqrt(16)
=4

Absolute value
K=abs(-8.4)
=8.4
Sign function (with out put just 0,1,-1)

L=sign(-8)
=-1
M=sign(0)
=0
L=sign(8)
=1

Conjugate of a function:

L=cong(8+3i)
=8-3i

Real or imag part of a complex number

p=real(8+3i)
=8
q=imag(8+3i)
=3

exponentional function:
r=exp(1)
= 2.7183

power of a number
s=5^3
=125

logarithm function and logarithm function with base 10


t=log(exp(1))
=1

u= log10(exp(1))
=0.4343
v= log10(100)
=2

Angle of a complex number

w=angle(8+3i)
= 0.3588

formatting of numbers
use percentage sign % and the write something like heading,to remember something ,
for beauty , to point out , or some values or function names or method, for remind us,
not a part of code.

Round off a number


x=round(5.678)
=6

ceil a number (to get next upper number)

x=ceil(5.378)
=6
floor a number (to get next lower number)

x=floor(5.378)
=5
fix a number (to get next number toward zero)

x=fix(-5.378)
=-5

Formate short (to get result in four decimal number)

format short
a = (5.123456/6.123456)

a =0.8367

Formate short e (to get result in four decimal digits bet 1 and 10
multiplied by some power of e)
Means to get standard form
format short e
a = (5.123456/6.123456)
a =8.3669e-01

Formate long (to get result in full decimal digits)

format long
a = (5.123456/6.123456)
= 0.836693527315294

Formate long e (to get result in full decimal digits bet 1 and 10
multiplied by some power of e)

format long e
a = (5.123456/6.123456)
= 8.366935273152939e-01

Formate bank (to get result in two decimal digits)

format bank
a = (5.123456/6.123456)
= 0.84

Vectors
A matrix with one row is called vector in matlab.

Multiplication of vectors
c1=[1 2 3 4];
c2=[4 5 6];
c=conv(c1,c2);

4.00 13.00 28.00 43.00 38.00 24.00

Multiplication of matrices
c3=[1 2 3; 4 5 6; 7 8 9]
c4=[1 2 3; 4 5 6; 7 8 9]
c=c3*c4
c3 =

1.00 2.00 3.00


4.00 5.00 6.00
7.00 8.00 9.00

c4 = 1.00 2.00 3.00


4.00 5.00 6.00
7.00 8.00 9.00

c = 30.00 36.00 42.00


66.00 81.00 96.00
102.00 126.00 150.00

Multiplication of matrix with itself


c3=[1 2 3; 4 5 6; 7 8 9]
c4=[1 2 3; 4 5 6; 7 8 9]
c=c3.*c3
c3 =

c3 =

1.00 2.00 3.00


4.00 5.00 6.00
7.00 8.00 9.00

c4 =

1.00 2.00 3.00


4.00 5.00 6.00
7.00 8.00 9.00

c =

1.00 4.00 9.00


16.00 25.00 36.00
49.00 64.00 81.00

inverse of matrix
c3=[1 2 3; 4 5 6; 7 8 9]
c=inv(c3)

c =

3152519739159346.00 -6305039478318692.00 3152519739159346.00


-6305039478318695.00 12610078956637386.00 -6305039478318692.00
3152519739159348.00 -6305039478318693.00 3152519739159346.00

For loops ( to repeat run something many time)


For and end work together with step size 1 and 0.5 or something

Example step size 1

for i=0:2
display('boundary layer')
end

boundary layer
boundary layer
boundary layer

Example step size 0.5


for i=0:0.5:2
display('boundary layer')
end

boundary layer
boundary layer
boundary layer
boundary layer
boundary layer

lecture 4 end
For loops ( to repeat run something many time with step
size show)
for i=0:0.5:2
A=i
display('boundary layer')
A =0
boundary layer
A =0.5000
boundary layer
A = 1
boundary layer
A = 1.5000
boundary layer
A =2
boundary layer

if loops with end (if the if condition is true the command run other wise
not)
if 2<5;
display('boundary layer');
end ;

if loops with end and or (if the one of the if condition is true the
command run other wise not)
if 9>10 || 10>9;
display('boundary layer');
end ;

if loops with end and and (if the both of the if conditions are true the
command run other wise not)
if 9>10 && 10>9
display('boundary layer')
end

while (run till condition satisfied)


a=1;
while a<5
display('i am running')
a=a+1
end

order of a matrix
v=[ 1 2 3 ]
size(v)

v =1 2 3

ans =1 3
2 example
v= [1 2 3;4 5 6]
size (v)
v = 1 2 3
4 5 6
ans = 2 3

Transpose of a matrix use just ‘


v= [1 2 3;4 5 6 ]
v=v'
v = 1 2 3
4 5 6

v = 1 4
2 5
3 6

v= [1 2 3;4 5 6]
v (1,:) means show just 1st row

v = 1 4
v = 1 2 3
4 5 6

ans =1 2 3

v= [ 1 2 3;4 5 6]
v (:,2) means show just 2nd column
v = 1 2 3
4 5 6
ans =
2
5
Submatrix of a large matrix
A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A(2:3,2:3)
A = 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

ans = 6 7
10 11

You might also like