16
1 Using Octave to Introduce Programming to Technical Science Students Nuno C. Marques [email protected] Francisco Azevedo [email protected] CENTRIA, DI- FCT/UNL Monte de Caparica, 29 June 2005 Carmen Morgado [email protected] Jorge Custódio [email protected] CITI, DI- FCT/UNL

1 Using Octave to Introduce Programming to Technical Science Students Nuno C. Marques [email protected] Francisco Azevedo [email protected] CENTRIA, DI-

Embed Size (px)

Citation preview

11

Using Octave to Introduce Programming to Technical Science Students

Nuno C. [email protected]

Francisco Azevedo

[email protected]

CENTRIA, DI- FCT/UNLMonte de Caparica, 29 June 2005

Carmen [email protected]

Jorge Custó[email protected]

CITI, DI- FCT/UNL

22

Course Background

• A computer science introductory course offered to non computer science Engineering students.

• Academic goals– basic digital computer architecture

• Computer basic instructions can be executed in an algorithmic way. – basic algorithmic notions for building a program

• Simple assignment instructions to program control flow instructions and functions.

– All instructions are presented with practical examples.• Simple calculation illustrates the assignment of values to variables.• The calculation of the solutions of a second degree polynomial are

used to introduce basic control instructions.• Iteration on tables presents the need for using cycles. • More complex problems as calculation of approximate solutions by

iterative algorithms• Recursive function calls.

33

Octave as a valuable tool

students have fairly heterogeneous set of interests. Non computer science students tend to think that programming a

computer is a very specific, difficult and unnecessary task. Learning to construct an algorithm is fundamental for structured thinking. Learning to program a computer is an essential tool for any Engineer.

Why Octave? traditional computer languages have become too distant for the current

GUI PC user. Octave provides a language with a higher abstraction level directly

related to linear algebra. A powerful algebra calculator Enables more challenging problems, while still keeping the clear illustration of

the basic concepts in programming.

Octave reduces the effort needed to solve real problems. Related language MatLab has an huge number of toolboxes available. To develop small algorithmic solutions that would adapt different

toolboxes for specific needs.

44

Some examples for Octave

octave:60> A=[1,2,3;4,5,6;7,8,9]A = 1 2 3 4 5 6 7 8 9octave:61> L=log10(A)L = 0.00000 0.30103 0.47712 0.60206 0.69897 0.77815 0.84510 0.90309 0.95424octave:62> (10*ones(size(A))).^L-Aans =

0.0000e+00 0.0000e+00 0.0000e+000.0000e+00 8.8818e-16 0.0000e+00 0.0000e+00 -8.8818e-16 0.0000e+00

octave:60> A=[1,2,3;4,5,6;7,8,9]A = 1 2 3 4 5 6 7 8 9octave:61> L=log10(A)L = 0.00000 0.30103 0.47712 0.60206 0.69897 0.77815 0.84510 0.90309 0.95424octave:62> (10*ones(size(A))).^L-Aans =

0.0000e+00 0.0000e+00 0.0000e+000.0000e+00 8.8818e-16 0.0000e+00 0.0000e+00 -8.8818e-16 0.0000e+00

octave:71> A=[1, 2, 3; 2, 3, 4; 4, 2, 5]A = 1 2 3 2 3 4 4 2 5octave:72> B=[4;5;1]B = 4 5 1

octave:89> X =A \ B octave:89> X =A \ B X =X = -1.40000 -1.40000 1.80000 1.80000 0.60000 0.60000

octave:71> A=[1, 2, 3; 2, 3, 4; 4, 2, 5]A = 1 2 3 2 3 4 4 2 5octave:72> B=[4;5;1]B = 4 5 1

octave:89> X =A \ B octave:89> X =A \ B X =X = -1.40000 -1.40000 1.80000 1.80000 0.60000 0.60000

Linear Algebra Basic OperationLinear Algebra Basic Operation Solving linear equationsSolving linear equations

55

The Iterative Fibonacci Example

fib = ones(1,10);for i = 3:10 fib(i)=fib(i-1)+fib(i-2);endfor octave:3> fibfib = 1 1 2 3 5 8 13 21 34 55

fib = ones(1,10);for i = 3:10 fib(i)=fib(i-1)+fib(i-2);endfor octave:3> fibfib = 1 1 2 3 5 8 13 21 34 55

Introduces: Initialization of vectors. Sequences FOR loops, with interaction among vector

components. The traditional notion of vector array is related

with linear algebra notion of vector.

88

E.x. 2D Graphic Transform

• A real example done by students.

• Basic transforms were given:

• Students implemented solution.– One group created a

snowman…

d11=[12;9;1];d12=[7;9;1];

d13=[8;10;1];d14=[7;10;1];d15=[9;10;1];d16=[8;11;1];d17=[6;13;1];d18=[4;11;1];d19=[4;10;1];d20=[5;9;1];

d1=[5;0;1];d2=[7;0;1];d3=[9;2;1];d4=[9;3;1];d5=[7;5;1];d6=[8;6;1];d7=[8;8;1];

