Lecture (6)

Post on 24-Feb-2016

62 views 0 download

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)

1Lecture (6)

Programming (3)Eng. Osama Talaat

2 Announcement Previous lecture videos are available

at the course link. Extra Materials. Survey.

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.

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

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)

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

>> [a,b]=view

a =

-37.5000

b =

30

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

8 Pause Pause for n seconds:

>> pause(n)

Pause till you press any key>> pause

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

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)

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.

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

13 While Loopwhile conditions

________________________

______ Commands ________

________________________

end

________________________

______ Commands ________

________________________

14 While & Forfor x=1:2:100

______________________________ Commands ________________________________

end

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

x=1;

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

end

15 15-Min Break6

7

8

121

2

3

4

5

9

10

11

Survey

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.

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 !!')

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

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);

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);

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)];

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 ...

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

24 Close MATLAB X button. Code

>> exit

>> quit

Ctrl + Q

25 GOOD LUCKTo be continued in the next lecture …