<texit info> author=Roman Putanowicz title=Lab 3: Defining and using functions backgroundtext=http://www.L5.pk.edu.pl/~putanowr/iten.html showbuttons=off </texit> <texit>\input{/home/prac/putanowr/dokuwiki/data/media/wiki/latex/it_preamb.tex} \setcounter{chapter}{2} </texit> <texit>\begin{comment}</texit> prev | up | next <texit>\end{comment}</texit>

Lab 3 : Defining and using functions

Description

Octave syntax for defining functions. Defining functions versus calling it - the most common mistakes. Functions and m-files - Octave mechanism for building libraries of functions. Name scopes, local variables versus global variables. Passing variables to functions by value. Introduction to plot command - ploting scalar functions in 2D. Setting selected plot paramters - axis range, plot color, basic annotations. Pausing Octave scripts with pause().

Skills to be acquired

  • Dfining and using own functions
  • Understanding the distinction between defining functions and calling them
  • Using Octave plot command to visualise scalar functions in 2D.

Self study

In this lab two important topics are touched: defining user functions and plotting in Octave. Functions are important because they are basic building blocks of Octave programs, they help to make the code easier to write, maintain and reuse. During the lab only the most important aspects of Octave functions are exercised. That is enough for writing simple programs, however sometimes more advanced constructs are necessary. Below some ideas for self-study related to Octave functions are given:

  • defining functions that accept variable number of arguments,
  • function handles and passing functions as variables,
  • documenting functions,
  • accessing global variables inside functions,
  • handling errors in functions,
  • using functions written in C/C++ (for the advanced users).

The second topic just touched in this lab is Octave support for making plots. One can use Octave built-in support for plotting or one of several extension packages, for instance Octaviz. Below are some ideas for self-study:

  • more control on plot color, line style, markers, etc.,
  • multiple plots in one window,
  • exporting plots to files.

Readings

* Lab 5 handouts – another course lab handouts (in Polish), a lot of simple examples

Solved problems

Ex. 3.1.1

Write a function that calculates the area of segment of a circle knowing its central angle and the circle radious. Circle segment. Solution

Ex. 3.1.2

Write a function that normalizes a vector.
Solution

Ex. 3.1.3

Write a script that plots funcion f(x) for x in the range [-5:5] \begin{equation*} f(x) = \frac{1}{1+(x\sin(x))^2} \end{equation*} Annotate the plot as shown in figure <texit>3.2</texit> below.

Screen capture of Octave plotting window

Solution

Questions and open problems

Ex. 3.2.1

Write a function that calculates arithmetic, harmonic and geometric average of two positive numbers.

Ex. 3.2.2

Write a function that calculates the volume of a regular tetrahedron with the edge length equal x.

Ex. 3.2.3

Write a function that caluclates the surface area of a regular N-gon inscribed in circle of radiurs R.

Ex. 3.2.4

Write a function that calculates the time from the start to the fall for a projectile in a skew throw, given the initial velocity v and the angle $\alpha$. Skew throw setup

Ex. 3.2.5

Write a function that calculates the area between 7-gon and its circumcircle. Reuse the function written for Ex. 3.2.3. 7-gon with circumcircle

Ex. 3.2.6

Write a function that plots the trajectory of a projectile in skew throw.

Ex. 3.2.7

Write a function that calculates the maximum deflection of a beam shown in figure below. The beam length L, crossection moment of inertia J ,material elasticity modulus E, and the load density q are the function's input parameters. Find a suitable reference for the formula of the beam deflection.  beam with constant loading

Ex. 3.2.8

Write a function that calculates the barycentirc coordinates (aka area coordinates) of a point P(x,y) with respect to the triangle ABC shown in figure below. Point in a triangle

Ex. 3.2.9

Given a parabola of the form

  y(x) = a^2 - x^2        a=const

write a function that depending on value “a” calculates the volume inside the surface made by rotating the part of the parablola for y>=0 around the X-axis.

Hints: Search for Papus' s centroid theorem and Simpson rule.

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

  • en/teaching/subjects/it/labs/lab3.txt
  • Last modified: 2017/10/02 15:54
  • (external edit)