Lecture (5) Programming (2) Eng. Osama Talaat 1. Announcement  M-Files are available:  Download the file from the course page www.osamatalaat.com/matlab.

Slides:



Advertisements
Similar presentations
Lecture (4) Plotting & Programming (1) Eng. Osama Talaat 1.
Advertisements

Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:
Lecture (6) Programming (3) Eng. Osama Talaat 1. Announcement  Previous lecture videos are available at the course link.  Extra Materials.  Survey.
Introduction to Matlab EE 2303 Lab. MATLAB stands for “Matrix Laboratory” APPLICATIONS OF MATLAB:  Mathematical Calculations  Data Analysis & Visualization.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Functions.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CSci 2031: Matlab Tutorial Guoquan (Paul) Huang Jan 24, 2008.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
What is MATLAB ? MATrix LABoratory Developed by The Mathworks, Inc ( Mathworks, Inc Interactive, integrated, environment –for.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
1 Flow of control Sequential Executing instructions one by one, in exact order given Selection Choosing to execute a particular set of statements depending.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
Programming Numerical Computing with. MATLAB for Scientists and Engineers.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Lecture (7) Introduction to GUI Eng. Osama Talaat 1.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Chapter 4 MATLAB Programming MATLAB Troubleshooting Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Practice Programming Exam 1 Review: Part 2 1. Programming Example Bags Fly Fee!!! 2 XYZ is a commercial airline that asked you to create a MATLAB program.
CSE123 - Lecture 4 Structured Programming- Loops.
NoViC, Dept. of Mechanical Eng Acoustics Lab., NoViC 1. Download Matlab 2.
Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Measurement Notes Metric unit of length is the meter (m).
MATLAB – More Script Files
Matlab Training Session 4: Control, Flow and Functions
MATLAB DENC 2533 ECADD LAB 9.
Week 8 - Programming II Today – more features: Loop control
Lecture 4 MATLAB programming (2)
When I want to execute the subroutine I just give the command Write()
تصمیم‌گیری و کنترل روند، استفاده از حلقه‌ها و دستورات شرطی در متلب
MATLAB – Basic For Loops
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
Introduction to Matlab LAB 4
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Electrical and Computer Engineering Department SUNY – New Paltz
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Lecture (5) Programming (2) Eng. Osama Talaat 1

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

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!!') 3 CleanStart

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!!') 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=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!!') 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/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!!') 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('Program Terminated !!') 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: ') disp(y) disp('Program Terminated !!') 9 Output

Plot Sine  Try n = any negative number: 10

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 !!') 11

If Statement if conditions ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________ 12

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 !!') 13 Error Msg

If Else Statement if conditions ________________________ ______ Commands ________ ________________________ else ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________ 14

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

%%%%%%%%%%%%%%%%%%%% % 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 !!') 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

Password  Enter the password.  If the entered password is ‘a13’, display a welcome message ‘Welcome Osama’.  Else, Display an error message ‘Wrong password’. 18

%%%%%%%%%%%%%%%%%%%% % 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 19 To enter string To compare strings

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

%%%%%%%%%%%%%%%%%%%% % 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 21

If Elseif Statement if conditions 1 ________________________ ______ Commands ________ ________________________ elseif conditions 2 ________________________ ______ Commands ________ ________________________ elseif conditions 3 ________________________ ______ Commands ________ ________________________... else ________________________ ______ Commands ________ ________________________ end ________________________ ______ Commands ________ ________________________ 22

Switch Case Statement switch variable case value 1 ______ Commands ________ break case value 2 ______ Commands ________ break case value 3 ______ Commands ________ break... otherwise ______ Commands ________ break end ________________________ ______ Commands ________ ________________________ 23

%%%%%%%%%%%%%%%%%%%% % 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 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 error('Wrong password') break end 25 Error Msg The error message stops execution

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 26

%%%%%%%%%%%%%%%%%%%% % 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') break end 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'); 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') break end 28

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

Calculate the Formula 30

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

Test Yourself !! 32

GOOD LUCK To be continued in the next lecture … 33