octave_2

13
1 % Escribe a archivo % abril 2015 filename=“volumen.res"; % abre archivo para escritura fid = fopen(filename, "w"); r=0; while r < 100 r=input(' introduce el radio = '); disp(r) vol = pi *r^3; if(r < 0) break;end; fprintf(fid, 'vol= %12.3f\n',vol); fprintf('vol = %12.3f \ n',vol);

description

octave cells

Transcript of octave_2

Matrices in Matlab

1% Escribe a archivo % abril 2015

filename=volumen.res";

% abre archivo para escriturafid = fopen(filename, "w");

r=0;while r < 100 r=input(' introduce el radio = '); disp(r) vol = pi *r^3;if(r < 0) break;end;

fprintf(fid, 'vol= %12.3f\n',vol); fprintf('vol = %12.3f \n',vol); end $ cierra archivofclose(fid);2Variables and ArraysArray: A collection of data values organized into rows and columns, and known by a single name.Row 1Row 2Row 3Row 4Col 1Col 2Col 3Col 4Col 5arr(3,2)3ArraysThe fundamental unit of data in octaveScalars are also treated as arrays by octave (1 row and 1 column).Row and column indices of an array start from 1.Arrays can be classified as vectors and matrices.Variables and Arrays4Vector: Array with one dimension

Matrix: Array with more than one dimension

Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first (For example: n x m array).Total number of elements in an array is the product of the number of rows and the number of columns.Variables and Arrays5Obtain informationsize(A): return [m n]length(A): length of a vectorlength(A) = max(size(A))B = A(2:4,3:5)B is the subset of A from row 2 to row 4, column 3 to column 5A(:, 2)=[]Delete second column

6 a = [1 2 3] or a = [1, 2, 3] e.g. V = [1 3 sqrt(5)], what is length(V)?build a row vector from existing ones: e.g. w = [1 2 3], z = [8, 9], cd = [2*z -w], sort(cd) (ascending order)look at value of particular entries: e.g. w(2) = ? set w(3) = 100, then w = ??

Row vectors7Column vectors e.g. c = [1; 3; sqrt(5)] column notation : a shortcut for producing row vectors e.g. 1:100 3:7 5:0.1:6 1:-1 --> [] 0.32:0.1:0.6 -0.4:-0.3:-283254761108911124711025Multidimensional Arrays811Multidimensional ArraysA two dimensional array with m rows and n columns will occupy mxn successive locations in the computers memory. a= [1 2 3; 4 5 6; 7 8 9; 10 11 12];a(5) = a(1,2) = 2

A 2x3x2 array of three dimensions c(:, :, 1) = [1 2 3; 4 5 6 ];c(:, :, 2) = [7 8 9; 10 11 12];9Remember that every variable can be a matrix!

Addition:>> C = A + B

Subtraction:>> D = A B

Multiplication:>> E = A * B (Matrix multiplication)>> E = A .* B (Element wise multiplication, A and B same size)

Division:Left Division and Right Division>> F = A . / B (Element wise division)>> F = A / B = A*inv(B) (A * inverse of B)>> F = A . \ B (Element wise division)>> F = A \ B=inv(A)*B (inverse of A * B)Basic Mathematical Operations10Matrix with ZEROS:>> A = zeros(m, n)

Matrix with ONES:>> B = ones(m, n)

IDENTITY Matrix:>> I = eye(m, n)

m Rowsn Columnszeros, ones, eye Octave functionsGenerating basic matrices11To enter a matrix2 5 36 4 1>> A = [2 5 3; 6 4 1]

>> B = [1:1.5:6; 2 3 4 5] ?

>> for i=1:4 for j=1:3 C(i,j)=i*j; end end

>> D =[]; D=[D;5]; D=[D;6;7] >> E = zeros(4, 5)Examples 12Save/Load Datasave fnameSave all workspace data into fname.matSave fname x y zSave(fname): when fname is a variable

Load fnameload fname x y

13Ejercicio: p--.m En un archivo p--.m, construya la siguiente matriz:X = 1 2 3 4 5 6 7 8 9Calcule el sin(x)Evale de nuevo utilizando instruccin forEjercicio: p--.m En un archivo p--.m, construya las siguientes matrices A = B = 0.1 0.2 0.30.3 0.1 0.2 0.4 0.5 0.60.9 0.7 0.8 0.7 0.8 0.90.4 0.6 0.5Obtenga:C= A+B ; C= A-B; C=A.*B; C=A./B; C=A.^3 Evale utilizando instruccin for