Programacio Basica 2
Programacio Basica 2
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:
In essence, a floating point is a number that can have decimal parts (being 3.1
or 5.8955E89).
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
Inside the formats we can display the result in scientifical notation (xEy)
>> 1/7
ans = 1.42857142857143e-001
>> 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:
>> 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
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
>> 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.
-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
-Extract rows or columns from a given matrix: using again range operator (:)
EXTRACT SUBMATRIX?
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:
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
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.
Result:
Code:
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.
Code: