1 / 33

Lecture (5)

Lecture (5). Programming (2) Eng. Osama Talaat. Announcement. M-Files are available: Download the file from the course page www.osamatalaat.com/matlab Copy it into the current directory Open it using MATLAB (Open, Double click, ... ) Run !!. Clean Start. clear all ; close all ;

paley
Download Presentation

Lecture (5)

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 (5) Programming (2) Eng. Osama Talaat

  2. Announcement • M-Files are available: • Download the file from the course page www.osamatalaat.com/matlab • Copy it into the current directory • Open it using MATLAB (Open, Double click, ... ) • Run !!

  3. Clean Start clear all; close all; clc; % n=input('Please enter the number of cycles: '); % f=input('Please enter the frequency: '); % A=input('Please enter the Amplitude: '); x=[0:0.1:2*pi]; y=sin(x); plot(x,y) xlabel('x') ylabel('sin(x)') title('Sine Curve') grid disp('The amplitude values are: ') y disp('Plotting Done Successfully!!')

  4. No of Cylces clear all; close all; clc; n=input('Please enter the number of cycles: '); % f=input('Please enter the frequency: '); % A=input('Please enter the Amplitude: '); x=[0:0.1:2*pi*n]; y=sin(x); plot(x,y) xlabel('x') ylabel('sin(x)') title('Sine Curve') grid disp('The amplitude values are: ') y disp('Plotting Done Successfully!!')

  5. Amplitude clear all; close all; clc; n=input('Please enter the number of cycles: '); % f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); x=[0:0.1:2*pi*n]; y=A*sin(x); plot(x,y) xlabel('x') ylabel([num2str(A) 'sin(x)']) title('Sine Curve') grid disp('The amplitude values are: ') y disp('Plotting Done Successfully!!')

  6. Frequency clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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: ') y disp('Plotting Done Successfully!!')

  7. Done !! clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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: ') y disp('Program Terminated !!')

  8. Output clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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: ') y disp('Program Terminated !!')

  9. Output clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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) disp('Program Terminated !!')

  10. Plot Sine • Try n = any negative number:

  11. clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); if n>0 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) end disp('Program Terminated !!')

  12. If Statement ifconditions ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________

  13. Error Msg clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); if n>0 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 !!')

  14. If Else Statement ifconditions ________________________ ______ Commands ________ ________________________ else ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________

  15. Compare ... if n>0 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 ...................................................... ifn>0 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) end if n<=0 disp('The number of cycles must be positive') end ...

  16. Comments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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. Comments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; n=input('Please enter the number of cycles: '); f=input('Please enter the frequency: '); A=input('Please enter the Amplitude: '); 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. Password • Enter the password. • If the entered password is ‘a13’, display a welcome message ‘Welcome Osama’. • Else, Display an error message ‘Wrong password’.

  19. To enter string %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; pass = input('Please enter password: ','s'); ifstrcmp(pass,'a13') disp('Welcome Osama') else disp('Wrong password') end To compare strings

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

  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; pass = input('Please enter password: ','s'); ifstrcmp(pass,'a13') disp('Welcome Osama') elseifstrcmp(pass,'com') disp('Welcome Hamdy') elseifstrcmp(pass,'t10') disp('Welcome Mona') else disp('Wrong password') end

  22. If Elseif Statement ifconditions 1 ________________________ ______ Commands ________ ________________________ elseifconditions 2 ________________________ ______ Commands ________ ________________________ elseifconditions 3 ________________________ ______ Commands ________ ________________________ ... else ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________

  23. Switch Case Statement switchvariable casevalue 1 ______ Commands ________ break casevalue 2 ______ Commands ________ break casevalue 3 ______ Commands ________ break . . . otherwise ______ Commands ________ break end ________________________ ______ Commands ________ ________________________

  24. Another Solution %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; pass = input('Please enter password: ','s'); switch pass case'a13' disp('Welcome Osama') break case'com' disp('Welcome Hamdy') break case't10' disp('Welcome Mona') break otherwise disp('Wrong password') break end

  25. Error Msg %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; pass = input('Please enter password: ','s'); switch pass case'a13' disp('Welcome Osama') break case'com' disp('Welcome Hamdy') break case't10' disp('Welcome Mona') break otherwise error('Wrong password') break end The error message stops execution

  26. Length Units Converter • Convert the length from meter to several length units. • Enter the length in meters. • Enter the unit to convert to. • Switch on the unit and upon its values multiply the input length by the conversion factor, and display the output

  27. Several values %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; len = input('Please enter Length in meter: '); unit = input('Please enter the unit to convert to: ','s'); switchunit case'cm' disp([num2str(len) ' m = 'num2str(len*100) ' cm']) break case'mm' disp([num2str(len) ' m = 'num2str(len*1000) ' mm']) break case'in' disp([num2str(len) ' m = ' num2str(len/0.0254) ' in']) break otherwise disp('This unit is not supported') break end

  28. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Designed by: Eng. Osama Talaat % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all; close all; clc; len = input('Please enter Length in meter: '); unit = input('Please enter the unit to convert to: ','s'); switchunit case{'cm' 'centimeter'} disp([num2str(len) ' m = 'num2str(len*100) ' cm']) break case{'mm' 'millimeter'} disp([num2str(len) ' m = 'num2str(len*1000) ' mm']) break case{'in' 'inch'} disp([num2str(len) ' m = ' num2str(len/0.0254) ' in']) break otherwise disp('This unit is not supported') break end

  29. For Loop clear all; close all; clc; for k=1:20 disp(rand) %do not display "ans =" end ________________________________________ forindex = vector ________________________ __ Commands to repeat __ ________________________ end ________________________ ______ Commands ________ ________________________

  30. Calculate the Formula >> s=0; >> s=s+1^2/factorial(1); >> s=s+2^2/factorial(2); >> s=s+3^2/factorial(3); >> s=s+4^2/factorial(4); >> s=s+5^2/factorial(5);

  31. Calculate the Formula clear all; close all; clc; s=0; for k=1:5 s=s+k^2/factorial(k); end disp(s)

  32. Test Yourself !!

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

More Related