MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work...

10
Practica 1

description

MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices and arrays.All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra.Array CreationMatrix and Array OperationsConcatenationComplex NumbersArray Creation

Transcript of MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work...

Page 1: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

Practica 1

Page 2: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

1) Crea las matrices A y B además realiza las siguientes operaciones:

A =

6 9 5 1 8 7 2 3 1 3 4 4 5 2 8 2

>> B=[4 8;3 7; 2 3; 5 1]

B =

4 8 3 7 2 3 5 1

>> E1=A(:,3:4)

a)E1 =

5 1 2 3 4 4 8 2

b) >> E2=A(1:2,2:3)

E2 =

9 5 7 2

c) >> E3=zeros(4)

Page 3: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

E3 =

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

>> E3(:,1:2)=E1;>> E3(:,3:4)=B;>> E3

E3 =

5 1 4 8 2 3 3 7 4 4 2 3 8 2 5 1

d) >> Prod=A(2,4)*B(1,2)

Prod =

24

2) Encuentre el resultado y que significa cada una de ellas:

>> A(:,1) /nos permite mostrar toda la primera columna de “A”

ans =

6 8 1 5>> A(2,:) /nos permite mostrar toda la fila 2 de “A”

ans =

8 7 2 3

>> A(:,2:3) /nos permite mostrar la 2° y 3° columna de “A”

ans =

Page 4: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

9 5 7 2 3 4 2 8

>> A(:,1:2:3) /nos permite mostrar la 1° y 3° columna saltándonos la 2, ya que avanza de 2 en 2.

ans =

6 5 8 2 1 4 5 8>> B=[A,[ones(1,2);eye(2)]] /nos permite añadirle a la matriz “B” una matriz de puro unos y la matriz identidad de 2 x 2.

>> A(:,:) /nos muestra toda la matriz “A” ans =

6 9 5 1 8 7 2 3 1 3 4 4 5 2 8 2

>> ones(3,3) /nos muestra una matriz de puros unos de 3 x 3.

ans =

1 1 1 1 1 1 1 1 1

>> eye(3) /nos muestra la matriz identidad de 3 x 3.

ans =

1 0 0 0 1 0 0 0 1

Page 5: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

>> diag(A) /nos muestra la diagonal principal de la matriz A

ans =

6 7 4 2>> zeros(size(A)) /crea una matriz llena de Ceros de acuerdo al tamaño de la matriz “A”

ans =

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>> rand(size(A)) /genera una matriz aleatoria de acuerdo al tamaño de matriz “A”

ans =

0.4218 0.6557 0.6787 0.6555 0.9157 0.0357 0.7577 0.1712 0.7922 0.8491 0.7431 0.7060 0.9595 0.9340 0.3922 0.0318>> magic(length(A)) / genera una matriz del tamaño de la matriz “A” dicha matriz toda sus columnas y filas suman lo mismo.(34)

ans =

16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

3) Grafica las sgte funciones:a)

>> t=-7:0.0467:7;>> y=(1-(sin(t).^2))./t;

Page 6: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

>> z=(t+1)./(t.^2+4);>> plot(t,y,t,z)

b) >> t=0.01:0.01:1;>> y=sin(1./t);>> plot(t,y)

>> t=0.01:0.01:1;>> y=sin(1./t);>> fplot('sin(1./t)',[0.01,1])

4)

function [min,max,m,d]=est(x) minimo=x(1); maximo=x(1); for i=2:length(x); if x(i)<minimo minimo=x(i); end if x(i)>maximo; maximo=x(i); end end display(minimo); display(maximo);media=(sum(x)/length(x)) n=0; for n=0:(length(x)-1); m=n+1; a(m)=(x(m)).^2; end a2=sum(a); desviacion=sqrt(a2/length(a)) end

Page 7: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

5. Informe.-

v=input('introdusca un vector fila: ');b=input('introdusca el valor a buscar: '); media=mean(v)desviacion_media=sum(abs(v-media))/length(v)vector_ordenado=sort(v)n=length(v);s=(b==v); if sum(s)>=1 u=find((b==v)>=1); fprintf('La posicion del valor es:') disp(u) disp(' ')else disp('el valor no se encuentra dentro del vector')end

Page 8: MATLAB is an abbreviation for "matrix laboratory." While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices

6) Vector creado en clase

x=input('introdusca un vector fila: ');Promedio=(sum(x)/length(x))minimo=x(1)for i=2:length(x);if x(i)<minimo minimo=x(i); end fprintf('El valor del mínimo y el Promedio son:',minimo,Promedio)end

7) Matriz creada en clase>> B=ones(4)

B =

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

>> C=B(2,:)+1;>> D=B(3,:)+2;>> E=B(4,:)+3;>> B=[B(1,:);C;D;E]

B =

1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4