Sunteți pe pagina 1din 13

GNU Octave

Higher-Level
Languages
for Numerical
Computations
GNU Octave vs. MatLab
Rebecca Winzer
Page 2

Introduction
This white paper project compares the effectiveness of two programming languages for
numerical computationsGNU Octave and MatLab. To perform this test I wrote the
appropriate source code for each method and each language in Notepad++ (a source
code editor) and ran the appropriate source code in each language. Some of the
methods I was able to test without using source code (such as availability of the
language).

The typical user for these products would be one with some basic knowledge of
programming and coding and who would have a need for a high-level (high-level as in
containing built in library functions to perform numerical tasks) numerical computations
languagesuch as an individual analyzing data or performing tests for their research. A
common example of the usability of these languages would be a biologist analyzing
their data using the predator-prey model for a particular group. This usability test is for a
technical writing class I am taking, but it is beneficial to those who want to compare
language choices before choosing one over the other.

Methods
I performed the tasks on my laptop since I have both languages stored on it. In order to
properly compare and evaluate the languages. I used a Likert scale to fairly analyze
each method. I conducted the test in the privacy of my home on a PC laptop. Since I
have some numerical programming experience from a class I was able to manipulate
and re-format some of my old code to test each program for the specific methods. I
used pieces of some of the problems I had tackled previously as examples in some of
the methods.

1. Availability of the language
2. Built in library functions
3. Load and save data
4. Comments and hiding answers
5. Write a function and use a function
6. Build a vector
7. Control statements
8. Matrices
9. Plot a graph
10. Solve an ODE (Ordinary differential equation)

Results
The usability for each method is explained. Then Octave and MatLab are compared by
the way each language approaches the particular method. Then the different
Page 3

approached are compared and evaluated as to which language is more efficient and
easy to use.

Method 1: Availability of the language
How available a language is in terms of cost and which systems it can run on is
important in deciding which particular language to learn and use for ones work. High
costs can deter some independent researchers as well as students from using it, but it
would be more available for companies that could afford it. Special system requirements
would also not allow the typical user to make use of the language.

GNU Octave:
Octave is a free language available for many different operating systems including
Windows, Linux, and BSD. Since it is available for so many different kinds of systems
and it does not cost the user anything, it is very much available for just about anyone
who wants to use it.

MatLab:
MatLab is not a free language but it is available to use with purchase on as many
operating systems as Octave. The language is available through universities and
companies, such as my version through the University of Idaho. MatLab is a very
popular language currently used in many engineering courses and in many research
labs. Since it is not available for free it does limit the pool of people who can or will use
the product.

Evaluation for Method 1:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x


Page 4

Since Octave is both free and easy to obtain it scored 10. MatLab is not free so it is not
easy to receive it but it is available for all operating systems and since I already had the
language it did not take a lot of time, therefore MatLab scored 8.

Method 2: Built in library functions
Built in library functions are why numerical computations languages are so powerful and
are much easier to use then a lower-level language like C for the typical less informed
programmer. One does not need a strong background in computer science to use these
programs for their necessary work; in fact built in library functions make it much easier
for the user to learn the language without outside assistance.

GNU Octave:
Octave has a host of built in library functions available. There are so many different
library functions it is not plausible to list even a good fraction of them. The library
functions include basics like addition and subtraction to many more complicated
processes like determining eigenvalues of a matrix, A (eig(A)), and the determinant of
the matrix (det(A)). These built in functions are character of a higher-level language and
allows the user to quickly use one of these functions for the problem they are trying to
solve without having to write the code behind these functions from scratch. While it does
limit the user from more flexibility with how their particular problem is handled, it can
make for efficient and quick numerical computations.

MatLab:
MatLab contains just as many built in library functions available as Octave. In fact the
formatting of these functions correlate heavily with that of Octave, such as the
determinant and eigenvalues of a matrix A being the same (det(A),eig(A)). It seems as if
you can run some of the same code used for Octave in MatLab and obtain the same
results. There is likely a high cross-over between library functions due to a lean towards
a more universal formatting for numerical computations. Here is an example of code I
ran in both programs that solve for the unknown vector x that solve A*x=b (A is a matrix
and x and b are both vectors):

A=[0.16,0.10;0.17,0.11;2.02,1.29]; %original matrix
b1=[0.26;0.28;3.31]; %right hand side
x1=A\b1
b2=[0.27;0.25;3.33]; %slightly perturbed right hand side
x2=A\b2

In both of these programs I got the original vector x to be equal to [1;1] and the
perturbed vector to be equal to [7.0089;-8.3957].
Page 5


