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

Programming

Prof. Dr. M. Krauledat

Faculty of Technology and Bionics

1
Introduction

Dr. Matthias Krauledat


Professor for Informatics

Phone: 02821 80673 625


Room: 06.03.027
Email: matthias.krauledat@hochschule-rhein-waal.de

Consultation hour: See moodle profile

2 Programming, M. Krauledat
Schedule
Week Topic of Lecture
1 Organizational Matters, Algorithms, Matlab as a Calculator
2 Vectors and matrices
3 Graphics, File I/O
4 Data types
5 Scripts and Functions
6 Selection Statements
7 Loop Statements
8 Nested Loops
9 Recursion
10 Advanced Graphics
11 Function Handles
12 Symbolic Mathematics
13 Polynomials
14 Debugging

3
Moodle course

Programming WS 23/24

Password: h4ck1n6w523

4
Introduction to Algorithms

In this course, we‘ll have two (concurring)


objectives when using the programming language
MATLAB:
1. Programming Concepts
2. Efficient use of built-in functionality

Let‘s start with the programming concepts!

5
History of algorithms

That guy

(Abu Abd Allah Muhammad


Ibn Musa al-Khwarizmi,
780-835)
published the „Rules of
restoring and equating“

Source: wikimedia commons


…hence the name
„algorithm“.

6
History of algorithms

The study of algorithms was originally a subject in


mathematics.
Early examples of algorithms
• Long division algorithm
• Euclidean Algorithm
Gödel's Incompleteness Theorem: Some
problems cannot be solved by algorithms.

7
Simple algorithms: Experiment 1

I‘ll now show you an image.

Decide whether or not there is a blue square in it!

8
9
Experiment 1

Was there a blue square?


• Just for the record: NO!
• It‘s a square, but it‘s not blue

10
Experiment 1: A possible solution

11
Experiment 1: A different solution

12
Experiment 2

I‘ll now show you some numbers.

At the end, tell me the largest number that you


saw!

13
Experiment 2

31
14
Experiment 2

42
15
Experiment 2

23
16
Experiment 2

1
17
Experiment 2

87
18
Experiment 2

7
19
Experiment 2

END
20
Experiment 2

Now, what was the largest number?


(Just for the record: 87)

What‘s the strategy here?


• Keep track of the „largest number seen so far“
• Only look at the next number in line and compare
to our „largest“ number.
• Exchange „largest number“ if necessary.

21
A possible algorithm

22
What is an algorithm?

Terminology.
Algorithm: A set of steps that defines how a task is
performed
Program: A representation of an algorithm
Programming: The process of developing a
program
Software: Programs and algorithms
Hardware: Equipment

23
What is an algorithm?

Definition (D. Knuth):


An algorithm is a finite set of rules that gives a
sequence of operations for solving a specific type
of problems. It has the following features:
1. Finiteness
2. Definiteness
3. Input
4. Output
5. Effectiveness

24
What is an algorithm?

Checklist for algorithms:

Finiteness* Terminates after a finite number of steps


Definiteness Each step is precisely defined
Input 0 or more quantities given to it
Output 1 or more quantities produced by it
Effectiveness The single steps are sufficiently „basic“

* otherwise, the procedure is sometimes called „computational method“.

25
What is an algorithm?

Example:

Counting the number of people in a room.


1. Start with a count of 0.
2. For each person in the room:
1. Point at the person
2. Add 1 to the count

26
What is an algorithm?

Exercise:
Use the „checklist for algorithms“ to show that
„Counting the number of people in a room“ is really
an algorithm!

Animation

27
What is an algorithm?

Example:

Counting the audience, Version 2.


1. Stand up. Your number is „1“.
2. As long as there is more than 1 person standing:
1. Find 1 other person
2. Replace your numbers by the sum of both your current numbers
3. One of you sits down.

28
What is an algorithm?

Exercise:
Use the „checklist for algorithms“ to show that
„Counting the audience, Version 2“ is really an
algorithm!

29
What is an algorithm?

Example: Socks drawer


Goal: find a matching pair of
socks.
1. Put on one sock randomly
2. Put on a second sock
randomly
3. As long as you don‘t wear
a matching pair:
1. Put back the second sock
2. Put on a new sock randomly

