Download as pdf or txt
Download as pdf or txt
You are on page 1of 57

Programming for Engineering System Design

Engineering System Design 2


ENGR10003

Programming for Engineering System Design


(includes slides by Shanika Karunasekera)

Adrian Pearce
(adrianrp@unimelb.edu.au)

Engineering System Design 2 1 Melbourne School of Engineering


Programming for Engineering System Design

In this section you will learn how to write computer


programs to solve engineering system design problems
using MATLAB

http://compumedicsneuroscan.com/using-
matlab-curry/

Engineering System Design 2 2 Melbourne School of Engineering


Programming for Engineering System Design

A bit about myself


• Adrian Pearce:
– Associate Professor in the School of Computing and Information
Systems

• Education:
– B. Sc. (First Class Honours) in Computer Science
• University of Melbourne
– PhD in Computer Science (Specialization: Artificial Intelligence)
• Curtin University of Technology

Engineering System Design 2 3 Melbourne School of Engineering


Programming for Engineering System Design

A bit about myself

• Academic Experience:
– Academic in the department from 2000 – to date
– Teaching
• Computer Graphics, Autonomy & AI Planning, Theory of Computer
Science, Logic Programming

– Research Interests
• Artificial Intelligence
• Automated Planning
• Optimisation
• Reasoning about Actions & AI Decision Making

Engineering System Design 2 4 Melbourne School of Engineering


Programming for Engineering System Design

Getting help

• Consultation:
– Regular consultation: Wednesday 3 pm – 3:45 pm
• Office: 6.20 Doug McDonell (DMD) Building
– Additional consultation by appointment
• Email: adrianrp@unimelb.edu.au

• ‘Ed’ Discussion Board

Engineering System Design 2 5 Melbourne School of Engineering


Programming for Engineering System Design

Objectives

• Upon successful completion of this section you will be


able to:
– develop programming solutions to engineering design problems
using the top-down approach to design;
– write computer programs to solve engineering design problems;
and
– understand the role of computer programs in solving complex
engineering problems.

Engineering System Design 2 6 Melbourne School of Engineering


Programming for Engineering System Design

References

• Programming for Engineering System Design - Lecture


Notes & Slides
(Available on LMS)

• Stormy Attaway, “ MATLAB A PRACTICAL APPROACH”,


Elsevier, 2009 ISBN 9780750687621

Engineering System Design 2 7 Melbourne School of Engineering


Programming for Engineering System Design

Overview

• Introduction to computer programming

• Role of computer programs in Engineering System


Design

• Introduction to MATLAB

• First MATLAB program

• Input/output in MATLAB

• Introduction to basic programming capabilities

Engineering System Design 2 8 Melbourne School of Engineering


Programming for Engineering System Design

Introduction to programming

• A computer program is a sequence of instructions


(commands) to perform a user-defined task

• The language used to write a computer program is called


a programming language
Engineering System Design 2 9 Melbourne School of Engineering
Programming for Engineering System Design

Role of computer programs in Engineering System Design

• Understand the behaviour of a engineering system


before building it
1. Develop a mathematical model of the system
2. Use the model in a computer program to mimic the system
behaviour – simulation

Solar Oven Simulation

Design Building Heating

Engineering System Design 2 10 Melbourne School of Engineering


Programming for Engineering System Design

Let us look at some motivating


examples in
Engineering System Design

Engineering System Design 2 11 Melbourne School of Engineering


Programming for Engineering System Design

Example: Chemical Process Alarm Generator

Temperature VT
Transducer
Alarm AOUT
Alarm
Ch Generator
Pressure VP
Transducer

Chemical Process
VTR VPR

Example taken from ESD 2 - Part 1

Engineering System Design 2 12 Melbourne School of Engineering


Programming for Engineering System Design

Example: Chemical Process Alarm Generator

Problem 1: Given VT ,VP , VTR and VPR what is AOUT ?

This is straightforward to compute because there is


specific solution

Engineering System Design 2 13 Melbourne School of Engineering


Programming for Engineering System Design

Example: Chemical Process Alarm Generator

Problem 2: Design a Chemical Process Alarm Generator


with probability of false alarms is less than 0.01.

– temperature and pressure measurements are performed by


devices which have a margin of measurement error

– therefore, the alarm generator will sometimes generate alarms