Evaluation for Method 2:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

I had to take time to look up both Octave and MatLabs built in library functions and
even though I was not able to look at them all I got a feel for both languages and what
they were able to accomplish. They both scored an 8.

Method 3: Load and save data
Loading and saving data is an essential part of a language because naturally one does
not complete all of their necessary work in one sitting. Knowing how a language loads
and saves data and the efficiency of such procedures is important information a user
should have before choosing to learn that language.

GNU Octave:
When exiting Octave you lose all of the variables created. If you are in the middle of
doing something and need to leave you can save your current data. To save your data
you just type save and then a file name, for example save anyname, and it will save it
as a mat file in your current directory. To then load the filename you just type load
anyname and then you can continue your work where you left off. It is important to be
aware of what directory you are in.

MatLab:
MatLab saves and loads your workspace very similar to the way that octave does. To
save your workspace to a filename named anyname you type save (anyname) and then
to load the workspace type load (anyname). The only difference between Octave and
MatLab is that MatLab uses parentheses around the filename for its format.



Page 6

Evaluation for Method 3:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

To test these methods I had to save and load a workspace using the appropriate
commands in each language. One language was as easy as the other and therefore
both scored an 8.

Method 4: Comments and hiding answers
Comments are important in understanding how one has written their code. For example
one can define a variable but forget what that variable stood for in their code. A
comment next to that particular line of code that reminds the writer what they are doing
along the way is very useful. Hiding answers is also very useful. For example you are
solving for something that relies on a host of other computations. It would be rather
annoying if each time you ran the code it printed out all of those extra computations. To
hide these answers one simply has to put a semicolon after the line of code and then
when the program is ran it will not print out those extra lines.

GNU Octave:
To comment in Octave you put a percent sign (%) before the comment in your code.
This commenting is important to keep track of what you are doing and what your
variables stand for. Comments do not run as code so they are easy to implement into
your source code without affecting anything. To hide an answer in Octave you put a
semicolon after your line of code. This semicolon will hide the answer to that line of
code but still run it in the program.

MatLab:
MatLab uses the exact same format for commenting and hiding answers as Octave
does. MatLab uses a semicolon to hide answers and a percent sign to comment. Both
languages are very easy to use in this manner.

Page 7

Evaluation for Method 4:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

This task was very easy and quick to complete in both languages and therefore both
languages scored a 10.

Method 5: Write a function and use a function
Functions are an essential component to numerical computations and therefore it is
necessary to evaluate how a language incorporates and uses functions before
attempting to learn the language.

GNU Octave:
To write a function Octave uses a very easy format. An example function would be
f(x)=sin(10*x)-x:

function y=f(x)
y=sin(10*x)-x;
end
To then use this function Octave can recall it immediately and use it once it is defined in
the code. Octave is very easy to use in this manner.

MatLab:
MatLab uses a similar format. It writes the function in the same exact way but there is a
difference in how the language uses the function. One has to call the function in from a
different file to use it in your written code. Essentially each function you write has to
have a different file name and to use the function one has to call it in to the program that
one is running by using an @filename. For example if you were to solve the same
function uses above for what x values make it zero the written code would be:


Page 8

function y=f(x)
y=sin(10*x)-x;
end
fsolve(@functfile);

Note that functfile.m is where I stored my written function that is above the fsolve library
function. MatLab is a little more difficult to use in this manner since you cant store all of
the code in the same file; you have to store each function you want to use in a different
file.

Evaluation for Method 5:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x


MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

I had to look up the format in both languages and write and run a function. MatLab was
harder to use and took more time because of the fact that the function had to be written
in a different file and then called in. Octave scored an 8 and MatLab scored a 6.

Method 6: Build a vector
Vectors are an important part of numerical computations since many different types of
mathematical problems make use of vectors. In fact one can hardly solve a problem in
physics without the use of vectors since direction and magnitude is a very important
aspect in the particular types of problems solved. Therefore it is a necessary component
of the language that the typical user would use.

GNU Octave:
To build a vector in Octave is very easy. The format is for a vector h in the x,y,z
direction:

h=[1;1;1]
Page 9


To create vertical components separate with a semi-colon and to create horizontal
components separate with a comma such as:

h=[1,1,1]

MatLab:
MatLab uses the exact same format as Octave does to build vectors and therefore is
just as efficient and easy to use as Octave is.

Evaluation for Method 6:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

This task was very simple and quick to complete in both languages therefore they both
scored a 10.

