Introduction to Matlab LAB 4

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

Introduction to Matlab
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Programming with MATLAB
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Introduction to MATLAB
Dr. Jie Zou PHY Introduction to MATLAB Programming with MATLAB 1 1 Applied Numerical Methods with MATLAB for Engineers and Scientists, 2nd ed., Steven.
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/2009.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
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.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
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.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
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 Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
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 Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
1 Structured Programming EEN170 Programming in MATLAB.
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.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Matlab Programming for Engineers
Introduction to Matlab
EEE 161 Applied Electromagnetics
Computer Application in Engineering Design
Matlab Training Session 4: Control, Flow and Functions
ECE 1304 Introduction to Electrical and Computer Engineering
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB DENC 2533 ECADD LAB 9.
MATLAB: Structures and File I/O
Introduction to MATLAB
Matlab review Matlab is a numerical analysis system
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
CS100J 26 April. Matlab Use help button!!! Variables, values, types
MATLAB Tutorial Dr. David W. Graham.
محاسبات عددی در مهندسی پزشکی جلسه اول و دوم مقدمه ای بر نرم افزار MATLAB گلناز بغدادی 1391.
Logical Operations In Matlab.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
INTRODUCTION TO MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
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 Tutorial
Matlab Basics.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Presentation transcript:

Introduction to Matlab LAB 4

Use of M-File Extension “.m” Click to create a new M-File Extension “.m” A text file containing script or function or program to run

Use of M-File Save file as Denem430.m If you include “;” at the end of each statement, result will not be shown immediately

Writing User Defined Functions Functions are m-files which can be executed by specifying some inputs and supply some desired outputs. The code telling the Matlab that an m-file is actually a function You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2)

Writing Functions: Example Here is a function that implements the quadratic formula. function [x1,x2] = quadform(a,b,c) d = sqrt(bˆ2 - 4*a*c); x1 = (-b + d) / (2*a); x2 = (-b - d) / (2*a); From MATLAB you could call >> [r1,r2] = quadform(1,-2,1) r1 = 1 r2 =1

Writing Functions: Example Another function which takes an input array and returns the sum and product of its elements as outputs The function sumprod(.) can be called from command window or an m-file as

Operators (relational, logical) == Equal to ~= Not equal to < Strictly smaller > Strictly greater <= Smaller than or equal to >= Greater than equal to & And operator | Or operator

IF statement

Control Structures If Statement Syntax if (Condition_1) Some Dummy Examples if ((a>3) & (b==5)) Some Matlab Commands; end if (a<3) elseif (b~=5) else If Statement Syntax if (Condition_1) Matlab Commands elseif (Condition_2) elseif (Condition_3) else end

The if Statement: Example The if statement’s basic form is if logical expression statements end if x >= 0 y = sqrt(x) end z = 0;w = 0; if (x >= 0)&(y >= 0) z = sqrt(x) + sqrt(y) w = sqrt(x*y) end Whenever entering text as input to title, legend or labels use ‘’ to enclose the text 10 10

Writing Functions along with IF statement Examples Write a function : out=squarer (A, ind) Which takes the square of the input matrix if the input indicator is equal to 1 And takes the element by element square of the input matrix if the input indicator is equal to 2 Same Name

The if Statement: Example if logical expression 1 statement group 1 if logical expression 2 statement group 2 end   Whenever entering text as input to title, legend or labels use ‘’ to enclose the text x = [4 -9 25]; 12 12

switch statement

Switch Statement The if/elseif construct is fine when only a few options are present. When a large number of options are possible, it’s customary to use switch instead. e.g. switch units case ’length’ disp(’meters’) case ’volume’ disp(’liters’) case ’time’ disp(’seconds’) otherwise disp(’I give up’) end Units = input(‘put units you need to know, ‘S’);

Switch Statement: Example month = input (‘Please input month (1-12)’); switch month case {1,3,5,7,8,10,12} disp (‘31 days’) case {4,6,9,11} disp (‘30 days’) case {2} disp (‘28 days’) end

Loop statement

Control Structures For loop syntax for i=Index_Array Matlab Commands Some Dummy Examples for i=1:100 Some Matlab Commands; end for j=1:3:200 for m=13:-0.2:-21 for k=[0.1 0.3 -13 12 7 -9.3] For loop syntax for i=Index_Array Matlab Commands end

for loop variable m:s:n statements end for Loops for loop variable m:s:n statements end for k = 5:10:35 x = k^2 end Whenever entering text as input to title, legend or labels use ‘’ to enclose the text 18 18

for Loops Write a script le to compute the sum of the rst 15 terms in the series 5 k2-2k, k = 1,2, 3, …, 15 total = 0; for k = 1:15 total = 5*k^2 - 2*k + total; end disp (‘The sum for 15 terms is:’) disp (total) Whenever entering text as input to title, legend or labels use ‘’ to enclose the text 19 19

for Loops Whenever entering text as input to title, legend or labels use ‘’ to enclose the text 20 20

for Loops We choose a spacing dx 35/300 to obtain 301 points, which is sufcient to obtain a smooth plot. The script le is the following: dx = 35/300; x = -5:dx:30; for k = 1:length(x) if x(k) >= 9 y(k) = 15*sqrt(4*x(k)) + 10; elseif x(k) >= 0 y(k) = 10*x(k) + 10; else y(k) = 10; end plot (x,y), xlabel(’x’), ylabel(‘y’) Whenever entering text as input to title, legend or labels use ‘’ to enclose the text 21 21

While statement

Control Structures While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end