Programming Numerical Computing with. MATLAB for Scientists and Engineers.

Slides:



Advertisements
Similar presentations
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Control Structures Nested ifs, switch statements.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Programming Environment S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan-Dearborn Introduction to Matlab: Control Flow.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Computer Science in Practice This course is an introduction to problems (and solutions) that arise in applied fields of computer science such as machine.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 8 Branching Statements and Program Design.
MATLAB Programming fprintf Cell arrays Structures Flow of control Vectorization Functions 1.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
How do I find the surface area and volume of a sphere?
Vlachopoulos Georgios Lecturer of Computer Science and Informatics Technological Institute of Patras, Department of Optometry, Branch of Egion Lecturer.
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
1 Advanced MATLAB Vectors and matrices fprintf Cell arrays Structures Flow of control Vectorization Functions.
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.
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Function M-File Numerical Computing with. MATLAB for Scientists and Engineers.
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.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
MATLAB Practice 1 Introducing MATLAB Lecture Notes on Video Search & Mining, Spring 2012 Presented by Jun Hee Yoo Biointelligence Laboratory School of.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
Lecture (5) Programming (2) Eng. Osama Talaat 1. Announcement  M-Files are available:  Download the file from the course page
Conditional Logic in MATLAB By Bruce Raine. How to do a basic IF – END structure in MATLAB if MATLAB Commands end i.e. do the MATLAB commands if the conditional.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The do - while Loop Venkatesh Ramamoorthy 02-March-2005.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
EXAMPLE 3 Comparing Metric Measurements Copy and complete the statement using, or =. a. 320 cm ? 4 m SOLUTION 320 cm ? 4 m Strategy: Convert meters to.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
Loops and Conditional Statements Dr.Abbas Lab 4 1ENG. Ahmad Alaql.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
ITEC 2600 Introduction to Analytical Programming
Volume of Cylinders Answers use 3.14 for π.
Computer Application in Engineering Design
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 MATLAB Programming
Introduction to MATLAB
Lecture 3 MATLAB programming (1)
Structured Program Development in C
Section 9.4 Linear Measurements.
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
Conditional Statements
SELECTIONS STATEMENTS
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.
Matlab Basics.
ME 123 Computer Applications I Lecture 11: More on For loop 3/27/03
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

Programming Numerical Computing with. MATLAB for Scientists and Engineers

You will be able to Write simple MATLAB programs using if statement, Write simple MATLAB programs using for statement, Convert a for-loop based program into a matrix operation version. 2

Relational Operators Operators Results are logicals 3 < < <= > > >= == ~= a = 5 < 8 b = [1:2:8] > 3 c = [2:2:13] == [ 0:3:17 ] A = randint(3,4,10) A>5 A(A>5) = 5

Exercise 1: Hate 4 Generate a 5x6 matrix A with random integers between 0 and 10. If some elements of matrix A is 4, replace it with 0. If some elements of matrix A is odd, subtract 1 from the elements. 4 hate4.m

Logical Operators Operators Built-in Functions Examples 5 & & | | ~ ~ and(A,B) or(A,B) not(A) xor(A,B) all(A) any(A) find(A) find(A>d)

Conditional Statement: if 6 if condition if condition – else x<0 S1 x<0 S1 T F S2 T F n = input('Enter an integer: '); if mod(n,2) fprintf('%d is an odd integer.\n', n ); end n = input('Enter an integer: '); if mod(n,2) fprintf('%d is an odd integer.\n', n ); end S1 n = input('Enter an integer: '); if mod(n,2) fprintf('%d is an odd integer.\n', n ); else fprintf('%d is an even integer.\n', n ); end n = input('Enter an integer: '); if mod(n,2) fprintf('%d is an odd integer.\n', n ); else fprintf('%d is an even integer.\n', n ); end S1 S2

Exercise 2 Volume in a Water Tank Find the volume of the water tank below when the depth is h. 7 10m 8m 6m h m