when neither the temperature nor the pressure had exceeded
the threshold – false alarms

This problem is easier to solve through writing a computer


program that mimics the behaviour of the system -
simulation.

Engineering System Design 2 14 Melbourne School of Engineering


Programming for Engineering System Design

Example: Analog-to-Digital Converter

Analog Digital
Signal v(t) Signal
Sampler Quantizer Encoder

Example taken from ESD 2 - Part 1

Engineering System Design 2 15 Melbourne School of Engineering


Programming for Engineering System Design

Example: Analog-to-Digital Converter

Problem 1: Given sampling frequency and the number of


quantization levels, how much memory is required to store
a song which is t seconds in duration?

This is straightforward to compute because there is a


specific closed form solution.

Engineering System Design 2 16 Melbourne School of Engineering


Programming for Engineering System Design

Example: Analog-to-Digital Converter

Problem 2: An engineer has built an A/D converter and now


wants to verify if it is working properly

– For a given input signal, a sampling rate, the number of quantization


levels, is the digital signal output from the A/D converter accurate?

Manually computing the output digital signal would be


tedious and therefore, this problem is easier to solve
through a computer simulation.

Engineering System Design 2 17 Melbourne School of Engineering


Programming for Engineering System Design

Example: Parachute design problem.

• The velocity v(t) at time t, of a falling parachute is given by


the following analytical equation:

v(t) = (gm/c)(1 – e-(c/m)t)

g - gravitational constant
m - mass
c - drag coefficient (depends
on the design of the parachute)

Problem 1: Find the velocity after t seconds - m, c, and g are


given

Engineering System Design 2 18 Melbourne School of Engineering


Programming for Engineering System Design

Click to edit
Example: Parachute
Master title
design
styleproblem.
• Problem 2: Design a parachute that attains a given
velocity v, after t seconds

v(t) = (gm/c)(1 – e-(c/m)t)

This problem can be solved using a numerical method


and is relatively straightforward to do using a computer
program.

Engineering System Design 2 19 Melbourne School of Engineering


Programming for Engineering System Design

Role of Computer Programs

• Computer programs have many uses beyond


engineering system design:
– general purpose computer applications
– as large scale software systems – Google, Facebook etc
– medical, e-Commerce applications
– many more

Engineering System Design 2 20 Melbourne School of Engineering


Programming for Engineering System Design

How can computer programs help solve problems

• Computer programs:

– Allow us to automate computational tasks, hence perform


them more efficiently than a human could do

– Can achieve repetition at high speeds

Engineering System Design 2 21 Melbourne School of Engineering


Programming for Engineering System Design

Computer Software, Hardware & Operating System

• Software: Computer programs are referred to as the


software – in this module you will learn how to write
software
• Hardware: The device that
runs the computer program
e.g. computers, smart phones

Von Neumann architecture

From: https://en.wikipedia.org/wiki/Von_Neumann_architecture
Engineering System Design 2 22 Melbourne School of Engineering
Programming for Engineering System Design

Click to edit
Computer Software,
Master title
Hardware
style & Operating System

Central processing unit Memory:


(CPU): – Used for storing program
– this is the brain of the data
computer

Engineering System Design 2 23 Melbourne School of Engineering


Programming for Engineering System Design

Computer Software, Hardware & Operating System

• Input-output Devices:
– used to communicate with the users and other systems

Engineering System Design 2 24 Melbourne School of Engineering


Programming for Engineering System Design

Click to edit
Computer Software,
Master title
Hardware
style & Operating System

Storage Media Directory (Folder)


Structure

Engineering System Design 2 25 Melbourne School of Engineering


Programming for Engineering System Design

Computer Software, Hardware & Operating System


• Operating System: Pre-installed system software that
help run programs we write
– e.g. Microsoft Windows, Unix, Linux, MAC-OS, iOS, Android

Computer Programs (Software) interacts with the outside


world (e.g. users) through input-output devices (hardware),
with the support provided by the operating system.

Engineering System Design 2 26 Melbourne School of Engineering


Programming for Engineering System Design

Programming Languages

• Many different programming languages exist today


– Basic, C, C++, C#, Java, Python, Fortran, Pascal, Algol,
Haskell, Prolog, Lisp, Smalltalk, Assembly, MATLAB etc

