Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture (6) Programming (3) Eng. Osama Talaat 1. Announcement  Previous lecture videos are available at the course link.  Extra Materials.  Survey.

Similar presentations


Presentation on theme: "Lecture (6) Programming (3) Eng. Osama Talaat 1. Announcement  Previous lecture videos are available at the course link.  Extra Materials.  Survey."— Presentation transcript:

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

2 Announcement  Previous lecture videos are available at the course link.  Extra Materials.  Survey. 2

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

4 %%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%% clear all; close all; clc; for k=1:5 pass = input('Please enter password: ','s'); if strcmp(pass,'a13') disp('Welcome Osama') break elseif strcmp(pass,'com') disp('Welcome Hamdy') break elseif strcmp(pass,'t10') disp('Welcome Mona') break else disp(['Wrong password, trial ' num2str(k) ' of 5']) end end 4 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) 5

6 View  Change the angle of view for a figure.  Get the current angles: >> [a,b]=view a = -37.5000 b = 30 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 7

8 Pause  Pause for n seconds: >> pause(n)  Pause till you press any key >> pause 8

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 9

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 disp('The number of accurance of 3s:'); disp(c) 10

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

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/3812465/slides/slide_12.jpg", "name": "%%%%%%%%%%%%%%%%%%%% % Designed by: Eng.", "description": "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

13 While Loop while conditions ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________ 13

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

15 15-Min Break 15 6 7 8 12 1 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. 16

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

18 Log of any base 18

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

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

21 Insert into a vector function 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)]; 21

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

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 23

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

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


Download ppt "Lecture (6) Programming (3) Eng. Osama Talaat 1. Announcement  Previous lecture videos are available at the course link.  Extra Materials.  Survey."

Similar presentations


Ads by Google