0% found this document useful (0 votes)
37 views20 pages

Programacio Basica 2

This document discusses numerical simulation and programming in Octave. It covers: 1) Representation of floating point numbers and integers in Octave, including formats, errors, and operations with floating points. 2) Variables, matrices, vectors, and operations with matrices like addition, multiplication, transposition, and submatrix extraction using ranges. 3) Determinants and inverses of square matrices. 4) Programming concepts in Octave like relational operators, if/else conditions, loops, scripts, functions, and input/output commands.

Uploaded by

guille1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views20 pages

Programacio Basica 2

This document discusses numerical simulation and programming in Octave. It covers: 1) Representation of floating point numbers and integers in Octave, including formats, errors, and operations with floating points. 2) Variables, matrices, vectors, and operations with matrices like addition, multiplication, transposition, and submatrix extraction using ranges. 3) Determinants and inverses of square matrices. 4) Programming concepts in Octave like relational operators, if/else conditions, loops, scripts, functions, and input/output commands.

Uploaded by

guille1989
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

NUMERICAL SIMULATION:

1.- Representation of floating points:

1.1- DEFINITIONS:

What is a floating point? Since computer memory is limited, you cannot store
numbers with infinite precision (imagine e). The idea is to compose a number of two main
parts:

 a significant that contains the number's digit. Negative significands represent


negative numbers
 an exponent that says where the decimal (or binary) point is place relatively to
the beginning of the signifiand. Negative exponents represents numbers that
are very small (close to zero)

In essence, a floating point is a number that can have decimal parts (being 3.1
or 5.8955E89).

What is a integer? a whole number (like 1 or 6)

1.2-REPRESENTATIONS IN OCTAVE

There are 2 ways to display a number, formats short (by default) and long.

>> 1/7
This is the short format
ans = 0.14286

>> format long


This is the long one. It's
>> 1/7 neccessary to "ask" th software to
use it.
ans = 0.142857142857143

Inside the formats we can display the result in scientifical notation (xEy)

>> format long e

>> 1/7

ans = 1.42857142857143e-001

>> format short e

>> 1/7

ans = 1.4286e-001

1.3 ERROR:

The relative rounding error of using a floating point instead of the real number
corresponds to:

Apuntes descargados de wuolah.com


And in Octave is obtained doing (this results corresponds to the previous
example of 1/7):

>> eps

ans = 2.2204e-016

The maximum and minimum positive floating points value are obtained doing:

>> realmin

ans = 2.2251e-308

>> realmax

ans = 1.7977e+308

1.3 OPERATIONS WITH FLOATING POINTS:

IMPORTANT! zero DOES NOT exist, but any number below the minimum positive
floating point is treated like zero. If an operation gives a number bigger than xmax MATLAB
provides Inf. Indetermination as 0/0 or infinite/infinite gives NaN.

EXAMPLE

>> x=1E-15

x = 1.0000e-015

>> ((1+x)-1)/x

ans = 1.1102e+000

>> eps

ans = 2.2204e-016

>>

>> x=1E-14

x = 1.0000e-014
>> ((1+x)-1)/x

ans = 9.9920e-001

>> eps

ans = 2.2204e-016

2.- Variables

The type of the variable is automaticaly detected by Octave. A variable can be


numbers, integers (must be specified like a=int32(3)), doubles, strings of character
(words...), arrays of numbers (1,2,8)

>> a='example'

a = example

>> b=3

b= 3

>> c=int32(3)

c=3
This happens because 'cinco' is
not a number, but the code treats
each characters as one.

3.. Matrices and vectors.

3.1. MATRICES AND VECTORS:

A matrix A is a set of elements aij distributed in m rows and n


columns. A matrix in Octave is delimited by [ and ],the elements of a row
are separated by spaces and rows by ;

3.2. OPERATIONS WITH MATRICES:

-Addition: being A and B matrices with the same numbers of columns and
rows, the opperation A+B gives as a result another matrix with the same numbers of
columns and rows. In Octave, after having defined A and B (is capital sensitive)¸A+B
-Product of a matrix and a real number: the result is a new matrix. In
Octave, after having defined a, a*A

-Product os matrices: Being the numbers of rows of A equal to the numbers


