Solving problems by computer

Computers are tools which enable us to solve a wide range of problems, both numeric and textual. The purpose of this course is to teach you the rudiments of numerical computing, starting with how to write programs and leading into specific numerical techniques for a variety of problems in Chemical Engineering.

You have already gained experience in using computers. In particular, you have learned how to write documents using a word processor such as Microsoft Word, how to do simple calculations using a spreadsheet calculator such as Microsoft Excel, and how to solve more complex problems using specific tools such as GAMS. This course will help you learn how to develop your own applications, although they will be much simpler than those you have already seen (as they involved large teams of programmers to develop over many years!).

There are three steps in writing a program:

  1. Defining the problem you want to solve,
  2. Writing an algorithm which describes the steps need to solve the problem, and
  3. Writing the actual program to solve your problem.

Each of these steps is now discussed in more detail.

Problem definition

The first step, and arguably the most important, is to properly define the problem to solve. It is difficult to write a program if the problem itself has ambiguities. Computers are not particularly good at handling ambiguities: they need to have everything spelled out!

Example problem

You've been out all evening celebrating the fact that your first program ran successfully. You've come home and suddenly realize that you are hungry. As it's quite late, and you possibly do not have the coordination for anything more complex, you figure that beans on toast is suitably filling and appetising. The problem is to make this dish.

What constitutes a precise problem description for this example? As this is a simple problem, we can summarise it as

Prepare a suitable amount of beans and toast, put the beans and toast together on a plate, and find a place to eat them.

As mentioned, this is a simple problem so the problem description is both< short and easy to formulate. Real problems will of course be more difficult to state clearly but it is important that this be done before going to the next step.

Writing an algorithm

An algorithm is essentially the same as a recipe: it states the steps necessary to solve a particular problem. Algorithms can vary greatly in the amount of detail presented and the language used to write them. The amount of detail depends on both the problem itself and the person which will be responsible in translating the algorithm to an actual program.

We will continue with our beans on toast example by writing a fairly detailed algorithm which will enable us to make our dinner should the occasion arise.

Algorithm BeansOnToast
   "the first step is to locate all the necessary ingredients"
   open cupboard and look for can of beans
   if no beans found then
      run to corner shop and buy some
   end if
   open bread bin and find suitable, non-moldy, slices of bread
   if no bread found or all of it moldy then
      run to corner shop again and buy some
      buy some milk for tomorrow's morning tea while there
   end if
   "okay, we've got the ingredients. Now prepare for the actual cooking"
   find saucepan for beans
   open can of beans and pour into saucepan
   find a toaster
   if no toaster then
      check for grill
      if no grill either then
         give up and go to sleep hungry
      end if
   end if
   "everything is ready. Go to it!"
   turn cooker on
   put beans on to heat up
   put bread in toaster (or in the grill)
   while beans not done and bread not done do
      if toast ready then
         get butter from fridge (hopefully there is some!)
         butter toast
      end if
      if beans ready then
         turn heat to low
      end if
   end while
   "everything is now ready"
   turn cooker off (including grill if used)
   put toast on plate
   pour beans on toast
   sit in front of TV to eat your beans on toast!
end Algorithm

And you thought that preparing beans on toast was easy!! Well, writing computer programs requires this level of detail. Computers need to be told exactly what to do. The purpose of an algorithm is to write down all the steps necessary for solving a problem in enough detail to make sure everything is there but without using a computer language. The use of (what is commonly known as) pseudo-English should make an algorithm much easier to read than a computer program (although a well written program should not be much more difficult to read than an algorithm, especially if comments are used liberally and correctly -- comments will be discussed later).

There are no rules when it comes to writing an algorithm. An algorithm should be written so that you understand each and every step, the order of these steps, and how they interact. If done properly, writing a program is simply a matter of translating each step in the algorithm to one or more lines of program code.

Writing a program

A computer program consists of a series of instructions to the computer. These instructions, which may tell the computer to add two numbers together or to write a message out to the user, are written in a high level language. In this course, we will be using a language known as MATLAB Following is an example program, one which takes two numbers from the user and prints out the result of multiplying these numbers together.

% Simple Program
x = input('Input value of x please '); % get the first number
y = input('Input value of y please '); % and the second number
z = x * y;                             % compute product
z                                      % show the result to the user

The syntax of MATLAB will be discussed in later lectures. This example is here simply to give you an indication of what a program looks like (so you'll be able to recognise such a beast if you run into one in a dark lane...).

Click HERE for next section.