• Programming languages have many different


classifications:
– High-level vs Low-level
– Procedural, Object-oriented, Functional, Logic-based, Multi-
paradigm
– Interpreted vs Compiled
– Platform dependent vs platform independent

Engineering System Design 2 27 Melbourne School of Engineering


Programming for Engineering System Design

Computer Programs (Software)

• In the coming lectures you will learn how to write


computer programs using MATLAB.

• You will also learn general computing concepts and


develop computational thinking skills that will be useful
when learning any programming language.

Engineering System Design 2 28 Melbourne School of Engineering


Programming for Engineering System Design

Introduction to MATLAB

• Powerful modelling and simulation platform

• Widely used by engineers and scientists

• Supports a programming environment

• Has built in capabilities to make programming easy

• Has a large number of toolboxes that give additional


computational capabilities

Engineering System Design 2 29 Melbourne School of Engineering


Programming for Engineering System Design

Programming with MATLAB

• A MATLAB program consists a sequence of instructions


(commands)

• When a program runs, it executes these commands one


at a time

• A basic instruction:

disp(‘Hello World!’);

– displays the message Hello World! on the screen

Engineering System Design 2 30 Melbourne School of Engineering


Programming for Engineering System Design

A closer look at the disp command


disp(‘Hello world!’);

• The instruction has two parts:


– Command: in this case, disp is the command which tells the
computer to display something on the screen.
– Input Arguments:
• What is provided within parenthesis, in this case the phrase
Hello world! (within single quotes) is the input
argument. This will be what is displayed on the screen.
• The single quotes around the phrase indicates that it is a
string – you will learn this later.
• This command has only one input argument, but commands
can have more than one input argument as you will see later.
– The semicolon ends the command and is optional– we will see
the effect of the semicolon later.

Engineering System Design 2 31 Melbourne School of Engineering


Programming for Engineering System Design

Executing (Running) an instructions

• MATLAB instructions can be run in the interactive mode.

• Start MATLAB

• Enter the following on the command window.

