Introduction to MATLAB Programming

Similar documents
Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

MATLAB Introduction to MATLAB Programming

CME 192: Introduction to Matlab

Step by step set of instructions to accomplish a task or solve a problem

ENGR 1181 MATLAB 05: Input and Output

Lecture 1: Hello, MATLAB!

COMPUTER SKILLS LESSON 12. Valeria Cardellini A.Y. 2015/16

An Introduction to MATLAB

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5)

Finding, Starting and Using Matlab

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

Chapter 3: Introduction to MATLAB Programming (4 th ed.)

Numerical Analysis First Term Dr. Selcuk CANKURT

Programming in MATLAB

Script and Function File

MATLAB PROGRAMMING LECTURES. By Sarah Hussein

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Programming in Mathematics. Mili I. Shah

PART 2 DEVELOPING M-FILES

Matlab Programming Arrays and Scripts 1 2

String Manipulation. Chapter 7. Attaway MATLAB 4E

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

EP375 Computational Physics

Dr. Khaled Al-Qawasmi

MATLAB GUIDE UMD PHYS401 SPRING 2012

Numerical Methods for Civil Engineers

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Introduction to Computing Lecture 03: Basic input / output operations

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Matlab Programming MET 164 1/24

PART 1 PROGRAMMING WITH MATHLAB

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

MATLAB GUIDE UMD PHYS401 SPRING 2011

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Strings and I/O functions

211: Computer Architecture Summer 2016

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

Fall 2014 MAT 375 Numerical Methods. Introduction to Programming using MATLAB

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Programming 1. Script files. help cd Example:

Dr Richard Greenaway

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

The continue command goes back to the start of a loop and increments by one unit.

Introduction to MATLAB

INTRODUCTION TO NUMERICAL ANALYSIS

Top bar items. Current folder should be shown. Command Window This has the the prompt >>

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

CMPT 102 Introduction to Scientific Computer Programming. Input and Output. Your first program

Question 2. [5 points] Given the following symbolic constant definition

Scalars and Variables

Programming in MATLAB

How to succeed in Math 365

MATLAB - Lecture # 4

Loop Statements and Vectorizing Code

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Page 1 of 7 E7 Spring 2009 Midterm I SID: UNIVERSITY OF CALIFORNIA, BERKELEY Department of Civil and Environmental Engineering. Practice Midterm 01

Laboratory 1 Octave Tutorial

Introduction to MATLAB

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

MATLAB GUIDE UMD PHYS375 FALL 2010

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

CSE 12 Spring 2016 Week One, Lecture Two

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Branches, Conditional Statements

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Prog-PC1: Attaway Chapter 1

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

Math 98 - Introduction to MATLAB Programming. Fall Lecture 1

Teaching Manual Math 2131

LabVIEW MathScript Quick Reference

Introduction to MATLAB

Desktop Command window

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

Operating System Interaction via bash

Scientific Computing with MATLAB

Access to Delimited Text Files

Chapter 3: Programming with MATLAB

Chapter 11 Input/Output (I/O) Functions

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to MATLAB

Introduction to C++ (Extensions to C)

Introduction to MATLAB

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

MAT 275 Laboratory 1 Introduction to MATLAB

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2

LECTURE 02 INTRODUCTION TO C++

Introduction to MATLAB

Part I: MATLAB Overview

Linear algebra & Numerical Analysis

Transcription:

Spring 2019 Spring 2019 1 / 17

Introduction Algorithm An algorithm is a sequence of steps needed to solve a problem. We will use MATLAB to develop algorithms to solve specific problems. The basic algorithm consists of 3 basic steps 1 Get input(s) 2 Calculate the result(s) 3 Display result(s) Spring 2019 2 / 17

Scripts Scripts A script is a sequence of MATLAB instructions that are stored in a M-file and saved. Before creating a script, make sure the current folder is set to the folder in which you want so save your files To start a new script >>edit script1.m Spring 2019 3 / 17

Scripts input function Objective: Take input from the user To call the input function - pass the prompt for input: If the expected input is a number 1 >>radius =input('enter the radius:'); Spring 2019 4 / 17

Scripts input function Objective: Take input from the user To call the input function - pass the prompt for input: If the expected input is a number 1 >>radius =input('enter the radius:'); If the expected input is a character or string of characters 1 >> letter=input('enter a char:','s') Spring 2019 4 / 17

Scripts Output statements: disp Output statements display strings and/ or results of calculations. Spring 2019 5 / 17

Scripts Output statements: disp Output statements display strings and/ or results of calculations. The simplest output function is disp 1 >> disp('hello World') 2 Hello World 3 >> disp(4ˆ2) 4 16 Spring 2019 5 / 17

Scripts Output statements: disp Output statements display strings and/ or results of calculations. The simplest output function is disp 1 >> disp('hello World') 2 Hello World 3 >> disp(4ˆ2) 4 16 disp will display the result of an expression or a string without assigning any value to ans. Spring 2019 5 / 17

Scripts Output statements: disp Output statements display strings and/ or results of calculations. The simplest output function is disp 1 >> disp('hello World') 2 Hello World 3 >> disp(4ˆ2) 4 16 disp will display the result of an expression or a string without assigning any value to ans. disp does not allow formatting. Spring 2019 5 / 17

Scripts Formatted output: fprintf Formatted output can be printed to the screen using fprintf. 1 >> fprintf('the answer is %d. \n',42) 2 The answer is 42. Specify decimal places for real numbers 1 >> x=2; 2 >> fprintf('the square root of %d is %.6f.\n',x,sqrt(x)) 3 The square root of 2 is 1.414214. Spring 2019 6 / 17

