Wichita State University Logo

Math 451: Computational Mathematics

1.4 Elements of Computer Programming


1.4.1 What is Computational Programming?

Computational Programming is an area of Software Development. We will use mathematical models, computational models and computer simulations to

  • solve mathematical problems
  • gather data
  • compute complex measurements
  • analyze physical and abstract systems
  • illustrate numerical associations
  • control automated processes

1.4.2 Computational Mathematics


Computational Mathematics is the mathematics of data structures and algorithms used to create computational software. This includes

  1. Decomposing a difficult or complex process into smaller tasks and repeating this process until the resulting basic tasks can be implemented with simple algorithms.
  2. Recombining these basic tasks, perhaps with the help of a flow chart, into a framework of algorithms that accomplishes the original complex process.
  3. Recognizing patterns among algorithms so that similar tasks can be implemented with a single algorithm whenever possible.
  4. Determining on the information or data that is relevant to each task and to the complex process.
  5. Organizing the data relevant to each task so that applying the algorithm to the data completes each task efficiently.
  6. Recognizing patterns of data and data structures so that different tasks can share the same data.
  7. Creating abstract data types and algorithms that can be used in a wide variety of computational processes.

These is the core objectives of our course. Any modern programming language can be used to implement our data structures and algorithms.

1.4.3 Why MATLAB ?


MATLAB is an Integrated Development Environment ( IDE ).

  1. The MATLAB language has all the programming elements we will need to implement our computational algorithms
  2. The MATLAB IDE has an integrated debugger that allows us to analyze our programming code to make sure it implements our algorithms faithfully.
  3. The MATLAB IDE includes a large number of built-in algorithms that we can use.
  4. The MATLAB IDE includes symbolic computation.
  5. A large number of independently created libraries of algorithms have already been written by other computational programmers.
  6. The MATLAB language and built-in programs provide extensive data analysis and visualization tools.
  7. The MATLAB language is an interpreted language. We will see that this feature allows us to start computing results very quickly and easily.
  8. The MATLAB language natively includes array computations. This will take some time to understand but will allow algothithms to be implemented more easily in MATLAB than many other modern languages.

1.4.4 Computer Languages

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

1.4.5 Identifiers

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.

1.4.6 Literals and Constants

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.

1.4.7 What is Programming?

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.

1.4.8 Data + Algorithm = Program

Example 1 - Baking a Cake

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

  1. butter and flour for coating and dusting the cake pan
  2. 3 cups all-purpose flour
  3. 3 cups granulated sugar
  4. 1 1/2 cups unsweetened cocoa powder
  5. 1 tablespoon baking soda
  6. 1 1/2 teaspoons baking powder
  7. 1 1/2 teaspoons salt
  8. 4 large eggs
  9. 1 1/2 cups buttermilk
  10. 1 1/2 cups warm water
  11. 1/2 cup vegetable oil
  12. 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.

  1. Preheat oven to 350 degrees Fahrenheit. Butter three 9-inch cake rounds. Dust with flour and tap out the excess.
  2. Mix together flour, sugar, cocoa, baking soda, baking powder, and salt in a stand mixer using a low speed until combined.
  3. Add eggs, buttermilk, warm water, oil, and vanilla. Beat on a medium speed until smooth. This should take just a couple of minutes.
  4. Divide batter among the three pans. I found that it took just over 3 cups of the batter to divide it evenly.
  5. Bake for 30-35 minutes in a 350 degree oven until a toothpick inserted into the center comes out clean.
  6. Cool on wire racks for 15 minutes and then turn out the cakes onto the racks and allow to cool completely.
  7. 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 .

1.4.9 Data Structures

We create a data structure when we organize data by defining precisely

  • what data types are employed
  • what each data element represents
  • what relationships the data elements have between them
  • how functions or operations may be applied to the data elements
  • how the data is presented in the programming language

A data structure is also called an model or a mathematical model . Let us start with a simple data structure.

Example 2 - Computing the Roots of a Quadratic Polynomial

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

  • three data elements of type double precision floating point
  • each data element represent coefficients of a quadratic polynomial from left to right, the leading coefficient, the linear coefficient and the constant coefficient, respectively
  • the data elements are stored in a $1\times 3$ array
  • we have defined no operations to combine or compare instances of this data type
  • the data structure is declared as a $1\times 3$ array of double precision floating point numbers in MATLAB

1.4.10 Algorithms

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

  • well-defined ; it must assign a unique interpretation to a value, instruction or expression.

  • implementable using a computer language; in this class the MATLAB computer language

  • consist of a finite number of steps

  • have an unambiguous beginning and end

  • be representable as a flowchart ; in this class a program flowchart


    A simple data sructure is a string . A string is an array of ascii characters. For example
>> 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)
In [2]:
message = 'Hello World';
display(message)
message =

    'Hello World'


Creative Commons Logo - White


Your use of this self-initiated mediated course material is subject to our Creative Commons License .


Creative Commons Attribution-NonCommercial-ShareAlike 4.0

Creative Commons Logo - Black
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.

Creative Commons Logo - Black
Noncommercial
You may not use the material for commercial purposes.

Creative Commons Logo - Black
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.