Lecture (6)

25
Lecture (6) Programming (3) Eng. Osama Talaat 1

description

Lecture (6). Programming (3) Eng. Osama Talaat. Announcement. Previous lecture videos are available at the course link. Extra Materials. Survey. Password. Enter the password. If the entered password is ‘a13’, display a welcome message ‘Welcome Osama’. - PowerPoint PPT Presentation

Transcript of Lecture (6)

Page 1: Lecture (6)

1Lecture (6)

Programming (3)Eng. Osama Talaat

Page 2: Lecture (6)

2 Announcement Previous lecture videos are available

at the course link. Extra Materials. Survey.

Page 3: Lecture (6)

3 Password Enter the password. If the entered password is ‘a13’, display

a welcome message ‘Welcome Osama’. If the entered password is ‘com’, display

a welcome message ‘Welcome Hamdy’. If the entered password is ‘t10’, display

a welcome message ‘Welcome Mona’. Else, Display an error message ‘Wrong

password’. Give the user only 5 unsuccessful trails.

Page 4: Lecture (6)

4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc;

for k=1:5pass = input('Please enter password: ','s');if strcmp(pass,'a13') disp('Welcome Osama') breakelseif strcmp(pass,'com') disp('Welcome Hamdy') breakelseif strcmp(pass,'t10') disp('Welcome Mona') breakelse disp(['Wrong password, trial ' num2str(k) ' of 5'])end

end

What it wrong ??

Break & Continue

Ctrl + C

Page 5: Lecture (6)

5 Animation%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; [x,y]=meshgrid(-2:0.1:2);z=x.*exp(-x.^2-y.^2);surf(x,y,z)

Page 6: Lecture (6)

6 View Change the angle of view for a figure. Get the current angles:

>> [a,b]=view

a =

-37.5000

b =

30

Page 7: Lecture (6)

7 Animation%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; [x,y]=meshgrid(-2:0.1:2);z=x.*exp(-x.^2-y.^2);surf(x,y,z)

for az=-37.5:7.5:37.5+360

view(az,30)end

Page 8: Lecture (6)

8 Pause Pause for n seconds:

>> pause(n)

Pause till you press any key>> pause

Page 9: Lecture (6)

9 Animation%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; [x,y]=meshgrid(-2:0.1:2);z=x.*exp(-x.^2-y.^2);surf(x,y,z)pause(1)for az=-37.5:7.5:37.5+360 pause(0.2) view(az,30)end

Page 10: Lecture (6)

10 For inside for%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear all; close all; clc; a=randi(30,40,20); c=0; for i=1:size(a,1) for j=1:size(a,2) if(a(i,j)==3) c=c+1; end endend disp('The number of accurance of 3s:'); disp(c)

Page 11: Lecture (6)

11 Game The program generate a random integer number

from 0:15. The user guess that number, if he was wrong

the program will till him that his guess is lower or greater than the number.

Then the user try again, till he give the right guess.

The program will display a congratulation message and the number of trials he took.

Page 12: Lecture (6)

12

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc;

n=randi(15);

trials=0;

g=-1;

while(g~=n)

g=input('Enter your guess between 1-15: ');

if g>n

disp('Your guess is greater, try again!')

trials=trials+1

elseif g<n

disp('Your guess is smaller, try again!')

trials=trials+1

else

disp('Congratulations, your guess is right!')

trials=trials+1

end

end

Page 13: Lecture (6)

13 While Loopwhile conditions

________________________

______ Commands ________

________________________

end

________________________

______ Commands ________

________________________

Page 14: Lecture (6)

14 While & Forfor x=1:2:100

______________________________ Commands ________________________________

end

----------------------------

x=1;

while x<=100______________________________ Commands ________________________________ x=x+2;

end

Page 15: Lecture (6)

15 15-Min Break6

7

8

121

2

3

4

5

9

10

11

Survey

Page 16: Lecture (6)

16 Functions To run any script (plotsin.m for example):

Run. F5 In command window >> plotsin

Create a function:

function plotsin(inputs)______________________________ Commands ________________________________

The file and the functions names must be the same.

The file must be saved in the current directory.

Page 17: Lecture (6)

17 Functionsfunction plotsin(n,f,A)

if n>0 %Check positive number of cycles

x=[0:0.1:2*pi*n/f];

y=A*sin(f*x);

plot(x,y)

xlabel('x')

ylabel([num2str(A) 'sin(' num2str(f) 'x)'])

title('Sine Curve'); grid

disp('The amplitude values are: '); disp(y)

else

disp('The number of cycles must be positive')

end

disp('Program Terminated !!')

Page 18: Lecture (6)

18 Log of any base Create a function to calculate the log:

Create new script called logn:

function logn(x,n)

log(x)/log(n) Try in the command editor:

>> logn(27,3)

ans =

3.0000

Page 19: Lecture (6)

19 Log of any base Try in the command editor:

>> a=logn(27,3)

Error using logn

Too many output arguments.

Create an output for the function:function y=logn(x,n)

y=log(x)/log(n);

Check wrong values:function y=logn(x,n)

if n<=0

disp('The base must be positive')

end

y=log(x)/log(n);

Page 20: Lecture (6)

20 Log of any basefunction y=logn(x,n)

if n<=0

disp('The base must be positive')

return

end

y=log(x)/log(n);

--- OR ---

function y=logn(x,n)

if n<=0

error('The base must be positive')

end

y=log(x)/log(n);

Page 21: Lecture (6)

21 Insert into a vectorfunction out=insertv(in,new,n)

if size(in,1)~=1|size(new,1)~=1

error('Works only with vectors')

end

if n<=0

error('The index must be positive')

end

out=[in(1:n-1) new in(n:end)];

Page 22: Lecture (6)

22 Open Selection You can open the code of some functions of

MATLAB. You can also change in it and save it for

yourself or publish it with some rules. Type any function in the command window. Select it. Right click. Open Selection. Its code will appear in the editor where you

can view, change and save as ...

Page 23: Lecture (6)

23 MATLAB Tips !! Tic ... Toc%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; disp('Press any key to start the stop watch!')pauseticdisp('Press any key to stop!')pausetoc

Page 24: Lecture (6)

24 Close MATLAB X button. Code

>> exit

>> quit

Ctrl + Q

Page 25: Lecture (6)

25 GOOD LUCKTo be continued in the next lecture …