250 likes | 364 Views
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’.
E N D
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’. • 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; for k=1:5 pass = input('Please enter password: ','s'); ifstrcmp(pass,'a13') disp('Welcome Osama') break elseifstrcmp(pass,'com') disp('Welcome Hamdy') break elseifstrcmp(pass,'t10') disp('Welcome Mona') break else disp(['Wrong password, trial ' num2str(k) ' of 5']) end end What it wrong ?? Break & Continue Ctrl + C
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)
View • Change the angle of view for a figure. • Get the current angles: >> [a,b]=view a = -37.5000 b = 30
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) foraz=-37.5:7.5:37.5+360 view(az,30) end
Pause • Pause for n seconds: >> pause(n) • Pause till you press any key >> pause
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) foraz=-37.5:7.5:37.5+360 pause(0.2) view(az,30) end
For inside for %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; a=randi(30,40,20); c=0; fori=1:size(a,1) for j=1:size(a,2) if(a(i,j)==3) c=c+1; end end end disp('The number of accurance of 3s:'); disp(c)
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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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
While Loop whileconditions ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________
While & For for x=1:2:100 ________________________ ______ Commands ________ ________________________ end ---------------------------- x=1; whilex<=100 ________________________ ______ Commands ________ ________________________ x=x+2; end
12 1 11 10 2 9 3 8 4 5 7 6 15-Min Break Survey
Functions • To run any script (plotsin.m for example): • Run. • F5 • In command window >> plotsin • Create a function: functionplotsin(inputs) ________________________ ______ Commands ________ ________________________ • The file and the functions names must be the same. • The file must be saved in the current directory.
Functions functionplotsin(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 !!')
Log of any base • Create a function to calculate the log: • Create new script called logn: functionlogn(x,n) log(x)/log(n) • Try in the command editor: >> logn(27,3) ans = 3.0000
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);
Log of any base function 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);
Insert into a vector function out=insertv(in,new,n) ifsize(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)];
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 ...
MATLAB Tips !! • Tic ... Toc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; disp('Press any key to start the stop watch!') pause tic disp('Press any key to stop!') pause toc
Close MATLAB • X button. • Code >> exit >> quit • Ctrl + Q
GOOD LUCK To be continued in the next lecture …