Source: greatspaceorganizing.com

…is this a (good) algorithm?


30
What is an algorithm?

Example:
How do you find the name
of a person in a
telephone book?

Source: www.kba.com

31
What is an algorithm?

Example:
Phonebook, Version 1.
1. Read the first name.
2. As long as you didn‘t
find the correct name:
• Read the next name.

Source: www.kba.com

32
What is an algorithm?

Exercise:
Phonebook, Version 2.
1. Open the phone book
somewhere in the middle and
look for the name.
2. If the wanted name is „larger“:
throw away the lower half of the
book and start over.
3. If the wanted name is „lower“:
throw away the upper half of
the book and start over.

Source: www.kba.com

33
What is an algorithm?

In Computer Science, there


are problems closely
related to the „phonebook
search“:
1. Searching
2. Sorting

OUTLOOK: We’ll do this in


Advanced Programming
(EL/SE students only). Source: www.kba.com

34
Engineering Software
R Statistical Computing:
Free software environment for statistical
computing. Tons of available modules and
software downloads via the integrated
package manager.
http://www.r-project.org

Microsoft Excel:
Calculations with spreadsheets. Easy data
sharing with „non-engineering“ colleagues.
Industry standard on office PCs. Extended
functionalities by macro programming
(Visual Basic).

35 Programming, M. Krauledat
Engineering Software
Scilab: Free open source software for
numerical computation
www.scilab.org

GNU Octave: Free high-level language,


primarily intended for numerical
computations
http://www.gnu.org/software/octave/inde
x.html

Also: MAPLE, Mathematica, Python,…

36 Programming, M. Krauledat
What is MATLAB?

• Short for MATrix LABoratory


• Developed by Cleve Moler and Jack Little in the 70s
• Distributed by The Mathworks (www.mathworks.com)
• Student version is available for ~ 70 EUR
• Free for students of HSRW: instructions on the website

37 Programming, M. Krauledat
What does MATLAB offer?

• Pros:
− Easy to use: on-line documentation, manuals, workspace browser,
extensive demos
− Platform independent: runs on different operating systems
(Windows, Linux, Unix, Mac)
− Extensive library of predefined functions
− Device-independent plotting
− Allows GUI programming for simple software interaction
− Standard in industry and academia
− Standardized interfaces to other software
− Rapid prototyping: no compilation step required

38 Programming, M. Krauledat
What does MATLAB offer?

… a great range of graphical tools. Ideal for visualization of


measured data (or models) and for further analysis.

39 Programming, M. Krauledat
What is MATLAB?

• Cons:
− Computation time: interpreted language which runs
slower than compiled language, proper structure of
MATLAB program required
− High cost: 5-10 times more expensive than C or Fortran
compiler
− Software only runs on computers where MATLAB is
installed, “software roll-out” not feasible

40 Programming, M. Krauledat
Literature

Available in the Library:


Stormy Attaway: Matlab - A practical introduction to
programming and problem solving. Elsevier.

Stephen J. Chapman: Essentials of MATLAB Programming. Cengage Learning. 2009.


William J. Palm: Introduction to MATLAB for Engineers. McGrawHill. 2011.

http://www.mathworks.de/moler/chapters.html
http://www.mathworks.com/academia/student_center/tutorials

… many other tutorials available

41 Programming, M. Krauledat
Prerequisite: Typing

Programming requires you to write code in an editor. You will have to use
a computer keyboard efficiently!

Learn to type!

Online resources:
• http://www.typingweb.com (Courses and tests, including special
characters and entire phrases)
• http://www.funtotype.com (Games for improving speed and accuracy
of typing)

42 Programming, M. Krauledat
Homework: Typing

Learn to type using


instructions from
http://www.typingweb.com

• Go to http://funtotype.com
• Play „Keyboard Ninja“ at „Hard Level“
• Use 10 finger system on „all letters“
• Don‘t stop until you break 1000 points!
• Post your highscore on moodle!

43 Programming, M. Krauledat
Checklist

1. Enroll in a PT group
2. Get your PC Pool badge
3. Complete „Pre-Quiz 1“
4. Download and install Matlab

5. …and learn to type!

44 Programming, M. Krauledat

You might also like