of columns of B, the result given is a new matrix with the numbers of columns of A and the
numbers of rows of B. In Octave, A*B
When the orders of the matrices
are not correct, Octave is unable
to operate, so this error is
displayed.

-Transpose: The columns and rows change places. In Octave is done by


transpose(A):
3.3. RANGES AND SUBMATRIX EXTRACTION:

Range: a sequence of numbers that can be generated using the range


operator (the double colon : ). It's useful to create and manipulate matrices and vectors.

-Create a vector m containing integers from i to j: m=i:j

-Create a vector m containing integers from i to j by increment k: m=i:k:j

-Extract rows or columns from a given matrix: using again range operator (:)

*Extract row i: A(i, :)

*Extract column j: A(:,j)

*Extract submatrix: A(m,n) being m and n vectors of integers

EXTRACT SUBMATRIX?

3.4. GENERATION OF SPECIAL MATRICES:

-Unit square of size nxn: eye(n)

-0 matrix os size nxm: zeros(n,m)

-Matrix with random coef. between 0 and 1 of size nxm:rand(n,m)


We don't know how to generate a matrix with random numbers high than 1, so we are
goint to do this task in two steps: first we generate a random numbers matrix and then we
multiply this matrix by 5, so we get as a result a matrix with random numbers between 0 and
5.

It's important to see that in both steps the matrix have been called A. We do this to
keep the matrix A as the result asked (when we define a variable and then redefine it, the
machine keeps the last order)
vector m:

Extractions of row and column 3:


Matrix with odd rows and columns using m:

COMPLETE WHEN SOLVED

3.5. DETERMINANT OF SQUARE MATRICES:

In Octave, det(A)
We can see that the relation
between det(A) and det(aA) is
equal to a^3. This is because a
multiplies all the rows OR
columns, so if we "take it out" we
extract 3 times a, which is the
same as saying that we extract
a*a*a=a^3

3.6. INVERSE OF A SQUARE MATRIX:

A n-by-n matrix A is called invertible or nonsingular or nondegenerate, if there exists an n-


by-n matrix B such that AB = BA = I*n. In this case B is called the inverse matrix of A,
denoted by A^-1
*If the determinant is equal to zero,a matrix has no inverse (it's singular)
*The inverse of a product is the product of the inverses.
*The det of an inverse is the inversed determinant.
In octave, inv(A)
This are the variables the task
asked for.

The inv(a*A) is the same as


dividing inv(A) for a. We are
actually doing a^-1*inv(A)
As expected, the result is the unit
matrix

To complete this part of the task, we have to remember that Octave allows us to do things
that may not be correct. In this case, A is a singular matrix, and the machine warns us that
it is, but even though it operates the inverse. If A would,'t be singular, the result of
inv(invA) would be A, but it isn't.

4.- Programming using Matlab.


4.1.- Relational operators: if a and b are two variables, the comparison between
them is a relational operator that returns a logical value (1 for true or 0 for false)
-Equal: a==b (if we use only one = the program will renew the value of a)
-Greater a>b ; greater than a>=b (same for lower)
-Different than a!=b or a~=b
4.2. Terminal input/output:
-DISPLAY: We use this command to ask the program to show something (a
variable that have been defined before, a string that we are defining inside the command...)
To control the output, we use fprintf, whose arguments are
*%d for entire decimal numbers
*%s for strings and characters
*%f and %e for floatings points in standard or engineering notation.
*\n closes the line
-READ A VALUE: we use the command var=input(' ') It displays what we put
between the commas and waits until this input is received. The value is saved as var

Result:

Code:

4.3.- Logical operators:


Logical operations returns a logical variable (1 or 0)
expr?

4.4.- Conditions:
We want: As a result f a given condition, differents expressions are run.
We use: statements if--elseif---else
IMPORTANT: WE MUST USE END.
Example:
Code:

What is displayed:
Code:

Solution:

4.5. Loops:
We use loops when we want something to be repeated several times. There are many
types of loops.
and we change the n=0:the number asked.

4.6. Scripts and programs:


A script is a sequence of operations structured to perfom a complex procedure.

Code:

WARNING: TO RUN THIS WE HAVE TO


WRITE THE NAME OF THE FUNCTION:

You might also like