Lecture (5)
description
Transcript of Lecture (5)
![Page 1: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/1.jpg)
1Lecture (5)
Programming (2)Eng. Osama Talaat
![Page 2: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/2.jpg)
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 !!
![Page 3: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/3.jpg)
3
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!!')
Clean
Start
![Page 4: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/4.jpg)
4
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!!')
No of Cylces
![Page 5: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/5.jpg)
5
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!!')
Amplitude
![Page 6: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/6.jpg)
6
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!!')
Frequency
![Page 7: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/7.jpg)
7
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 !!')
Done !!
![Page 8: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/8.jpg)
8
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 !!')
Output
![Page 9: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/9.jpg)
9
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 !!')
Output
![Page 10: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/10.jpg)
10 Plot Sine Try n = any negative number:
![Page 11: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/11.jpg)
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>0x=[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'); griddisp('The amplitude values are: '); disp(y)
end
disp('Program Terminated !!')
![Page 12: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/12.jpg)
12 If Statementif conditions
________________________
______ Commands ________
________________________
end
________________________
______ Commands ________
________________________
![Page 13: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/13.jpg)
13
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>0x=[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'); griddisp('The amplitude values are: '); disp(y)
else disp('The number of cycles must be positive')end
disp('Program Terminated !!')
Error Msg
![Page 14: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/14.jpg)
14 If Else Statementif conditions
________________________
______ Commands ________
________________________
else
________________________
______ Commands ________
________________________
end
________________________
______ Commands ________
________________________
![Page 15: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/15.jpg)
15
...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'); griddisp('The amplitude values are: '); disp(y)
else disp('The number of cycles must be positive')end
......................................................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'); griddisp('The amplitude values are: '); disp(y)
endif n<=0
disp('The number of cycles must be positive')end...
Compare
![Page 16: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/16.jpg)
16
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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 cyclesx=[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'); griddisp('The amplitude values are: '); disp(y)
else disp('The number of cycles must be positive')end
disp('Program Terminated !!')
Comments
![Page 17: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/17.jpg)
17
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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 cyclesx=[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 !!')
Comments
![Page 18: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/18.jpg)
18 Password Enter the password. If the entered password is ‘a13’, display a
welcome message ‘Welcome Osama’. Else, Display an error message ‘Wrong
password’.
![Page 19: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/19.jpg)
19
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear all; close all; clc; pass = input('Please enter password: ','s');if strcmp(pass,'a13') disp('Welcome Osama')else disp('Wrong password')end
To enter stringTo
compare strings
![Page 20: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/20.jpg)
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’.
![Page 21: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/21.jpg)
21
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Designed by: Eng. Osama Talaat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear all;close all;clc; pass = input('Please enter password: ','s');if strcmp(pass,'a13') disp('Welcome Osama')elseif strcmp(pass,'com') disp('Welcome Hamdy')elseif strcmp(pass,'t10') disp('Welcome Mona')else disp('Wrong password')end
![Page 22: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/22.jpg)
22 If Elseif Statementif conditions 1
______________________________ Commands ________________________________
elseif conditions 2______________________________ Commands ________________________________
elseif conditions 3______________________________ Commands ________________________________
...else
______________________________ Commands ________________________________
end______________________________ Commands ________________________________
![Page 23: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/23.jpg)
23 Switch Case Statementswitch variable
case value 1______ Commands ________break
case value 2______ Commands ________break
case value 3______ Commands ________break
. . .otherwise______ Commands ________break
end______________________________ Commands ________________________________
![Page 24: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/24.jpg)
24
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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') breakend
Another Solution
![Page 25: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/25.jpg)
25
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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') breakend
Error Msg
The error message stops execution
![Page 26: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/26.jpg)
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
![Page 27: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/27.jpg)
27
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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'); switch unit 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') breakend
Several values
![Page 28: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/28.jpg)
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'); switch unit 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') breakend
![Page 29: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/29.jpg)
29 For Loopclear all; close all; clc; for k=1:20 disp(rand) %do not display "ans ="end
________________________________________
for index = vector__________________________ Commands to repeat __________________________
end______________________________ Commands ________________________________
![Page 30: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/30.jpg)
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);
![Page 31: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/31.jpg)
31 Calculate the Formulaclear all; close all; clc;
s=0;
for k=1:5
s=s+k^2/factorial(k);
end
disp(s)
![Page 32: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/32.jpg)
32 Test Yourself !!
Try it using matrices:
![Page 33: Lecture (5)](https://reader035.fdocuments.in/reader035/viewer/2022062810/56815c65550346895dca72aa/html5/thumbnails/33.jpg)
33 GOOD LUCKTo be continued in the next lecture …