1 / 25

Lecture (6)

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

peigi
Download Presentation

Lecture (6)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture (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: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

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

  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 Loop whileconditions ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________

  14. While & For for x=1:2:100 ________________________ ______ Commands ________ ________________________ end ---------------------------- x=1; whilex<=100 ________________________ ______ Commands ________ ________________________ x=x+2; end

  15. 12 1 11 10 2 9 3 8 4 5 7 6 15-Min Break Survey

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

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

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

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

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

  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!') pause tic disp('Press any key to stop!') pause toc

  24. Close MATLAB • X button. • Code >> exit >> quit • Ctrl + Q

  25. GOOD LUCK To be continued in the next lecture …

More Related