0% found this document useful (0 votes)
2 views

IE141_Distance_Education_Notes_Week_3

The document provides an overview of MATLAB and GNU Octave, highlighting their functionalities as programming platforms for engineers and scientists. It explains the features of both software, including their command interfaces, file handling, and variable management. Additionally, it covers basic operations, data types, and resources for further learning about MATLAB.

Uploaded by

Sevim Köse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

IE141_Distance_Education_Notes_Week_3

The document provides an overview of MATLAB and GNU Octave, highlighting their functionalities as programming platforms for engineers and scientists. It explains the features of both software, including their command interfaces, file handling, and variable management. Additionally, it covers basic operations, data types, and resources for further learning about MATLAB.

Uploaded by

Sevim Köse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

IE141 Computer Programming I

2021–2022 Fall
Distance Education Notes

Week 3 (12.10.2021)

Asst. Prof. Tolga Ulaş Gürbüz │ Asst. Prof. Mehmet Merkepçi


IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

 What is MATLAB?
• MATLAB® is a commercial programming platform designed specifically for engineers and scientists. The name MATLAB
stands for MATrix LABoratory. The heart of MATLAB is the MATLAB language, a matrix-based language. MATLAB is also a
numerical computing environment and by using it you can perform tasks such as matrix manipulations, plotting of functions and
data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages. The
language, apps, and built-in math functions enable you to quickly explore multiple approaches to arrive at a solution.

• Engineers and scientists in industry and academia use MATLAB. As of 2020, MATLAB has more than 4 million users worldwide.
You can use MATLAB for a range of applications, including deep learning and machine learning, signal processing and
communications, image and video processing, control systems, test and measurement, computational finance, and
computational biology.

Sources: https://www.mathworks.com/discovery/what-is-matlab.html
https://uk.mathworks.com/content/dam/mathworks/handout/2020-company-factsheet-8-5x11-8282v20.pdf
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

An example of a typical MATLAB screen:


IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

 GNU Octave
• GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command line
interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language
that is mostly compatible with MATLAB. .

• GNU Octave is also freely redistributable software. You may redistribute it and/or modify it under the terms of the GNU
General Public License (GPL) as published by the Free Software Foundation.

• Similar to MATLAB, GNU Octave has a powerful mathematics-oriented syntax, which is largely compatible with MATLAB, and
built-in 2D/3D visualization tools.

Source: https://www.gnu.org/software/octave/index
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

Octave’s GUI looks quite similar to MATLAB’s GUI:


IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

File Browser: You can browse through the files in a working directory and change the names. You can run an .m file by
clicking on the file. The file opens in the Editor window and can be run from there.
Workspace: It stores the variables names, values, and their properties like types and sizes. It is useful for developers to
visualize the variables and their contents. The meaning of variables and their values, sizes, etc. is illustrated in subsequent
chapters.
Command History: It stores the commands used in an Octave session. A command can be run by simply clicking it in
command window. It is then executed at Octave command prompt.

All three panes are optional and can be closed down for a session by clicking the cross sign in the upper-right.

On the right side, there is a pane named Command Window. The bottom part of the Command window includes three tabs:
Command window
Editor
Documentation
The Command window takes input one line at a time.
The Editor window is used to write an “.m script file” that can then be executed.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

• The Documentation window can be used to read documentation and seek help to learn more about
commands. Octave has an extensive documentation that enables a beginner to learn Octave with nothing but
a command line. It also helps an experienced user who can seek help in using less common commands.

• Sometimes you’ll need to obtain a clear screen, which is what the clc command does.

• When you start an Octave session, you can work in an interactive session in the sense that the Octave prompt
>> waits for you to input a command, which will be executed as soon as you press the Enter key at the end of
command.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

Working with Files


• Apart from working on Octave command prompt you can write multi-line programs using the built-in text editor in
Octave and run the program.

• This can be created by typing edit helloagain at the Octave command prompt. A new file called helloagain.m
will be created in a folder/directory in which the present session of Octave is running.

• Alternatively, the program can also be created in the editor by clicking on the lowermost part of the Command
Window, which has an option named Editor. This opens a blank editor window in which the helloagain.m code
can be written manually. You can then save the file using Ctrl+S.

• Note that all Octave script files are saved with an .m extension. You can open the existing file by navigating to
the appropriate folder and choosing the file in the explorer.

Example:

disp("\nHello World!\n")
disp("Hello again\n")

The helloagain.m file


The \n character in the string input is used to print a newline character, which simply adds a paragraph return
and prints the next characters on a new line.
The disp() function prints the string at the command prompt.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

You have many options for running an Octave file:


You can simply type the name of file (without the extension) at the Octave command prompt. For
example, by typing helloagain.
From the Editor menu, you can click on Run and choose Save File and Run.
You can also choose to click the given key combination. It prompts you to save the file if the script file is
being run for the first time. You can choose to save the file at a chosen destination within the local
computer’s storage.
In any case, the output is displayed at the command prompt, unless graphical output is directed to a graphical
terminal.

These two methods of working with Octave (using the command prompt and using files) each has its own
merits and usage. Interactive sessions are best for quickly checking for a small part of complex code. Files are
best with a project involving detailed calculations and are linked with one another to perform a computational
task.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

Using the Workspace

• A workspace is the space in the memory reserved for objects in the Octave session.
• All the objects used in calculations are displayed. This is usually placed as the second option in the left panel of
the main Octave session window.

• The command clear clears all global and local variables in the workspace and makes it fresh, just as when an
Octave session is initially launched.

• If the semicolon symbol ; is used at the end of a command, the output is not displayed upon the execution of the
command. This is useful when you expect too much output would be displayed. For example, when you are
dealing with a multitude of data points, say a million data points, it would be pointless to invest time and
computer memory in displaying them at Octave’s command prompt. This feature can also be used within
Octave scripts, when you don’t want to print a particular output at the command prompt.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

Octave as a Calculator
• In its simplest form, Octave works as a calculator with mathematical operators like multiplication (*), division
(/), addition (+), subtraction (-), and exponentiation (^). The following code illustrates this behavior:

>> 3+5
ans = 8
>> 3.0+5.0
ans = 8
>> 3.1+5.0
ans = 8.1000
>> 2-3
ans = -1
>> 3.0*5
ans = 15
>> 2/3
ans = 0.66667

• When a command is entered at the Octave command prompt >>, it is executed and an answer is displayed in
the next line as ans =.
ans is a global variable that stores the value of the last executed expression.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

• A number of physical constants are defined as follows: pi, e (Euler’s number), i and j (the imaginary number −1 ),
inf (infinity), and NaN (not a number, which results from undefined operations such as Inf/Inf).

>> pi
ans = 3.1416
>> e
ans = 2.7183
>> i
ans = 0 + 1i
>> j
ans = 0 + 1i
>> Inf/Inf
ans = NaN

• A number of built-in mathematical functions exist in Octave. A few of the more common ones are:
Absolute value abs()
Natural logarithm log()
Base-10 logarithm log10()
Trigonometric functions sin(), cos(), and tan(). Arguments are taken in radians.
Inverse-trigonometric functions asin(), acos(), and atan().
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

Using Variables

• Until now, we have been feeding numbers into Octave with on-the-spot evaluation. Alternatively, you can
designate a memory location where values are stored and this memory location can be known by a name for
ease of usage. Such a programming construct is known as a variable.

• To store values temporarily (in RAM), you use variables that store the value at a particular memory location and
address it with a symbol or set of symbols (called strings). For example, you can store the value of 1/10*pi as a
variable a and then use it in an equation:

>> a=1/10*pi
a = 0.31416
>> aˆ2 + 10* sqrt(a)
ans = 5.7037

• Multiple assignments can be performed using the comma (,) operator. Also if you do not want to produce results on-
screen, you can suppress this by using the ; operator. Try the following commands:

>> a1 = 1, a2 = 10, a3 = 100


>> a1 = 1, a2 = 10, a3 = 100;
>> a1 = 1; a2 = 10; a3 = 100;
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

• While assigning data to a variable, it is important to understand that data can be defined as a variety of objects
defined by a data type, as follows:

Logical: This type of data stores boolean values 1 or 0. Boolean values can be operated on by boolean
operators, like AND, OR, XOR, etc.
Char: This type of data stores alphabetic characters and strings (groups of characters written in a
sequence).
Int8, int16, int32, and int64: This type of data is stored as integers within 8 bits, 10 bits, 32 bits, and 64
bits. The size of the integer is given by its bit counts. Both logical and char are 1 byte (8 bits) wide.
uint8, uint16, uint32, and uint64: This type of data stores unsigned integer data in 8, 16, 32, and 64
bits.
double, single: This type of data is stored as double and single precision floating types, respectively.
Decimal numbers are represented by floating point data types. Single precision occupies 4 bytes (32
bits) and double precision occupies 8 bytes (64 bits) to store the floating point numbers.
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

MATLAB Additional Links

There are many MATLAB sites on the web.

mathworks.com is the first site you should visit. This is the home of MATLAB.

https://www.mathworks.com/help/pdf_doc/matlab/matlab_prog.pdf

https://www.it.northwestern.edu/software/matlab/links.html
http://engineering.nyu.edu/mechatronics/vkapila/matlabtutor.html
IE141 Computer Programming I – Distance Education Week 3 (12.10.2021)

This is the end of this week’s notes.

You might also like