Method 7: Control Statements
Control statements are essentially loops and are very important part of doing numerical
computations. You can put different conditions and do a vast array of complicated
numerical procedures using loops and therefore it is a very important method to have as
part of the language one uses.

GNU Octave:
Using loops in Octave is quite simple. There are many different loops (for loops, ifelse
if loops, and while loops). Each loop uses the same kind of format. An example of a for
loop would be:

for k=[1:10]
epsilon=10^(-2*k);
end
Page 10


This loop would run through 10 times computing epsilon each time for k=1-10. For each
loop one has to start the loop and then end the loop using end.

MatLab:
MatLab again uses the exact same format as Octave does and therefore is just as
efficient and easy to use.

Evaluation for Method 7:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

The loops took some time because I had to assign a problem to test them, but one
language was just as easy and quick to complete the task in as the other. Therefore
they both scored an 8.

Method 8: Matrices
Matrices are very important in numerical computations and can be used for many
different kinds of problems. They are an essential component to any decent numerical
language.

GNU Octave:
Forming a matrix in Octave is similar to forming a vector. To assign horizontal
components to the matrix use a comma to separate and to assign vertical components
to the matrix use a semicolon to separate. For example a 2x2 matrix A of zeros would
be written as:

A=[0,0;0,0]

This format is quite simple and easy to use.

Page 11

MatLab:
Once again MatLab assigns matrices the exact same way that Octave does and
therefore is just as easy to use.

Evaluation for Method 8:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

This task was very quick and easy to complete in both languages, therefore they both
scored a 10 on the Likert scale.

Method 9: Plot a graph
Being able to plot ones data is very necessary for many users to these type of
programs. A numerical computations language that does not incorporate a library
function to plot is essentially useless.

GNU Octave:
Plotting in Octave again is quite easy. One uses the library function plot to accomplish
the task. For example let us say a user had data with both independent and dependent
data stream (a very typical stream of data). To plot the dependent variable (x) and the
independent variable (y) all one would have to do is enter the data in terms of these
variables and then type plot(x,y). This would plot the data as x vs. y. One can even
change the color by typing plot(x,y,-r), which would change it to red but the default plot
color is black. Again Octave is quite easy to use in this manner.

MatLab:
MatLab again uses the exact same library function format as Octave for plotting and
therefore is just as easy to use.



Page 12

Evaluation for Method 9:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

Plotting a graph was quite quick and easy to do in both languages. All I had to do was
make up a set of data for both x and y (I just used 4 random data points for both x and
y) and then plot the data. Both languages scored a 10 on the Likert scale.

Method 10: Solve an ODE (Ordinary Differential Equation)
Solving an Ordinary Differential Equation might not be for the typical user but it is a very
powerful asset to pull from ones numerical computations language. There are ways to
code for solving ODEs by hand but fortunately both Octave and MatLab have their own
built in library function for accomplishing this task.

GNU Octave:
To solve an ODE in Octave one has to define a function f in the code and then use the
library function lsode. One uses lsode as such:

lsode(f) , where f is an already defined function in the code.

Learning how to use this solver can be a bit tricky with the setup because one has to
learn to assign the appropriate boundary conditions in the function and then learn how
to use the ODE solver.

MatLab:
To solve an ODE in MatLab one has to define the function in a different file and then call
the function in to use (as discussed previously under defining and using functions).
MatLab uses the library function ode45 and it is very similar to the way that lsode works
for Octave. The only difference is you have to call the function in from a different file to
use in the ODE solver. If I named my function file functfile.m I would use the ode45
solver as such:
Page 13


ode45(@functfile)

MatLab is much trickier to use in this manner than Octave because one has to not only
know how to use the solver properly, one also has to call the function in from a different
location so there is a lot more to handle in that manner.

Evaluation for Method 10:
GNU Octave 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

MatLab 1 (Strongly Disagree) 2 3 4 5 (Strongly Agree)
The task was easy to
complete.
x
The task did not take an
ample amount of time to
complete.
x

This task was not quick and easy to complete for either language but Octave was much
easier to use because it better handles using a function then MatLab does. Therefore
Octave scored a 6 and MatLab scored a 4.

Recommendations
Using the Likert scale method Octave clearly is the better option to choose for numerical
computations. Not only is it more available since it is a completely free software, it also
does everything that MatLab does (and some in a more efficient way). Octave is not as
popular in name as MatLab, but once you learn Octave it will be quite easy to learn
MatLab if it is ever necessary. After analyzing the results of my test, I would suggest
Octave over MatLab as the language to use.

S-ar putea să vă placă și