>> disp(‘Hello world!'); <- what you type


Hello world! <- program output

Engineering System Design 2 32 Melbourne School of Engineering


Programming for Engineering System Design

MATLAB Programs (scripts)

• A program is a sequence of instructions

• You can type a command at a time (or multiple


commands) in the command window to execute the task.

• However, these instructions will be lost when you exist


MATLAB and restart.

• A program is a sequence of instructions that is readily


executed whenever one wants to – therefore, programs
are saved in files for later execution.

Engineering System Design 2 33 Melbourne School of Engineering


Programming for Engineering System Design

Building your first MATLAB Program

Let us try it out!

Engineering System Design 2 34 Melbourne School of Engineering


Programming for Engineering System Design

Hello World in MATLAB

• The first program that is learnt in any programming


language is the “Hello world” program:
– a program that prints a message “Hello world!” to the screen
(console).

• Let’s take a look at “Hello world” in MATLAB.

• In the following examples:


– The double right bracket >> is the MATLAB command prompt
– User input command is shown is slanted courier font
– MATLAB output is shown in bold courier font

Engineering System Design 2 35 Melbourne School of Engineering


Programming for Engineering System Design

Hello World in MATLAB

% HelloWorld.m
% This program writes a message to the console
% Version: 1.0 Date: 27/02/2014
% Author: Shanika Karunasekera(karus@unimelb.edu.au)

% Display a message on the console


disp('Hello world!');

>> HelloWorld
Hello world!

Engineering System Design 2 36 Melbourne School of Engineering


Programming for Engineering System Design

A closer look at – Hello World

• Lines 1- 4 and line 6 of the program are comments.


– Comments in MATLAB start with character “%”
– Comments are ignored by the computer – they are for the
benefit of the human readers of the program
– Commenting code for future readability and understandability is
strongly recommended
– Develop this habit from the beginning
– Include the author, version, date and a high level introduction at
the top
– The examples shown in the lecture slides will have minimal
commenting to fit the slide space - do not use programs in the
slides as examples of good commenting

Engineering System Design 2 37 Melbourne School of Engineering


Programming for Engineering System Design

When writing computer programs, strictly


following the rules of the language , referred to as
the SYNTAX , is important.

If you do not, the programs will either fail or will


not do what was intended.

Engineering System Design 2 38 Melbourne School of Engineering


Programming for Engineering System Design

Running (Executing) the program

• Start MATLAB by clicking on the icon.


• Use MATLAB editor to write the program:
1. Choose File->New-Script and the editor will open.
2. Type in the program lines in the editor.
3. Choose File->Save As and enter the name of the file as
HelloWorld and save – the program will be saved in a file
named HelloWorld.m which is a MATLAB script file. You
may choose any name for the file but always use a
meaningful name.
• Run the program:
1. Click on the green arrow on the top menu to run the program
OR enter HelloWorld on the command window prompt.
2. Observe the output on the command window.

Engineering System Design 2 39 Melbourne School of Engineering


Programming for Engineering System Design

Running (Executing) the program

• A MATLAB program is stored in a script file


(HelloWorld.m).

• MATLAB script files have a .m file extension.

• When you run the program, MATLAB interprets the


commands and performs the necessary actions to
complete the task based on the commands.

Engineering System Design 2 40 Melbourne School of Engineering


Programming for Engineering System Design

Receiving user input from the keyboard

• The first program (HelloWorld) demonstrated how the


disp() command in MATLAB was used to display a
string on the screen - console output.

• Programs can also accept user input.


– the next example will demonstrate how to accept user
input from the keyboard.

Engineering System Design 2 41 Melbourne School of Engineering


Programming for Engineering System Design

Receiving user input from the keyboard


% InputDisplay.m
% This program demonstrates how a program can
% accept user input from the keyboard
% Version 1.1 30/03/2015
% Author Shanika Karunasekera karus@unimelb.edu.au
% Prompt user for a number
a = input('Enter a number: ');
% Display a message on the console
disp(a);

>> InputDisplay
Enter a number: 3
3

>> InputDisplay
Enter a number: 3.5
3.5000
Engineering System Design 2 42 Melbourne School of Engineering
Programming for Engineering System Design

Receiving user input from the keyboard


• The first command:
a = input(‘Enter a number: ’);
accepts input from the keyboard.

• The input command three parts:


– Command: input tell the computer to accept input from the
keyboard.
– Input arguments: as with the display command Enter a
number: is an input argument that gets displayed on the screen.
– Output arguments:
• a is an output argument
• The value user entered is stored in a, in the computer
memory
Engineering System Design 2 43 Melbourne School of Engineering
Programming for Engineering System Design

Receiving user input from the keyboard

• The second command:


disp(a);
display the number user entered on the screen.

• The disp command:


– is the same as what you saw in the first example
– in this case, you will see that the input argument, a, does not have
single quotes around it as in the case of the first example
– this implies that the value that is carried in a, rather than the
character a, should be displayed.
– this command displays the value the user entered.

Engineering System Design 2 44 Melbourne School of Engineering


Programming for Engineering System Design

Receiving user input from the keyboard


• Now consider what happens if the user enters something
other than a number.

>> InputDisplay
Enter a number: hello
Error using input
Undefined function or variable ’hello’.
Error in InputDisplay (line 8)
a = input(’Enter a number: ’);
Enter a number:

• The syntax of the input command you used is only able


to accept numbers
– we will next see how the change the command to so that it can
accept numbers as well as other characters.

Engineering System Design 2 45 Melbourne School of Engineering


Programming for Engineering System Design

Receiving user input from the keyboard


% InputDisplay.m
% This program demonstrates how a program can
% accept user input from the keyboard
% Version 1.2 30/03/2015
% Author Shanika Karunasekera karus@unimelb.edu.au
% Prompt user for a character string
a = input(‘Enter a character string: ’, ‘s’);
% Display a message on the console
disp(a);

>> InputDisplay
Enter a character string : 3.5
Second input argument - tells the
3.5
computer to accept any character
String not just a number
>> InputDisplay
Enter a character string : hello
hello
Engineering System Design 2 46 Melbourne School of Engineering
Programming for Engineering System Design

Formatted output in MATLAB

• In the previous examples you saw the simplest form of


output command in MATLAB – the disp command.

• Sometimes it is necessary to display strings in a specific


customized format.

• Formatted output can be printed using the fprintf


command.

Engineering System Design 2 47 Melbourne School of Engineering


Programming for Engineering System Design

Formatted output in MATLAB


% FormattedOutput.m
% Prompt user for a number
a = input(‘Enter a number: ’);
% Prompt user for a string
b = input(‘Enter a character string: ’, ‘s’);
% Display a message on the console
fprintf('The number you entered is: %d \n', a);
fprintf('The string you entered is: %s \n', b);

>> FormattedOutput
Enter a number: 3
Enter a character string: hello
The number you entered is: 3
The string you entered is: hello

Engineering System Design 2 48 Melbourne School of Engineering


Programming for Engineering System Design

Formatted output in MATLAB


• The following command are used for displaying
formatted output on the screen:
fprintf('The number you entered is: %d \n', a);
fprintf('The string you entered is: %s \n', b);

Format Strings (%d and %s):


• When displaying the output on the screen, these will be replaced
with the value of the second input argument (a and b
respectively).
• The letter in the format string indicates the type of data in the
second argument
%d - integer
%f - float (a number with decimal points)
%c - character
%s - string

Engineering System Design 2 49 Melbourne School of Engineering


Programming for Engineering System Design

Formatted output in MATLAB

fprintf('The number you entered is: %d \n', a);


fprintf('The string you entered is: %s \n', b);

– Non-printable characters (\n):


• The letter n, preceded by \, called an escape character, makes this
pair non-printable characters, but has a special meaning when
displayed - \n is a new line.
• After displaying the first string, the next string is displayed on
a new line as a result of this.
• Following are other possible escape characters.
\b - Backspace
\f - Form feed
\n - New line
\t - Horizontal tab

Engineering System Design 2 50 Melbourne School of Engineering


Programming for Engineering System Design

Formatted output in MATLAB

>> a = 3;
>> b = ‘hello’;

>> fprintf(’The number is: %d and the string is: %s


\n’, a, b);

The number is: 3 and the string is: hello

>> fprintf(’The number is: %4d and the string is:


%s \n’, a, b);

The number is: 3 and the string is: hello

Engineering System Design 2 51 Melbourne School of Engineering


Programming for Engineering System Design

Formatted output in MATLAB

>> c = 4.2365
>> fprintf(’The floating point number is equal to
%f\n’, c);

The floating point number is equal to 4.236500

>> fprintf(’The floating point number is equal to


%0.2f\n’, c);
The floating point number is equal to 4.24

Engineering System Design 2 52 Melbourne School of Engineering


Programming for Engineering System Design

Getting help on MATLAB commands

• In you need more information about a command type the


following in the MATLAB help command cab be used.
e.g.
>> help fprintf

• If you did not know the name of the command, just type
help to get a categorized list of commands.

Engineering System Design 2 53 Melbourne School of Engineering


Programming for Engineering System Design

How do you learn programming?

• The only way you learn programming is by doing it


yourself

• You will probably find it hard and will make a lot of


mistakes at the start, but unless you do the programming
yourself you will not be able to learn programming

• MATLAB is available in the engineering labs

Engineering System Design 2 54 Melbourne School of Engineering


Programming for Engineering System Design

How do you learn programming?

• As a student, you can get MATLAB installed on your


personal computers following the instructions below:
– There is a directory called ‘MathWorks - Matlab 2014b’ in the L:
drive that students can access when logging into Lab PCs
– In this directory, you will find the required documentation, with
the Activation key info as well as Mathworks account and
installation info
– There is also a Patch directory to update the 2014b installation
to the current level

Get started with programming today and you will start


enjoying programming!

Engineering System Design 2 55 Melbourne School of Engineering


Programming for Engineering System Design

Building blocks of a computer program

• You have now learnt one fundamental capability or a


feature in programming:

Input-output: the ability to accept input from the keyboard


and displaying output on the console (you will learn more on
input-output later).

Engineering System Design 2 56 Melbourne School of Engineering


Programming for Engineering System Design

Building blocks of a computer program

• Following are four additional fundamental features that


you will require to learn to write more complicated
programs:
– Calculation – Perform computational tasks
– Functions (abstraction) – Creating self-contained modules
that can then be put together to create a program
– Selection (Making choices) - Branching
– Iteration (Doing repetitive actions) - Looping

• In the coming topics you will learn these!

Engineering System Design 2 57 Melbourne School of Engineering

You might also like