Upload
tranhanh
View
216
Download
2
Embed Size (px)
Citation preview
Elements ofProgramming
in Matlab
Daniel J. BodonyDepartment of Aerospace EngineeringUniversity of Illinois at Urbana-Champaign
Video 15
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
What is a program?
A definition:
A sequence of commands written in one or more programming languagesthat 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?
A definition:
A sequence of commands written in one or more programming languagesthat 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 taskscomes in two forms:
interpreted language — The source code is read by an intermediateprogram that directly executes the commands. Examples: Matlab,Python
compiled language — The source code is converted into amachine-readable file, called the binary or executable, by a compiler.The binary is then executed separately by the user. Examples:Fortran, C, C++.
D. J. Bodony (UIUC) AE199 IAC Video 15 4 / 8
Interpreted vs. Compiled Language
Pros ConsInterpreted More intuitive language Slow to runCompiled 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 forcontrolling hardware)
History (e.g., Aerospace company many have many programs alreadywritten in one language)
Community-wide acceptance of one approach (e.g., In dynamics andcontrols, Matlab is king while in computational fluid dynamics (CFD),C/C++/Fortran dominate)
D. J. Bodony (UIUC) AE199 IAC Video 15 5 / 8
Interpreted vs. Compiled Language
Pros ConsInterpreted More intuitive language Slow to runCompiled 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 forcontrolling hardware)
History (e.g., Aerospace company many have many programs alreadywritten in one language)
Community-wide acceptance of one approach (e.g., In dynamics andcontrols, 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
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 performtasks
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
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 performtasks
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
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 performtasks
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
Program Organization
Consider the simple example of writing a program to compute the area ofthe circle for a given radius, A = πr2.
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 separatefunctions 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
Program Organization
Consider the simple example of writing a program to compute the area ofthe circle for a given radius, A = πr2.
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 separatefunctions 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