Solution 2 Function m-file Commands 8 water_volume.m

if – elseif – else – end Nested If 9 F F F if S1 S2 T elseif S3 S4 T T if A x = a else if B x = b else if C x = c else x = d end if A x = a else if B x = b else if C x = c else x = d end if A x = a elseif B x = b elseif C x = c else x = d end if A x = a elseif B x = b elseif C x = c else x = d end

For Loops for n=a:b statements end 10 i=0; for t=0:0.1:100 i = i+1; x(i) = sin(2*pi*200*t); end i=0; for t=0:0.1:100 i = i+1; x(i) = sin(2*pi*200*t); end t=0:0.1:100; x = sin(2*pi*200*t); t=0:0.1:100; x = sin(2*pi*200*t); for n=1:5 for m=5:-1:1 a(n,m)=n^2 + m^2; end for n=1:5 for m=5:-1:1 a(n,m)=n^2 + m^2; end n=1:5; m=1:5; [nn,mm]=meshgrid(n,m); a=nn.^2 + mm.^2; n=1:5; m=1:5; [nn,mm]=meshgrid(n,m); a=nn.^2 + mm.^2;

Example Taylor series of sine function Write a function y= Tsin(x,n), which is the partial sum of n-terms. 11

Exercise 3 Taylor series of exp(x). Write a function y= Texp(x,n), which is the partial sum of n-terms. Use Texp() to plot exp(x), Texp(x,5), Texp(x,10), Texp(x,100). 12

Solution 3 Function m-file Commands and Screenshot 13 Texp.m

While Loops i=0;t=0; while t<=100 i = i+1; x(i) = sin(2*pi*200*t); t = t+0.1; end i=0;t=0; while t<=100 i = i+1; x(i) = sin(2*pi*200*t); t = t+0.1; end t=0:0.1:100; x = sin(2*pi*200*t); t=0:0.1:100; x = sin(2*pi*200*t); i=0; for t=0:0.1:100 i = i+1; x(i) = sin(2*pi*200*t); end i=0; for t=0:0.1:100 i = i+1; x(i) = sin(2*pi*200*t); end 14

Lotto Numbers disp('I generate Lotto numbers'); disp('Good Luck!'); B=[]; for i=1:5 L=0; while L ~= 6, fprintf('Try - %d \n ', i ); A=floor(rand(1,6)*45)+1; A=unique(A); L=length(A); end B(i,:) = A; pause(1); end B disp('I generate Lotto numbers'); disp('Good Luck!'); B=[]; for i=1:5 L=0; while L ~= 6, fprintf('Try - %d \n ', i ); A=floor(rand(1,6)*45)+1; A=unique(A); L=length(A); end B(i,:) = A; pause(1); end B 15

Branch apple=100; if apple < 5 disp('few'); elseif apple < 20 disp('a few'); else disp('a lot'); end apple=100; if apple < 5 disp('few'); elseif apple < 20 disp('a few'); else disp('a lot'); end mon = 10; switch mon case 1 month = 'January'; case 2 month = 'February'; otherwise month = 'Others'; end mon = 10; switch mon case 1 month = 'January'; case 2 month = 'February'; otherwise month = 'Others'; end 16

Branch x = 2.7; units = 'm'; switch units case {'inch', 'in'} y = x * 2.54; case {'feet', 'ft'} y = x * 2.54 * 12; case {'meter', 'm'} y = x * 100; case {'milimeter', 'mm'} y = x / 10; case {'centimeter', 'cm'} y = x; otherwise y = nan; end x = 2.7; units = 'm'; switch units case {'inch', 'in'} y = x * 2.54; case {'feet', 'ft'} y = x * 2.54 * 12; case {'meter', 'm'} y = x * 100; case {'milimeter', 'mm'} y = x / 10; case {'centimeter', 'cm'} y = x; otherwise y = nan; end Unlike C, no break! Unlike C, no break! 17