Scripts Formatted output: fprintf Formatted output can be printed to the screen using fprintf. 1 >> fprintf('the answer is %d. \n',42) 2 The answer is 42. Specify decimal places for real numbers 1 >> x=2; 2 >> fprintf('the square root of %d is %.6f.\n',x,sqrt(x)) 3 The square root of 2 is 1.414214. We can also specify field width 1 >> fprintf('the square root of %d is... %20.6f.\n',x,sqrt(x)) 2 The square root of 2 is 1.414214. Spring 2019 6 / 17

Scripts Formatted output: fprintf We can also specify field width 1 >> fprintf('the square root of %d is... %20.6f.\n',x,sqrt(x)) 2 The square root of 2 is 1.414214. If the field with is negative, the printing is left aligned 1 >> fprintf('the square root of %d is... %-20.6f.\n',x,sqrt(x)) 2 The square root of 2 is 1.414214. Spring 2019 7 / 17

Scripts Formatted output: fprintf We can also print vectors or matrices Spring 2019 8 / 17

Scripts Formatted output: fprintf We can also print vectors or matrices 1 >> x = [0, 0.5, 1]; 2 >> y = [x; exp(x)]; 3 >> fprintf('%6.1e %12.4e\n',y); 4 0.0e+00 1.0000e+00 5 5.0e-01 1.6487e+00 6 1.0e+00 2.7183e+00 And strings 1 >> fprintf('my string is %s! \n','hello World') 2 My string is Hello World! Spring 2019 8 / 17

Scripts Formatted output: fprintf We pass to fprintf text to be printed and conversion specifications and expressions to be printed. Each conversion specification is introduced by a % character and ended by a letter d c s e f The argument is converted into decimal notation is taken to be a single character is a string is converted into decimal notation of the form m.nnnnnexx where the length of n s is specified is converted into decimal notation of the form mmm.nnnnn where the length of n s is specified Spring 2019 9 / 17

Scripts Special formats Special character Format specifier Backspace \b New line \n Horizontal tab \t Additional options can be found here Spring 2019 10 / 17

Functions User defined functions Scripts vs Functions All variables and parameters of a script are accessible in the workspace, i.e. externally accessible. Spring 2019 11 / 17

Functions User defined functions Scripts vs Functions All variables and parameters of a script are accessible in the workspace, i.e. externally accessible. This makes scripts good for testing and experimenting. Spring 2019 11 / 17

Functions User defined functions Scripts vs Functions All variables and parameters of a script are accessible in the workspace, i.e. externally accessible. This makes scripts good for testing and experimenting. In general, create a function to solve a given problem for arbitrary parameters. Spring 2019 11 / 17

Functions User defined functions Scripts vs Functions All variables and parameters of a script are accessible in the workspace, i.e. externally accessible. This makes scripts good for testing and experimenting. In general, create a function to solve a given problem for arbitrary parameters. Use a script to run functions for specific parameters required. Spring 2019 11 / 17

Functions Anatomy of MATLAB functions A function returning a single result consists of the following: Function header (the first line), comprised of function outputargument = functionname(input arguments) Spring 2019 12 / 17

Functions Anatomy of MATLAB functions A function returning a single result consists of the following: Function header (the first line), comprised of function outputargument = functionname(input arguments) Comments that describe what the function does (these comments will be printed when help is called) Spring 2019 12 / 17

Functions Anatomy of MATLAB functions A function returning a single result consists of the following: Function header (the first line), comprised of function outputargument = functionname(input arguments) Comments that describe what the function does (these comments will be printed when help is called) The body of the function that should manipulate the inputvariable and assign a value to the outputvariable Spring 2019 12 / 17

Functions Anatomy of MATLAB functions A function returning a single result consists of the following: Function header (the first line), comprised of function outputargument = functionname(input arguments) Comments that describe what the function does (these comments will be printed when help is called) The body of the function that should manipulate the inputvariable and assign a value to the outputvariable end at the end of the function Spring 2019 12 / 17

Functions Anatomy of MATLAB functions 1 function outputargument = functionname(input arguments) 2 %Comments that describe what this function does 3 4 Statements and computations 5 end % end of function Spring 2019 13 / 17

Functions Programming Style Guidelines Make sure your comments describing functions or scripts contain useful information Spring 2019 14 / 17

Functions Programming Style Guidelines Make sure your comments describing functions or scripts contain useful information Put a newline character at the end of every string printed by fprintf Spring 2019 14 / 17

Functions Programming Style Guidelines Make sure your comments describing functions or scripts contain useful information Put a newline character at the end of every string printed by fprintf Suppress the output from all assignment statements in a function Functions that return a value do not normally print the value Spring 2019 14 / 17

Functions Single input and output Compute the area of a circle of radius r. 1 function area = calcarea(r) 2 %Computes area of a circle of radius r 3 %02/03 MA302 4 5 area = pi*pow2(r,2); 6 end Spring 2019 15 / 17

Functions Single input and multiple outputs Comupute the average x = 1 n n i=1 (x x)2. n n x i and standard deviation i=1 1 function [avg,sd] = stat(x) 2 %computes the average value and standard 3 %deviation of a vector 4 %PC 02/03 MA302 5 6 n = length(x); 7 avg = sum(x)/n; 8 sd = sqrt(sum((x - avg).ˆ2)/n); 9 end WARNING - the functions mean and std already exist so do not use these as variable names otherwise MATLAB will not perform these functions. Spring 2019 16 / 17

Functions Multiple inputs and outputs Evaluate f (x, y) = cx 2 y 1 function z = myfun(x,y,c) 2 %evaluates f(x) = cxˆ2*y 3 %x and y are coordinates from meshgrid 4 %PC 02/03 MA302 5 6 z = c*(x.ˆ2).*y; 7 end Spring 2019 17 / 17