<<

Elements of Programming in Matlab

Video 15

Daniel J. Bodony Department of Aerospace Engineering University of Illinois at Urbana-Champaign In this video you will learn. . .

1 What is a program? 2 Interpreted vs. Compiled Language 3 Roles of scripts and functions 4 Program Organization 5 Example: computing the area of a circle

D. J. Bodony (UIUC) AE199 IAC Video 15 2 / 8 The human-readable sequence of commands is called the source code.

What is a program?

A definition: A sequence of commands written in one or more programming languages that instruct a computer or other device to perform a desired set of tasks.

D. J. Bodony (UIUC) AE199 IAC Video 15 3 / 8 What is a program?

A definition: A sequence of commands written in one or more programming languages that instruct a computer or other device to perform a desired set of tasks.

Source code The human-readable sequence of commands is called the source code.

D. J. Bodony (UIUC) AE199 IAC Video 15 3 / 8 What is a program?

Interpreted or compiled computer language How the source code is used to instruct the computer to perform tasks comes in two forms: interpreted language — The source code is read by an intermediate program that directly executes the commands. Examples: Matlab, Python compiled language — The source code is converted into a machine-readable file, called the binary or executable, by a . The binary is then executed separately by the user. Examples: , , C++.

D. J. Bodony (UIUC) AE199 IAC Video 15 4 / 8 Which approach you choose depends on many things: Language features (e.g., Matlab has many easy-to-use functions for controlling hardware) History (e.g., Aerospace company many have many programs already written in one language) Community-wide acceptance of one approach (e.g., In dynamics and controls, Matlab is king while in computational fluid dynamics (CFD), C/C++/Fortran dominate)

Interpreted vs. Compiled Language

Pros Cons Interpreted More intuitive language Slow to run Compiled Fast! Requires compilation step; difficult to debug

D. J. Bodony (UIUC) AE199 IAC Video 15 5 / 8 Interpreted vs. Compiled Language

Pros Cons Interpreted More intuitive language Slow to run Compiled Fast! Requires compilation step; difficult to debug

Which approach you choose depends on many things: Language features (e.g., Matlab has many easy-to-use functions for controlling hardware) History (e.g., Aerospace company many have many programs already written in one language) Community-wide acceptance of one approach (e.g., In dynamics and controls, Matlab is king while in computational fluid dynamics (CFD), C/C++/Fortran dominate)

D. J. Bodony (UIUC) AE199 IAC Video 15 5 / 8 Our goals for this course

To introduce you to programming in An interpreted language using Matlab A compiled language using C

D. J. Bodony (UIUC) AE199 IAC Video 15 6 / 8 The controlling script file will use logic control (e.g., if-else and for) to call functions to perform tasks interact with the user through plots, fprintf, and error statements organize the data

The one or more function m-files will perform particular tasks as asked by the controlling script convert input data into output data

Roles of scripts and functions

A Matlab program will typically use: One script m-file to control the overall behavior One or more function m-files to perform individual tasks

D. J. Bodony (UIUC) AE199 IAC Video 15 7 / 8 The one or more function m-files will perform particular tasks as asked by the controlling script convert input data into output data

Roles of scripts and functions

A Matlab program will typically use: One script m-file to control the overall behavior One or more function m-files to perform individual tasks

The controlling script file will use logic control (e.g., if-else and for) to call functions to perform tasks interact with the user through plots, fprintf, and error statements organize the data

D. J. Bodony (UIUC) AE199 IAC Video 15 7 / 8 Roles of scripts and functions

A Matlab program will typically use: One script m-file to control the overall behavior One or more function m-files to perform individual tasks

The controlling script file will use logic control (e.g., if-else and for) to call functions to perform tasks interact with the user through plots, fprintf, and error statements organize the data

The one or more function m-files will perform particular tasks as asked by the controlling script convert input data into output data

D. J. Bodony (UIUC) AE199 IAC Video 15 7 / 8 Return to the video!

Program Organization

Consider the simple example of writing a program to compute the area of the circle for a given radius, A = πr 2.

An algorithm to calculate the circle’s area might include 1 Get the input (the radius) 2 Calculate the area 3 Display the results

A Matlab program would have a script m-file that called three separate functions to 1 prompt the user and read in the radius 2 calculate the area from the radius 3 display the results

D. J. Bodony (UIUC) AE199 IAC Video 15 8 / 8 Program Organization

Consider the simple example of writing a program to compute the area of the circle for a given radius, A = πr 2.

An algorithm to calculate the circle’s area might include 1 Get the input (the radius) 2 Calculate the area 3 Display the results

A Matlab program would have a script m-file that called three separate functions to 1 prompt the user and read in the radius 2 calculate the area from the radius 3 display the results

Return to the video!

D. J. Bodony (UIUC) AE199 IAC Video 15 8 / 8