Computational Programming is an area of Software Development. We will use mathematical models, computational models and computer simulations to
Computational Mathematics
is the mathematics of data structures and algorithms used to create computational software. This includes
These is the core objectives of our course. Any modern programming language can be used to implement our data structures and algorithms.
MATLAB is an
Integrated Development Environment
(
IDE
).
A programming language and its compiler or interpreter provide a bridge between the human readable form of a program and the machine readable form.
In the machine readable form (
machine language
), everything is identified by a binary string or its hexadecimal equivalent such as
0110 0011 1001 1101 = 0x639d
These binary strings represent locations in memory, hardware registers, mnemonic codes, etc. Shortly after John von Neumann published his conditional transfer instructions in 1945, the Short Code programming language came into use. However programmers were required to translate their Short Code programs into hardware dependent binary opcodes , mneumonic machine instructions by hand.
Grace Hopper wrote the first compiler for the B-0 or FLOW-MATIC programming language. This was the beginning of modern high level computer languages. A high level computer language uses abstraction to represent
concepts and objects by mirroring common features or attributes of various non-abstract objects or systems
physical, spatial, or temporal details or attributes in the study of objects or systems
specific elements of the data used by the program
language constructs of the programming language
flow of control of the algorithm implemented by the program
Names become very important subject in programming. An
identifier
or name in MATLAB must begin with an upper case or lower case letter and may contain letters, numbers, and the underscore. Identifiers are
case sensitive
so the variable
b
and
B
represent
different
objects. In MATLAB we give names to
variables
,
function
,
classes
,
packages
, and more.
Keywords are identifiers that belong to the vocabulary of the programming language. They have a special meaning to programs written in that language and may not be used by developers to name other processes or objects. The MATLAB language has a lot of keywords !
We use names because they are a familiar element of the grammar of human languages. We gives names to everything. We give names to the objects in the world around us. We have names for the people and animals we meet. We give names to ideas. When we think about about a person, animal, or object, we are using abstraction. The ideas in our minds are not the objects themselves. They are what we imagine about them. When we give that collection of images a name, we are employing a powerful kind of abstraction .
Once we start using names, we invariably require definitions . When we communicate our ideas with each other, we often find that we have different names for many ideas. Two people often find that they use the same name to represent completely different ideas. Just as we do when communicating with each other, we must clearly define our names in a programming language so that the compiler or interpreter will create a machine readable version of our ideas that truly represent the data and algorithm we intend.
Some text in a programming language like MATLAB are not names, nor are they keywords. We call these strings of text literals .
There are several one character literals or operators . Like keywords they are special characters reserved by the MATLAB language to represent specific elements of an algorithm.
x = 1:10
x = 1:3:19
A(2:5,3)
All are examples of using operators
=
,
:
,
(
, and
)
in a MATLAB expression.
Sequences of characters may represent constant values in an expression:
seq = 'GCTAGAATCC';
A = ["a","bb","ccc"; "dddd","eeeeee","fffffff"]
f = 71;
c = (f-32)/1.8;
t = "Hello, world";
All are examples of assignment statements that employ literal expressions to initialize a variable.
Constants are not literals although constants are often initialized by literals. A constant is a value that is never altered by a program during normal execution. In some languages data is immutable , or constant. Like many programming languages, MATLAB statements or code are immutable. The difference is that a constant is part of the data of a program. It is a name that represents a memory location and the value stored in that memory location does not change during the execution of the program. A literal is part of the algorithm part of the program and is described in the programming statements.
In the MATLAB programming language literals are often used in statements and expressions. Immutable data types are not implemented in the MATLAB programming language. This means that a variable is constant when the programmer designs the algorithm to never change the value, not because the programming language refuses to allow any program statement to alter the value.
The MATLAB language does allow one to declare a property of a class to be
immutable
. This means that the constructor can initialize the value of the property, however no other method or function is allowed to change it. In these cases the properties of these classes have the
attribute
SetAccess = immutable
. This means that the
property set method
can not be defined for these properties and the built-in property set method raises an error. We will explain classes and their properties, methods, and attributes in a future chapter.
Programming is the implementation of algorithms in hardware and/or software. In this class we will write MATLAB scripts and programs to implement our algorithms. We will start with very basic computations and build up to programs that achieve each of the tasks described above. An algorithm is a finite list of instructions used to perform a task. If this definition did not shed much light on the definition of an algorithm, then welcome to the rest of us.
We use a program to describe the data and the algorithm for manipulating the data to achieve a result. The result may include a mean, median, and mode. The result may be a graphical representation of the data to illustrate important properties of the data. The result may be a correction to the power output of an oxygen regulator.
We write our programs in a high level programming language so that we can write our programs more efficiently and accurately. This also allows us to read and understand everyone else's programs.
A recipe for baking a cake comes with a list of quantities of each ingredient. In Computational Science this represents our data .
The Most Amazing Chocolate Cake
- butter and flour for coating and dusting the cake pan
- 3 cups all-purpose flour
- 3 cups granulated sugar
- 1 1/2 cups unsweetened cocoa powder
- 1 tablespoon baking soda
- 1 1/2 teaspoons baking powder
- 1 1/2 teaspoons salt
- 4 large eggs
- 1 1/2 cups buttermilk
- 1 1/2 cups warm water
- 1/2 cup vegetable oil
- 2 teaspoons vanilla extract
The recipe also consists of a list of instructions or algorithm that consists of each step in the process of creating the cake.
- Preheat oven to 350 degrees Fahrenheit. Butter three 9-inch cake rounds. Dust with flour and tap out the excess.
- Mix together flour, sugar, cocoa, baking soda, baking powder, and salt in a stand mixer using a low speed until combined.
- Add eggs, buttermilk, warm water, oil, and vanilla. Beat on a medium speed until smooth. This should take just a couple of minutes.
- Divide batter among the three pans. I found that it took just over 3 cups of the batter to divide it evenly.
- Bake for 30-35 minutes in a 350 degree oven until a toothpick inserted into the center comes out clean.
- Cool on wire racks for 15 minutes and then turn out the cakes onto the racks and allow to cool completely.
- Frost with your favorite frosting and enjoy!
If you change the data or portions of the ingredients, or do not follow the algorithm precisely then you might get something chocolate cake-like, but it won't be amazing .
We create a data structure when we organize data by defining precisely
A data structure is also called an model or a mathematical model . Let us start with a simple data structure.
Here we need to consider a model of a quadratic polynomial. We must identify the data that describes a quadratic polynomial and organize the data. We require that each representation describe exactly one polynomial, and each quadratic polynomial be described by exactly one set of data. A quadratic polynomial is a polynomial of degree 2 and examples of polynomials of degree 2 are
$$\begin{align*}
x^2 + 5 \\
3x^2 - 5x + 2 \\
-x^2 + 3x + 1
\end{align*}$$
From these examples we need to create an
abstract representation
of a quadratic polynomial that uniquely identifies every polynomial of degree 2. The letter that represents the variable is not important. The important information about the quadratic polynomial is that it has three real numbers called
coefficients
; one for each of the three terms,
$$ax^2 + bx + c$$
We need a way to keep 3 real numbers. An array is a
collection
of numbers in placed in a well-defined order. A
vector
is an array with only one
row
or one
column
. We can store three real numbers in a $1\times 3$ vector, coeffs$\ = [ a, b, c ]$. Here $1\times 3$ means one
row
and 3
columns
. Using this
data structure
we can represent each of the three quadratic polynomials above uniquely,
[ 1 0 5 ]
[ 3 -5 2 ]
[ -1 3 1 ]
Notice we have defined
Now that we have a data structure, we need an algorithm.An algorithm is a finite sequence of instructions that can be implemented to complete a task. In mathematics and computer science tasks and their algorithms must be
>> message = 'Hello World'
This command create a variable in the global address space named
message
. It assigns the
value
'Hello World'
to the variable. There are other ways to create this string
>> [ 'H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd' ]
>> message = [ 'Hello' ' ' 'World' ]
>> message = "Hello World"
>> message = "Hello" + " " + "World"
Most of these methods are not very useful. For our first MATLAB function let us emit this string to the MATLAB command window.
>> message = 'Hello World';
>> display('%s\n', message)
message = 'Hello World';
display(message)
Creative Commons Attribution-NonCommercial-ShareAlike 4.0
Attribution
You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
Noncommercial
You may not use the material for commercial purposes.
Share Alike
You are free to share, copy and redistribute the material in any medium or format. If you adapt, remix, transform, or build upon the material, you must distribute your contributions under the
same license
as the original.