Introduction To Octave

From CDS 130

Jump to: navigation, search


  1. Introduction
  2. Installing Octave
    1. Step 1 - Install
    2. Step 2 - Test
  3. Increase the font size
  4. Create a program
    1. Hints
    2. Windows
    3. Mac
    4. Question
    5. The semicolon
    6. Question
  5. Create a plot
  6. Save a plot
  7. Problems
    1. PC
    2. Mac
      1. Fuzzy images
      2. Library not loaded: /usr/Xdyld
  8. Questions
    1. Problems?
    2. Sum
    3. Plot
    4. File Size
    5. Command line

1. Introduction

  • Octave and MATLAB are programs used by computational scientists
  • They can do everything a spreadsheet can do and much more
  • Octave is an "Open Source" program that is mostly compatible with MATLAB
  • The primary interface for both is the command line. In contrast, primary interface of the spreadsheet is the GUI (Graphical User Interface).
  • The examples in this book will work with both MATLAB and Octave
  • Octave is free (that is, you do not need to pay for a license to use it). Octave is not installed on computers on campus.
  • A student version of MATLAB may be purchased for $109, typically at a campus bookstore. The non-educational entry-level version of MATLAB starts at $2,000.

2. Installing Octave

2.1. Step 1 - Install

  • Go to
  • Click on the Windows installer link
    • Use the default options for installation! Do not select octave-forge for install (image command won't work)!
  • for Mac OS X link
    • Drag to the Applications folder: Screeenshot of Octave Install.
    • Drag (in a folder called Extras in to the Applications folder: Screeenshot of Gnuplot install.
    • If you have a regular Macbook (not Pro), you will need to install the program called X11. It is on your operating system install DVD.

2.2. Step 2 - Test





The line that you enter commands on (the command line) starts with




or, for MATLAB


Type in a command:


you should see

ans = 28



3. Increase the font size

Students often type a period instead of a comma or a colon instead of a semicolon and have difficulty figuring out what went wrong because the font size is too small.

  • PC:
    • Right-click on the Octave shortcut that was placed on your desktop. Select Properties->Font and increase the font size.
  • Mac:
    • Click on the Octave window. Click on the command and + buttons or select View->Bigger on the menu bar.
    • Click on the TextWrangler window and then command-T to increase the font size.

4. Create a program

There are two ways to tell Octave to execute commands. You can type the commands on the command line or you can enter the commands into a file and tell Octave to execute the commands in the file. The commands in the file are also referred to as a program.


  • The procedure depends on if you are using a Windows or Mac operating system.
  • The file that you save must have the extension ".m".
  • When you type the filename on the command line to execute the program, don't include the .m.
  • Don't use a simple name for the file:
    • Bad: demo.m (already used), 2.m (first character may not be a number), matrix.m (already used).
    • Good: bob1.m, mycalc.m, etc.
    • When in doubt: type which bob1. If the response is 'bob1' not found. or which: 'bob1' is undefined, then it is OK to create a file named bob1.m.

4.1. Hints

  • To repeat a command without typing it in again, press the up or down arrow key until you see the command. Then press enter to execute the command again.
  • On Windows, you should exit by typing in the word exit on the command line. (Or, if you click the x on the window to exit, you will get an error message that you can ignore.)

4.2. Windows

On the command line, type

edit myfirstprogram.m

Notepad++ should open (don't worry about what the cd command is doing for now) . Delete the contents of the file and type


then save. Back in the command line, tell Octave to execute the contents of the file myfirstprogram.m by typing myfirstprogram on the command line (without the .m):


(note that there is no .m when you type the name on the Octave command line) you should see

ans = 28

Ask Octave where the file myfirstprogram.m is stored:

which myfirstprogram

The directory that is listed is the location where you can find myfirstprogram.m using a directory browser.

4.3. Mac

You need a text editor. Install TextWrangler (free). You may not use the text editing program that comes installed with OS X (Textedit). Open TextWrangler. Enter


save the file as myfirstprogram.m in your home directory using the default settings for "encoding". Start Octave and type


(don't worry about what the cd command is doing for now) you should see

ans = 28

Ask Octave where the file myfirstprogram.m is stored:

which myfirstprogram

The directory that is listed is the location where you can find myfirstprogram.m using a directory browser.

4.4. Question

How large (in bytes) do you expect the file where you entered "sum([1:7])" to be? Find out its size by using the file browser for your operating system. Compare this with the size of the file created using WordPad (on Windows) or TextEdit (on Mac).

4.5. The semicolon

When you do not end a line with a semicolon, Octave will respond with information about the variable. To see this, type

A(1) = 1

Octave will respond

A = 1

If you type

A(1) = 1;

Octave will just respond with a new command line.


4.6. Question



on the command line. How does this compare with typing


5. Create a plot

Create a plot by typing commands in on the command line


(This will not work on the Mac version if you did not drag to the Applications folder.)

6. Save a plot

Start Octave and type

print -dpng MyFirstImage.png

then type


which will result in a line that shows the directory that the image was saved to. Use your operating system's browser to locate the image and open it.

7. Problems

7.1. PC

  • Problem: The image command does not work.
  • Solution: Delete the folder C:\Octave and reinstall using the default options (do not select octave-forge).

7.2. Mac

7.2.1. Fuzzy images

  • Problem: The image command creates blurry images
  • Solution: Click the figure window, select Aquaterm->Preferences and set Image Interpolation Method to none

7.2.2. Library not loaded: /usr/Xdyld

Problem: When plotting, the following error appears:

dyld: Library not loaded: /usr/Xdyld: Library not loaded: /usr/X11/lib/libfreetype.6.dylib
 Referenced from: /usr/X11R6/lib/libfontconfig.1.dylib
 Reason: Incompatible library version: libfontconfig.1.dylib requires version 13.0.0 or later, but libfreetype.6.dylib provides version 10.0.0

Solution: Use this fix (from [1]):

  1. open the folder /Applications in the finder
  2. right-click on and select "show package contents"
  3. navigate to /Applications/
  4. right-click on the file "gnuplot" and select "Open with -> Other ... ->"
  5. change the line:



8. Questions

8.1. Problems?

If you have a problem that is difficult to explain in words, you can send a screencast to your instructor:

  1. Go to
  2. Enter your name and email and click Generate.
  3. Click on the link and record your actions that show what you did when you ran into a problem.
  4. Go to your email and forward the email from ShowMeWhatsWrong to your instructor.

8.2. Sum

On the command line, enter


What happens when you enter


Explain what happens and why.

8.3. Plot

Start Octave and enter




Describe the difference between the two plots that are created.

Now type

figure(1);peaks(32);  figure(2);peaks(64);

Describe what happens.

8.4. File Size

How large do you expect the file created using a text editor (Notepad++ on PC and TextWrangler on a Mac) where you entered sum([1:7]) to be? Find out its size by using the file browser for your operating system. Compare this with the size of the file with the same contents, sum([1:7]), created using WordPad (on Windows) or Textedit (on Mac). Write down the size of the files and suggest a reason why the file sizes are different.

8.5. Command line

Is text entry area at a command line?

Personal tools