<texit info> author=Roman Putanowicz backgroundtext=http://www.L5.pk.edu.pl/~putanowr/iten.html title=Lab 2: Getting started with Octave showbuttons=off </texit> <texit>\input{/home/prac/putanowr/dokuwiki/data/media/wiki/latex/it_preamb.tex} \renewcommand{\chaptername}{Lab Handouts} \setcounter{chapter}{1} </texit> <texit>\begin{comment}</texit> prev | up | next <texit>\end{comment}</texit>

Lab 2 : Getting started with Octave

Description

Running Octave in interactive mode. The difference between Octave prompt and shell prompt. Command line editing in Octave; command history. Octave as scientific calculator; evaluating arithmetic expressions in interactive mode. Variables and assignment operator. Writing Octave scripts. Running Octave scripts from shell command line and from Octave interactive session. Octave simple I/O: disp and input commands. Defining vector variables, reading vectors via input command. Defining arithmetic sequences the : (colon) operator.

Skills to be acquired:

  • running Octave in interactive and batch mode,
  • using Octave as scientific calculator,
  • defining scalar and vector variables,
  • using commands for simple input and output.

Self study

The goal of this lab is to introduce the basic concepts of programming in Octave, especially the working of the assignment operator. While some basics of defining scalar and vector variable are shown, there is a lot of topics about vectors1) and matrices that are not covered. Octave is a matrix-based language, which means that matrix operations are built-in into language core, it provides special syntax for handling matrix variables and functions that can operate both on scalar and matrix arguments. Mastering operations on matrices can speed-up programs development, make them shorter and more easy to understand and in most cases make them execute faster. Below are some ideas for self study related mostly to operations on vectors:

  • Read from Octave manual the description of function printf.
  • Learn how to access a single element of a vector.
  • Exercise the working of operators *, +, - on vectors.
  • Learn the difference between operators ^, *, /, +, - and their version prefixed with “.” (dot), i.e. “.*”.

Readings

Solved problems

Ex. 2.1.1

Using Octave in interactive mode calculate the value of the expression:
<latex> $ \Large\begin{equation*}\text{.}\quad\quad \sin(a)^{\sin(b)+1} \end{equation*}\normalsize $ </latex>
for the following pairs of values (a,b):

  (0.7,   1.0)
  (0.1,   3.0)
  (0.3,  -0.5)

Solution.

Ex. 2.1.2

Write a script that asks user to enter coordinates of two vectors and then calculates and displays the Euclidean norm, scalar product and vector product of these vectors.
Solution.

Ex. 2.1.3

Write a script that given initial term and common difference of successive members of an arithmetic sequence displays N initial terms of that sequence.
Solution.

Questions and open problems

Ex. 2.2.1

Write a script that allows the user to translate between radians and degrees.

Ex. 2.2.2.

Write a script that calculates the surface area of a cone knowing its radius and height.

Ex. 2.2.3

Write a script that reads in coordinates of three points in 3D space and calculates the area of a triangle with these points as vertices.
Hint: Check out the geometric meaning of vector cross product magnitude.

Ex. 2.2.4

Write a script that calculates the distance on the Earth's surface from a given point to the North Pole. Assume that the point's geographic coordinates are given with minute resolution.

Ex. 2.2.5

Write a script that for any given argument x calculates the value of a function shown in the figure <texit>2.1</texit> below.
Function f(x) -- what is the formula for it?

Ex. 2.2.6

Write a script that reads coordinates of three points A, B, C in 2D and calculates the distance from the point C to the line defined by the segment AB.

Ex. 2.2.7

An electric field is created by three charged elementary particles with charges respectively

 q_1 =  2 [C]
 q_2 = -4 [C]
 q_3 = 12 [C]

located as shown in figure <texit>2.2</texit>

Location of charged particles.

Write a script that calculates the E-field at point (x,y).

Ex. 2.2.8

The “pyramid” shown in the figure <texit>2.3</texit> is built from square plates with the material area density $\rho_s$ [kg/m^2]. Write a script that calculates total weight of a pyramid H blocks high and with block dimension equal a.

A 2D pyramid.

Ex. 2.2.9

Bob the Builder is going to build a plank fence around the yard shown in the figure <texit>2.4</texit> below. He wonders how many planks are needed.
Bob's yard.

Write a script that given the yard dimensions R, L and H as well as the plank's width w will calculate the number of planks that needs to be ordered.

<texit>\begin{comment}</texit> prev | up | next <texit>\end{comment}</texit>

1)
Vectors are treated as a special kind of matrices that have one row or one column only.
  • en/teaching/subjects/it/labs/lab2.txt
  • Last modified: 2017/10/02 15:54
  • (external edit)