d8=[11;8;1];d9=[12;7;1];

d10=[12;8;1];

d21=[4;8;1];d22=[1;8;1];d23=[0;9;1];d24=[0;8;1];d25=[0;7;1];d26=[4;6;1];d27=[5;5;1];d28=[3;3;1];d29=[3;2;1];

snowman=[d1,d2,d3,d4,d5,d6,d7,d8,d9,d8,d10,d8,d11,d8,d7,d12,d13,d14,d15,d13,d16,d17,d18,d16,d18,d19,d20,d21, d22,d23,d22,d24,d22,d25,d22,d21,d26,d27,d28,d29,d1];

99

E.x. 2D Graphic Transform

GNU Octave, version 2.1.35 (i386-pc-linux-gnu).GNU Octave, version 2.1.35 (i386-pc-linux-gnu).Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.This is free software with ABSOLUTELY NO WARRANTY.This is free software with ABSOLUTELY NO WARRANTY.For details, type `warranty'.For details, type `warranty'.

*** This is a development version of Octave. Development releases*** This is a development version of Octave. Development releases*** are provided for people who want to help test, debug, and improve*** are provided for people who want to help test, debug, and improve*** Octave.*** Octave.********* If you want a stable, well-tested version of Octave, you should be*** If you want a stable, well-tested version of Octave, you should be*** using one of the stable releases (when this development release*** using one of the stable releases (when this development release*** was made, the latest stable version was 2.0.16).*** was made, the latest stable version was 2.0.16).

octave:1> _octave:1> _

GNU Octave, version 2.1.35 (i386-pc-linux-gnu).GNU Octave, version 2.1.35 (i386-pc-linux-gnu).Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.This is free software with ABSOLUTELY NO WARRANTY.This is free software with ABSOLUTELY NO WARRANTY.For details, type `warranty'.For details, type `warranty'.

*** This is a development version of Octave. Development releases*** This is a development version of Octave. Development releases*** are provided for people who want to help test, debug, and improve*** are provided for people who want to help test, debug, and improve*** Octave.*** Octave.********* If you want a stable, well-tested version of Octave, you should be*** If you want a stable, well-tested version of Octave, you should be*** using one of the stable releases (when this development release*** using one of the stable releases (when this development release*** was made, the latest stable version was 2.0.16).*** was made, the latest stable version was 2.0.16).

octave:1> _octave:1> _ objectos_

1010

E.x. 2D Graphic Transform

GNU Octave, version 2.1.35 (i386-pc-linux-gnu).GNU Octave, version 2.1.35 (i386-pc-linux-gnu).Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.This is free software with ABSOLUTELY NO WARRANTY.This is free software with ABSOLUTELY NO WARRANTY.For details, type `warranty'.For details, type `warranty'.

*** This is a development version of Octave. Development releases*** This is a development version of Octave. Development releases*** are provided for people who want to help test, debug, and improve*** are provided for people who want to help test, debug, and improve*** Octave.*** Octave.********* If you want a stable, well-tested version of Octave, you should be*** If you want a stable, well-tested version of Octave, you should be*** using one of the stable releases (when this development release*** using one of the stable releases (when this development release*** was made, the latest stable version was 2.0.16).*** was made, the latest stable version was 2.0.16).

octave:1> _objectosoctave:1> _objectos===> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <======> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <===Qual o objecto que vai escolher?Qual o objecto que vai escolher?Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)?

GNU Octave, version 2.1.35 (i386-pc-linux-gnu).GNU Octave, version 2.1.35 (i386-pc-linux-gnu).Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001 John W. Eaton.This is free software with ABSOLUTELY NO WARRANTY.This is free software with ABSOLUTELY NO WARRANTY.For details, type `warranty'.For details, type `warranty'.

*** This is a development version of Octave. Development releases*** This is a development version of Octave. Development releases*** are provided for people who want to help test, debug, and improve*** are provided for people who want to help test, debug, and improve*** Octave.*** Octave.********* If you want a stable, well-tested version of Octave, you should be*** If you want a stable, well-tested version of Octave, you should be*** using one of the stable releases (when this development release*** using one of the stable releases (when this development release*** was made, the latest stable version was 2.0.16).*** was made, the latest stable version was 2.0.16).

octave:1> _objectosoctave:1> _objectos===> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <======> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <===Qual o objecto que vai escolher?Qual o objecto que vai escolher?Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? snowman_

1111

E.x. 2D Graphic Transform

octave:1> _objectosoctave:1> _objectos===> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <======> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <===Qual o objecto que vai escolher?Qual o objecto que vai escolher?Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? snowmanQuadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? snowmanDeseja fazer uma operação de translacção? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de translacção? sim (prima 1)/não (prima 2)? 1Tx? Tx? -10-10Ty? Ty? -10-10Deseja fazer uma operação de rotação? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de rotação? sim (prima 1)/não (prima 2)? 1Qual e o ângulo (em graus)? Qual e o ângulo (em graus)? 4545Deseja fazer uma operação de escala? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de escala? sim (prima 1)/não (prima 2)? 1Sx? (Sx>0) Sx? (Sx>0) 1.5Sy? (Sy>0) Sy? (Sy>0) 1.5

