EL2310 – Scientific ProgrammingLecture 1: Introduction
Carl Henrik Ek([email protected])
Royal Institute of Technology – KTH
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Overview
Overview
Lecture 1: Introduction to the CourseIntroductionAbout the LanguagesGoals & Organization
Lecture 1: Introduction to MATLAB
About MATLAB
Getting StartedBasic CommandsVectors and Matrices
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Welcome
� Lecturer: Carl Henrik Ek� Course overview
� 16 lectures (2 x 45 min. each)� 3 labs� 3 project assignments
� 7.5 credits� Grade: Pass / Fail� EL2310 and EL2800 refer to the same course!
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Content
� Part I - MATLAB
� Part II - C
� Part III - C++
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Content
� Part I - MATLAB
� Part II - C
� Part III - C++
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Content
� Part I - MATLAB
� Part II - C
� Part III - C++
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Course homepage
� http://www.csc.kth.se/˜chek/teaching/EL2310/
� Place to find the latest information.� You can download slides from the lectures.� Lab notes and project descriptions can be found here.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Bilda
� Online learning tool http://bilda.kth.se� Assignments� Feedback and Discussions
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Introduction
Motivation for the course
� Programming is a key competence for todays engineers.� Some courses depend on you being able to program,
� Programming will be a tool not subject of study.� Tailored for Master students as it starts with MATLAB.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About the Languages
Why MATLAB?
� MATLAB is a tool for interactive numerical computations.� Focus on rapid prototyping for task with complex computations.� Extensive code-base in a wide range of fields such as,
� control,� signal processing,� optimization,� image processing.
� Tool to visualize and analyze data.� It is used in many engineering companies, and also extensively
at KTH.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About the Languages
Why C/C++?
� It is used extensively in industry and in academia.� Allows “closer” interaction with hardware.� Real-time applications mostly use C/C++, JAVA is getting there
now.
� Free compilers availible for most hardware.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About the Languages
MATLAB vs. C/C++
MATLAB:� Interpreted+ Fast developing time- Slow run-time+ PortableC/C++:� Compiled- Slower developing time+ Possible to write fast programs
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About the Languages
Programming environment
� We will not use an IDE (Integrated Development Environment)for C/C++.
� For C/C++, the tools are gcc (compiler) and emacs (editor).� An IDE “hides” things you will need to know.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Your background
� You are no expert on MATLAB and C/C++.� You may know some programming.� You have basic knowledge in linear algebra and calculus.� You want to learn!
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Goals are to
� have an understanding for basic concepts in programming.� be able to read, process and display data in MATLAB.� solve problems and implement algorithms in MATLAB.� be skilled enough using MATLAB, so it does not pose a
problem in other courses that require knowledge of MATLAB.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Goals cont.
� be able to read and process data in programs written in C andC++.
� solve problems and implement algorithms in C and C++.� understand what simpler programs written in C or C++ do, i.e.
be able to read and understand existing code.� have an understanding for the importance of writing code which
others can understand, change, correct and build upon.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Lectures
� covers the basics of programming,� starts with MATLAB, continues with C and finishes with C++.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Labs
� One for each language, i.e. MATLAB, C and C++ to� get familiar with the UNIX/Linux computers at CSC, and� prepare you for the projects.� Co-operation is encouraged.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Projects
� The projects will have a robotics theme.� So, you will learn something more than just programming.� The projects should be solved individually.� Deadlines will be announced soon
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Value of feedback
� The quality of the course depends on your feedback!� Not only at the end of the course (evaluation), but during the
course.� Use Bilda as mode of interaction NOT email.� Important: This course can not be tailored for everyone, since
your backgrounds vary dramatically.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Self study course
� The lectures and labs can show you the basics, but if you wantto learn, you need to study on your own.
� MATLAB is available on “KTH-CD”� http://progdist.ug.kth.se/public/
� Tools for C/C++ are available with all Linux distributions� See course website
� Strongly recommended that you use Linux.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
System
� For the C/C++ part we cannot support all systems� Free open-source programs (i.e. Linux)� Environments
� Own system� Virtual Machine through http://www.virtualbox.org/� CSC Computers� OS X
� Your assignments will be checked in Virtual Machine
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Registration
If you are registered you should be able to,� Log into Bilda http://bilda.kth.se� Have access to the CSC computers.If not let me know.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
Literature
� No course book in the normal sense� Very much and good information available online.� Share valuable resources with each other on Bilda.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Goals & Organization
End of part 0
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About MATLAB
Part I - Introduction to MATLAB
� MATLAB background� Basics� Interactive calculations� Matrices and vectors
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About MATLAB
Acknowledgements
� The lectures on MATLAB are partially based on material from� Mikael Johansson, EE/KTH (course 2E1215)� Fredrik Gustavsson, Linkoping (course TSRT04)
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About MATLAB
Matlab background
� MATLAB = MATrix LABoratory� Commercialized 1984 by Mathworks� Heavily extended since then� A standard tool today� Makes numerical computations easy
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
About MATLAB
Alternatives
� There are alternatives such as� Octave (free and very similar)� Scilab� Lyme� NumPy� Matrix-X
� Symbolic complements� Maple� Mathematica
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Getting Started
Running MATLAB
� More hands-on experience during the first labs� Available for Windows, Unix/Linux, Mac
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Getting Started
MATLAB construction
� Core functionality based on compiled C-routines� Most functionality given as .m-files� Grouped into toolboxes� .m-files
� contain source code� can be copied and altered� are platform independent (same on PC, Unix/Linux, Mac)
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Getting Started
Interactive calculations
� You do not need to declare variables in MATLAB
� It is interactive
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Basic Commands
Documentation
� Help with syntax and function definitions>> help <function>Ex: “help sin”
� To look for a function with unknown name>> lookfor <keyword>
� Advanced hyperlinked help systemhelpdeskCan also be accessed through the “Help” menu item
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Basic Commands
Variables
� Look at what variables are defined with>> who>> whos
� Clear variables with>> clear [variable(s)]
� Suppress output with ending “;” (semicolon)
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Basic Commands
Loading and saving variables
� You can save all variables in memory with>> save <filename>
� To save some variables do>> save <filename> var1 var2 ... varN
� You can load them back into memory with>> load <filename>
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Basic Commands
Making a diary
� You can use the function diary to record what you are doing� Allows you to go back and check what commands were issued� Start the diary with>> diary [filename] or >> diary(’filename’)without the filename argument the diary file will be called “diary”
� You can also call>> diary on
� To stop the diary call>> diary off
� If you call diary without an argument you toggle diary on/off
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Vectors
� Matrix and vector operations are at the very core of MATLAB
� For speed try to formulate a problem in terms of matrixoperations
� Vector v = [ 1 2 3 4 ] is defined by>> v=[1 2 3 4];
� Vector w =
1234
is defined by
>> w=[1; 2; 3; 4];
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Vectors cont’d
� Can create a vector with “colon-notation”>> v = start value:step:end value
� Ex: To create a vector with number 1 3 5 7 you do>> v = 1:2:7
� Notice that step can be negative to create for example 7 5 3 1>> v = 7:-2:1
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Indexing vectors
� To access a certain value in a vector do>> v(i)where i is the index of the value
� Note: All indices start at 1 in MATLAB.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Matrices
� Matrices (2D arrays) are defined similarly
� Matrix A =
[1 2 33 5 6
]is defined by
>> A = [1 2 3; 3 5 6];
� Note: MATLAB is case sensitive
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Dimensions
� You can check the size of a matrix with >> size(A)which will return the number of rows and column
� You can also ask specifically for the number of rows andcolumns
� To get number of rows>> size(A,1)and number of columns>> size(A,2)
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Matrix operations
� You can use all common operators with the matrices such as>> C = A + B;or>> C = A * B;assuming that the involved matrices have the right dimensions.
� You can mix scalars and matrices such as>> C = A + 2;in which case the scalar adapts to fit the situation (here it willexpand to a matrix of the same size as A with all elementsequal to 2).
� Even functions like sin and cos can be applied to matrices inwhich case they operate on each element.
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Matrix transpose
� To transpose a matrix do>> B = A’
� Note that the transpose will conjugate complex entries� To avoid this use>> B = A.’
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Indexing Matrices
� Index individual elements with>> A(i,j)where i is the row and j is the column
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Indexing Matrices cont’d
� Index sub-matrices>> A([1 3],[2 3])
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Indexing Matrices cont’d
� Sometimes convenient with single index notation� Matrix elements ordered column by column
A =
a1 a4 a7a2 a5 a8a3 a6 a9
that is, A(n) = an with the above ordering
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Indexing Matrices cont’d
� You can go from subscripts (i , j) to single indices.� You can do it for an array of subscripts
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Wrap Up
Today:� Introduction to the Course� Introduction to MatlabNext time (Thursday, 15-17, Q17):� Matlab as a Tool
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming
Lecture 1: Introduction to the Course Lecture 1: Introduction to MATLAB
Vectors and Matrices
Todo
� Log into Bilda� Familarise yourselves with the course page
Carl Henrik Ek, Patric Jensfelt Royal Institute of Technology – KTH
EL2310 – Scientific Programming