Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

Data AnalysisComputer ScienceEngineering MathematicsMATLAB Programming

An introduction to MATLAB programming, covering the basics of algorithms, scripts, and functions. It explains how to take user input, perform calculations, and display results using MATLAB's input and output functions. The document also introduces formatted output using fprintf and discusses the differences between scripts and functions.

What you will learn

  • What are the advantages of using functions instead of scripts in MATLAB?
  • How do you create and save a script in MATLAB?
  • What is the difference between the input and input functions in MATLAB?
  • How do you use fprintf for formatted output in MATLAB?
  • What is an algorithm and how is it used in MATLAB programming?

Typology: Lecture notes

2021/2022

Uploaded on 09/12/2022

barnard
barnard 🇺🇸

3.9

(9)

1 document

1 / 31

Toggle sidebar

Related documents


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 Spring 2019 1 / 17 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) Introduction to MATLAB Programming Spring 2019 2 / 17 Introduction to MATLAB Programming 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') Introduction to MATLAB Programming Spring 2019 4 / 17 Introduction to MATLAB Programming 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. Introduction to MATLAB Programming Spring 2019 5 / 17 Introduction to MATLAB Programming 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. Introduction to MATLAB Programming Spring 2019 5 / 17 Introduction to MATLAB Programming 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. Introduction to MATLAB Programming Spring 2019 6 / 17 Introduction to MATLAB Programming 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. Introduction to MATLAB Programming Spring 2019 6 / 17 Introduction to MATLAB Programming 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 . Introduction to MATLAB Programming Spring 2019 7 / 17 Introduction to MATLAB Programming 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 The argument d is converted into decimal notation c is taken to be a single character s is a string e is converted into decimal notation of the form m.nnnnnExx where the length of n’s is specified f is converted into decimal notation of the form mmm.nnnnn where the length of n’s is specified Introduction to MATLAB Programming Spring 2019 9 / 17 Introduction to MATLAB Programming Scripts Special formats Special character Format specifier Backspace \b New line \n Horizontal tab \t Additional options can be found here Introduction to MATLAB Programming Spring 2019 10 / 17 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. Introduction to MATLAB Programming Spring 2019 11 / 17 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. Introduction to MATLAB Programming Spring 2019 11 / 17 Introduction to MATLAB Programming 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 Introduction to MATLAB Programming Spring 2019 12 / 17 Introduction to MATLAB Programming 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 Introduction to MATLAB Programming Spring 2019 12 / 17 Introduction to MATLAB Programming 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 Introduction to MATLAB Programming Spring 2019 13 / 17 Introduction to MATLAB Programming 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 Introduction to MATLAB Programming Spring 2019 14 / 17 Introduction to MATLAB Programming 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 Introduction to MATLAB Programming Spring 2019 14 / 17 Introduction to MATLAB Programming Functions Single input and multiple outputs Comupute the average x = 1 n n∑ i=1 xi and standard deviation√∑n i=1(x − x)2 n . 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. Introduction to MATLAB Programming Spring 2019 16 / 17 Introduction to MATLAB Programming Functions Multiple inputs and outputs Evaluate f (x , y) = cx2y 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 Introduction to MATLAB Programming Spring 2019 17 / 17
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved