Docsity

Introduction to MATLAB Programming: Algorithms, Scripts, and Functions, Lecture notes of Anatomy

Partial preview of the text

Download Introduction to MATLAB Programming: Algorithms, Scripts, and Functions and more Lecture notes Anatomy in PDF only on Docsity!

Introduction to MATLAB Programming

Spring 2019

Introduction to MATLAB Programming

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)

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:');

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

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

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.

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.

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.

Formatted output: fprintf

We can also print vectors or matrices

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+ 5 5.0e-01 1.6487e+ 6 1.0e+00 2.7183e+

And strings

1 >> fprintf('My string is %s! \n','Hello World') 2 My string is Hello World!

Special formats

Special character Format specifier

Backspace \b

New line \n

Horizontal tab \t

Additional options can be found here

Introduction to MATLAB Programming Functions

User defined functions

Scripts vs Functions

All variables and parameters of a script are accessible in the

workspace, i.e. externally accessible.

Introduction to MATLAB Programming 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.

Introduction to MATLAB Programming 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.