eng.mohammed elasmersite.iugaza.edu.ps/masmar/files/matlab_lab_3.pdf · add your company slogan...

47
LOGO Feb,2012 Eng.Mohammed Elasmer

Upload: others

Post on 06-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

LOGO

Feb,2012

Eng.Mohammed Elasmer

Page 2: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

2

MatLab :Matrix Laboratory

Page 3: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

3

Page 4: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Page 5: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

5

14

Compute:

e and 382801 to 15 digits .Which is bigger?

>> format long; [exp(14), 382801*pi]

ans =

1.0e+006 *

1.202604284164777 1.202604809386827

The second number is bigger.

Page 6: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

6

The fractions : 2709/1024 , 10583/4000 , 2024/765

which of these is the best approximation to 7

[2709/1024, 10583/4000, 2024/765, sqrt(7)]

ans =

2.645507812500000 2.645750000000000 2.645751633986928 2.645751311064591

The third, that is, 2024/765, is the best approximation

Page 7: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

7

Solve:

+ +

+ 1

6 3

3x 4y 5z = 2

2x 3y 7z

x y z

[x, y, z] = solve('3*x + 4*y + 5*z = 2',

'2*x - 3*y + 7*z = -1', 'x - 6*y + z = 3',

'x', 'y', 'z')

x =241/92

y =-21/92

z =-91/92

Page 8: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

8

2 2simplify: cos sinx x

syms x y

simplify((cos(x)).^2-(sin(x)).^2)

ans = 2*cos(x)^2-1

Page 9: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

randint>> help randint

RANDINT Generate matrix of uniformly distributed

random integers.

>> randint(1,4)

ans = 0 0 1 1

>> randint(1,4,[-1 1])

ans = -1 0 1 -1

>> randint(2,4,[10 12])

ans = 10 12 11 11

12 12 12 12

Page 10: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

10

Figure Palette (subplot)

Page 11: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

11

Page 12: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

12

Solving Equation

2x 2x 4 = 0 >> solve('x^2 - 2*x - 4 = 0')

ans = 5^(1/2)+1

1-5^(1/2)

>> double(ans)

ans = 3.2361

-1.2361

% or

>> roots([1 -2 -4])

ans =

3.2361

-1.2361

Page 13: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

13

Loops

10! 10*9*8*...*2*1

f = 1;

for n = 2:10

f = f*n;

end

f

f = 3628800>> factorial(10)

ans =3628800

Page 14: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

14

While

i = 1

i = 2

i = 3

i=0

while i<3

i=i+1

end

Page 15: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

15

f = 1;

for n = 2:10

f = f*n;

pause

f

end

Pause in Loops

Page 16: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

16

Break

y=0;

while y>0

A=input('enter the value of A= ')

y=y+A

if y>10;

break

end

end

Page 17: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

17

Loopsx=[ ];

for i=1:4

x=[x,i^2]

end

x = 1

x = 1 4

x = 1 4 9

x = 1 4 9 16

Page 18: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

18

Compute the absolute value of a real number

x=input('enter the number= ');

if x >= 0

y = x;

else

y = -x;

end

If

Page 19: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

19

If

function y = signum(x)

if x > 0

y = 1;

elseif x == 0

y = 0;

else

y = -1;

end

Page 20: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

5

1

If : [2, 2,3,1,8]

and ( )

Write a Matlab code to get the value of y

n

x

y x n

x=[2 2 3 1 8];

y=0;

for i=1:5;

y=x(i)+y;

end

or

>> sum(x)

ans= 16

Page 21: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

21

5

1

Given: [2, 2,3,1,8]

and ( ) ( ) * , k=1,2,3,4,5

Write a Matlab code to get the value of y

n

x

y k x n k

x=[2,2,3,1,8];

y=zeros(1,5);

for k=1:5;

for n=1:5;

y(k)=y(k)+x(n)*k;

end

end

y = 16 32 48 64 80

Page 22: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

22

2015

1510

100

2

0

5.0

][

n

n

n

nx

x=0:0.1:20;

y=0.5*(x<10)+0*(x>=10 & x<15) -2*(x>=15 & x<=20);

plot(x,y)

0 10 20-2

-1

0

1

Page 23: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

23

15 9

[ ] 5 0 9

2 0

x

f x x

x

x=-10:0.01:30 ;

for k=1:length(x)

if x(k)>=9

y(k)=15;

elseif x(k)>=0

y(k)=5;

else

y(k)=2;

end

end

plot(x,y)-10 0 10 20 300

5

10

15

Page 24: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

24

theta=320;

if(theta>=0 & theta<90)

y=1;

elseif(theta>=90 & theta<180)

y=2;

elseif(theta>=180 & theta<270)

y=3;

else

y=4;

end

y

Page 25: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

25

Switch …case

Your lost

angle = 80;

switch angle

case 0

disp('East')

case 90

disp('North')

case 180

disp('West')

case 270

disp('South')

otherwise

disp('Your lost')

end

Page 26: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

26

< less than

> greater than

<= less than or equal

>= greater than or equal

== equal

~= not equal

Page 27: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

27

>> x=[1 0 1 1 0];

>> y=[0 1 1 0 1];

>> x&y

ans = 0 0 1 0 0

>> x|y

ans = 1 1 1 1 1

>> ~x

ans = 0 1 0 0 1

>> xor(x,y)

ans = 1 1 0 1 1

& and | or

~ not xor

Page 28: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Let A=[5 17 -3 8 0 -1 12 15 20 -6 6 4 -7 16].

write code that doubles the element that are :

positive and are divisible by 3 or 5,

and raise to the power of 3 the element that are:

negative but greater than -5

Hint

Use OR | , AND & , rem(a,b)

Page 29: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Let y=[5 17 -3 8 0 -1 12 15 20 -6 6 4 -7 16].

write code that doubles the element that are :

positive and are divisible by 3 or 5,

and raise to the power of 3 the element that are:

negative but greater than -5

Hint

Use OR | , AND & , rem(a,b)

y = [5 17 -3 8 0 -1 12 15 20 -6 6 4 -7 16].

out=[10 17 -27 8 0 -1 24 30 40 -6 12 4 -7 16].

Example:

Page 30: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

y=[5 17 -3 8 0 -1 12 15 20 -6 6 4 -7 16];

A=y;

for x=1:length(y);

if y(x)>=0&rem(y(x),3)==0|rem(y(x),5)==0

y(x)=2*y(x);

elseif y(x)<0&y(x)>-5

y(x)=y(x)^3;

end

end

[A;y]

Page 31: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Let y=[5 17 -3 8;0 -1 12 15;20 -6 6 4];

write code that doubles the element that are :

positive and are divisible by 3 or 5,

and raise to the power of 3 the element that are:

negative but greater than -5

Hint

Use OR | , AND & , rem(a,b)

Repeat if :

Page 32: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

y=[5 17 -3 8;0 -1 12 15;20 -6 6 4];

A=y;

[a,b]=size(y);

for i=1:a;

for j=1:b;

if y(i,j)>=0&rem(y(i,j),3)==0|rem(y(i,j),5)==0

y(i,j)=2*y(i,j);

elseif y(i,j)<0&y(i,j)>-5

y(i,j)=y(i,j)^3;

end

end

end

A

y

Page 33: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

33

>> help bi2de

BI2DE Convert binary vectors to decimal

numbers.

>> B = [0 0 1 1; 1 0 1 0]

B =

0 0 1 1

1 0 1 0

>> D = bi2de(B,'left-msb')

D =

3

10

bi2de

Page 34: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

34

>> B = de2bi([3;10],'left-msb')

B = 0 0 1 1

1 0 1 0

de2bi

Page 35: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

35

dec2hex & hex2dec >> dec2hex(12)

ans =C

>> dec2hex(18)

ans =12

>> dec2hex(26)

ans =1A

>> hex2dec('1A')

ans =26

>> hex2dec('12')

ans =18

Page 36: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

>> help mod

MOD Modulus after division.

Modulus

>> mod(9,3)

ans = 0

>> mod(10,3)

ans =1

>> mod(11,3)

ans =2

>> mod(3,9)

ans =3

Page 37: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

37

Convert Point P(1,3,5) form Cartesian to spherical

( , , ) (5.91,32.31 ,71.56 )o or

>> [phi,theta,r]=cart2sph(1,3,5)

phi =1.2490

theta =1.0069

r =5.9161

cart2sph

Page 38: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

38

Convert Point P(1,3,5) form Cartesian to spherical

Sph2cart(phi,theta,r)

>> [x,y,z]=sph2cart(1.249,1.0069,5.916)

x =1.0001

y = 2.9997

z =5.0001

Page 39: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

39

pol2cart(phi,rho,z)

>> [X,Y,Z] = pol2cart(pi/6,2,5)

X =1.7321

Y =1.0000

Z =5

Convert Point P(2,pi/6,5) form cylindrical to Cartesian

Page 40: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

cov Covariance matrix

max Largest elements in array

mean Average or mean value of array

median Median value of array

min Smallest elements in array

mode Most frequent values in array

std Standard deviation

var Variance

Descriptive Statistics

Page 41: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

>> x=[60 70 62 73 75 70 80 90 99 100];

>> var(x)

ans =201.6556

>> std(x)

ans =14.2005

>> mean(x)

ans =77.9000

Page 42: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

>> median([1 2 3 4 5])

ans =3

>> median([1 2 3 4 5 6])

ans =3.5000

Page 43: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

>> A=[1,2,3;4,5,6;7,8,9];

>> xlswrite('data.xls',A)

xlswriteWrite Microsoft Excel spreadsheet file

Page 44: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Read Microsoft Excel spreadsheet file

xlsread

>> y=xlsread('data.xls')

y = 1 2 3

4 5 6

7 8 9

Page 45: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Database>>E(1)=struct('Name','Ali','Age','24','Phone','12355');

>>E(2)=struct('Name','Ahmed','Age','23','Phone','99324');

Page 46: Eng.Mohammed Elasmersite.iugaza.edu.ps/masmar/files/MATLAB_LAB_3.pdf · Add your company slogan LOGO 6 The fractions : 2709/1024 , 10583/4000 , 2024/765 which of these is the best

Add your company slogan

LOGO

Database>>E(1)=struct('Name','Ali','Age','24','Phone','12355');

>>E(2)=struct('Name','Ahmed','Age','23','Phone','99324');