octave:1> _objectosoctave:1> _objectos===> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <======> SEJA BEM VINDO AO PROGRAMA OBJECTOS!!! <===Qual o objecto que vai escolher?Qual o objecto que vai escolher?Quadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? snowmanQuadrado (obj1), Rectângulo (obj2), Triângulo(obj3) ou Laço (obj4)? snowmanDeseja fazer uma operação de translacção? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de translacção? sim (prima 1)/não (prima 2)? 1Tx? Tx? -10-10Ty? Ty? -10-10Deseja fazer uma operação de rotação? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de rotação? sim (prima 1)/não (prima 2)? 1Qual e o ângulo (em graus)? Qual e o ângulo (em graus)? 4545Deseja fazer uma operação de escala? sim (prima 1)/não (prima 2)? 1Deseja fazer uma operação de escala? sim (prima 1)/não (prima 2)? 1Sx? (Sx>0) Sx? (Sx>0) 1.5Sy? (Sy>0) Sy? (Sy>0) 1.5

===> Veja o resultado! :) <=== ===> Veja o resultado! :) <===

1212

Student Evaluation: Questions in Final Exams (1/2)

General algorithm questions: In GNU-Octave a FOR instruction:

is necessary to repeat the execution of several instructions; is used to take decisions in an algorithm; is necessary to implement recursive problems, such as a recursive function to

calculate the factorial of an integer. is used to repeat the execution of several instructions for a precomputed range

(or set) of values. None of the above options is correct

Excel lookup questions: The first 50 lines in an Excel Worksheet have student grades in a given course.

Column A contains the student number. Column B contains student's name and column C student's grade. The students are sorted by number. If there is a student with number 1335 what is the formula to get this student's grade?

=hlookup(1335,A1:C50,1,TRUE); =hlookup(1335,A1:C50,3,FALSE); =vlookup(1335,C1:C50,3,FALSE);

Simple algebra in Octave: If a = [1, 2, 3] then:

a.*a' returns ans=14. a * a' returns ans=14. a * a returns ans=14.

=vlookup(1335,A1:C50,3,FALSE);

=vlookup(1335,C1:C50,1,TRUE);

a + a returns ans=0. None of the above options is

correct.

1313

Student Evaluation: Questions in Final Exams (2/2)

Programming:Programming: Given the following Octave function (defined in file Given the following Octave function (defined in file

gera.m):gera.m):

What are the values of variables m and n after What are the values of variables m and n after executing the commands:executing the commands:

m=gera(5,3); n=gera(2,3);m=gera(5,3); n=gera(2,3);

function v=gera(a,b) if (a>0 && a<=5) for i=1:a for j=1:b if (a>b) v(i,j)=0; else v(i,j)=1; endif endfor endfor else v=0; endifendfunction

1414

Assignments presented to students over the semesters

1515

Analysis of student evaluation

The evolution in learning how to program.

Better results when knowledge evaluation was divided into two assignments.

even greater improvement by replacing first assignment by Octave written exam (begins spring semester of 2002-03).

S01/02S01/02 W01/02W01/02 S02/03S02/03 W02/03W02/03

-correct/Acceptedcorrect/Accepted

-No ReplyNo Reply

-wRongwRong

1616

Analysis of student evaluation

Relation between general Octave matrix manipulation and Octave programming.

There is a strong relation between the success rate in replies to Octave matrix calculus and the success rate in the programming questions.

Excel LOOKUP questions are correlated to simple programming questions.

37% accuracy in programming pass to 51% accuracy for students that reply to Excel LOOKUP question.

1717

Conclusions

GNU Octave is an excellent way to present computer programming to students.

can replace and extend MS-Excel in numerical analysis studies. students easily start to use Octave command line for issuing basic

algebra and numeric manipulation commands. Octave programming environment:

It is just a simple prompt in the command line. Students tend to adapt relatively fast to the command line interface. More complex graphical interfaces:

Are more suitable to user dispersion. Try to hide the algorithmic and command oriented computer. But it would be useful to show the state of a running program (as in a

debugger). Graphically oriented problems are more attractive.

Most students finished course with the main concepts regarding algorithmic knowledge and with a tool to apply in real life situations.

1818

Thank you for your attention

For additional information please contact the authors at Departamento de Informática FCT/ UNL

Tel: (+351) 21 294 8536 Fax: 21 294 8541

EMAIL: [email protected] (Please add subject: ICP ) Web-Page: http://ssdi.di.fct.unl.pt/~nmm/ICP

For additional information please contact the authors at Departamento de Informática FCT/ UNL

Tel: (+351) 21 294 8536 Fax: 21 294 8541

EMAIL: [email protected] (Please add subject: ICP ) Web-Page: http://ssdi.di.fct.unl.pt/~nmm/ICP

An algorithm? [ Pavão Martins 94]An algorithm? [ Pavão Martins 94]