Você está na página 1de 14

ASIGNACION UNO: PROGRAMMING REVIEW (MATLAB)

RAFHAEL ANDRES LUNA TEZNA (502147694)

Reporte presentado al profesor: Luis Montejo Ph.D, en la materia:


(INCI6997) : Practicum in Signal Processing and Data Analysis.

ENERO 28 DE 2015

PROGRAMA DOCTORAL DE INGENIERIA CIVIL 0580


UNIVERSIDAD DE PUERTO RICO RECINTO UNIVERSITARIO DE MAYAGUEZ

PUNTO UNO - VALE 20%


El cdigo en MATLAB de la rutina es el siguiente:
%
% PUNTO UNO - (VALE 20) ENCONTRAR NUMEROS PARES E IMPARES EN UNA MATRIZ,
% ELABORADO POR RAFHAEL ANDRES LUNA TEZNA
%
clc, clear all
% INGRESO DE LA MATRIZ
disp('===================================================================')
A = input(' DIGITE LA MATRIZ A EN LA FORMA ( p.e [1 2; 3,4] ) : ');
disp('===================================================================')
t=size(A);
f=t(1);
c=t(2);
auxp=1;
auxi=1;
for i=1:c
for j=1:c
if mod(A(i,j),2)==0
par(auxp)=A(i,j);
auxp=auxp+1;
else
impar(auxi)=A(i,j);
auxi=auxi+1;
end
end
end
disp(['NUMEROS PARES : ',num2str(par)])
disp(['NUMEROS PARES : ',num2str(impar)])
disp('===================================================================')

El pantallazo de la entrada y salida del programa es el siguiente:

Figura Uno. Resultado Entrada/Salida en la ventanas de comandos de MATLAB, punto uno.

PUNTO DOS - VALE 40%


A continuacin se deduce el ajuste por mnimos cuadrados de una tabulacin de puntos (xi,yi) a una funcin
polinomica de segundo grado de la forma y a bx cx 2 .

Figura Dos. Ajuste por mnimos cuadrados de tabulacin de puntos a una funcin polinomica de segundo grado.

El error al ajustar un punto P(xi,yi) a una funcin polinomica de segundo orden de la forma y a bx cx 2 est
dado por:
Ei y i a bx i cx i2
Para despreciar el signo del error y evitar un clculo errado, se trabaja con el cuadrado del error as:

Ei2 ( y i a bx i cx i2 ) 2
E2

2
i

i1

El mejor ajuste se da cuando el error al cuadrado es mnimo, por lo tanto:

E 2
0
a
n

(y a bx cx ) 0

2
i

i1

yi a

i1

a.n b

1 b

i1

i1

xi c

xi c

i1

i1

x i2

2
i

i1

y
i1

(1)

E 2
0
b
n

(y a bx cx )x

2
i

i1

x y a x b x
i i

i1

i1

xi b

i1

2
i

i1

x i2 c

i1

3
i

i1

x i3

i1

x y

i i

(2)

i1

E2
0
c
n

(y a bx cx )x

2
i

2
i

i1

x i2 y i a

i1

i1

x i2 b

i1

x i2 b

i1

x i3 c

x i3 c

i1
n

i1

x i4

4
i

i1

x y

2
i i

(3)

i1

Escribiendo matricialmente las ecuaciones 1, 2 y 3 se tiene:

n
xi
in1

2
xi
i1

x i2
yi

i 1
a i1
n
n

x i3 b x i y i
i 1

c in1
n

2
x i4
xi y i
i 1

i1
n

i 1
n

i 1
n

xi
2
i
3
i

i 1

Resolviendo matricialmente se encuentra el valor de los coeficientes a, b y c del polinomio de segundo orden
ajustado a los datos de la tabulacin.
Las ecuaciones de polinomios de ajuste de oren superior, se sacan por simple inspeccin, as:
POLINOMIO DE GRADO UNO: y a bx

n
xi
i1

xi
yi

i1
a i1
n
n
b
x i2 x i y i
i1

i1

POLINOMIO DE GRADO DOS: y a bxi cx i2

n
xi
in1

2
xi
i1

x i2
yi

i 1
a i1
n
n

x i3 b x i y i
i 1

c in1
n

4
2
xi
xi y i
i 1

i1
n

i 1
n

i 1
n

xi
2
i
3
i

i 1

POLINOMIO DE GRADO TRES: y a bx cx2 dx3

n
xi
in1

2
xi
in1

3
xi
i1

x x

x x

x x

i 1
n
i 1
n

x i3
yi

i 1
i1

n
a n

4
xi xiy i
b

i 1
in1

n
c
x i5 x i2 y i

i 1
d in1

x i3 y i
x i6
i1

i 1

i 1
n

xi
2
i
3
i

4
i

i 1

i 1
n
i 1
n

i 1
n

x i2
3
i

4
i
5
i

i 1

En el mtodo de ajuste por mnimos cuadrados, una forma de medir la exactitud de la correlacin de los valores
tabulados y la funcin polinomica ajustada es por medio del coeficiente R, que relaciona el ajuste de los datos
tabulados y la funcin ajustada con respecto al valor de la media, as:
n

f (x i ) y2
i1
n

(y y)
i

, y

i1

i1

El valor de R se puede interpretar as:

VALOR DE R

OBSERVACION

0.0 R < 0.2

CORRELACION MUY DEBIL, DESPRECIABLE

0.2 R < 0.4

CORRELACION DEBIL BAJA

0.4 R < 0.7

CORRELACION MODERADA

0.7 R < 0.9

CORRELACION FUERTE ALTA, IMPORTANTE

0.9 R < 1.0

CORRELACION MUY FUERTE, MUY ALTA

R = 1.0

CORRELACION EXACTA

El cdigo en MATLAB de la funcin de ajuste por mnimos cuadrados lineal, cuadrtica y cubica es el siguiente:
%
% FUNCION DE AJUSTE POR MINIMOS CUADRADOS LINEAL CUADRATIC0 Y CUBICO
% ELABORADA POR RAFHAEL ANDRES LUNA TEZNA
%
function [al,bl,fxl,R2l,REl REml,as,bs,cs,fxs,R2s,REs REms,ac,bc,cc,dc,...
fxc,R2c,REc,REmc ] = minc(xi,yi)
n=length(xi);
ym = mean(yi);
% AJUSTE LINEAL
Al = [n sum(xi);sum(xi) sum(xi.^2)];
vl = [sum(yi);sum(xi.*yi)];
xl = inv(Al)*vl;
al = xl(1,1);
bl = xl(2,1);
fxl = al+bl*xi;
REl = yi-fxl;
REml = mean(REl.^2);
R2l = sum((fxl-ym).^2)/sum((yi-ym).^2);
% AJUSTE CUADRATICO
As = [n sum(xi) sum(xi.^2);sum(xi) sum(xi.^2) sum(xi.^3); sum(xi.^2)...
sum(xi.^3) sum(xi.^4)];
vs = [sum(yi);sum(xi.*yi);sum((xi.^2).*yi)];
xs = inv(As)*vs;
as = xs(1,1);
bs = xs(2,1);
cs = xs(3,1);
fxs = as+bs*xi+cs*xi.^2;
REs = yi-fxs;
REms = mean(REs.^2);
R2s = sum((fxs-ym).^2)/sum((yi-ym).^2);
% AJUSTE CUBICO
Ac = [n sum(xi) sum(xi.^2) sum(xi.^3);sum(xi) sum(xi.^2) sum(xi.^3)...
sum(xi.^4);sum(xi.^2) sum(xi.^3) sum(xi.^4) sum(xi.^5);sum(xi.^3)...
sum(xi.^4) sum(xi.^5) sum(xi.^6)];
vc = [sum(yi);sum(xi.*yi);sum((xi.^2).*yi);sum((xi.^3).*yi)];

xc = inv(Ac)*vc;
ac
bc
cc
dc

=
=
=
=

xc(1,1);
xc(2,1);
xc(3,1);
xc(4,1);

fxc = ac+bc*xi+cc*xi.^2+dc*xi.^3;
REc = yi-fxc;
REmc = mean(REc.^2);
R2c = sum((fxc-ym).^2)/sum((yi-ym).^2);

El cdigo en MATLAB de la rutina es el siguiente:


%
% PUNTO DOS - (VALE 40) AJUSTE POR MINIMOS CUADRADOS LINEAL CUADRATIC0 Y
% CUBICO ELABORADO POR RAFHAEL ANDRES LUNA TEZNA
%
clc, clear all, close all
% CARGA DE DATOS
datos = load('myfirstnoisydata.txt');
x = datos(:,1);
y = datos(:,2);
% CALCULO DE AJUSTE POR MINIMOS CUADRADOS
[al,bl,fxl,R2l,REl REml,as,bs,cs,fxs,R2s,REs REms,ac,bc,cc,dc,fxc,R2c,...
REc,REmc ] = minc(x,y);
% PRESENTACION DE RESULTADOS NUMERICOS
disp('===================================================================')
disp('AJUSTE LINEAL ')
disp(['ECUACION DE AJUSTE: Y= ',num2str(al),'+',num2str(bl),'*X'])
disp(['PROMEDIO DEL CUADRADO DE LOS RESIDUOS: ',num2str(REml)])
disp(['FACTOR DE CORRELACION R2: ',num2str(R2l)])
disp('===================================================================')
disp('AJUSTE CUADRATICO ')
disp(['ECUACION DE AJUSTE: Y= ',num2str(as),'+',num2str(bs),'*X+',...
num2str(cs),'*X^2'])
disp(['PROMEDIO DEL CUADRADO DE LOS RESIDUOS: ',num2str(REms)])
disp(['FACTOR DE CORRELACION R2: ',num2str(R2s)])
disp('===================================================================')
disp('AJUSTE CUBICO ')
disp(['ECUACION DE AJUSTE: Y= ',num2str(ac),'+',num2str(bc),'*X+',...

num2str(cc),'*X^2+',num2str(dc),'*X^3'])
disp(['PROMEDIO DEL CUADRADO DE LOS RESIDUOS: ',num2str(REmc)])
disp(['FACTOR DE CORRELACION R2: ',num2str(R2c)])
disp('===================================================================')
% PRESENTACION GRAFICA DE RESULTADOS
figure
subplot(2,1,1)
plot(x,y,'ro',x,fxl,'b')
grid on
title('AJUSTE LINEAL POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('y')
legend(': DATOS TABULADOS',[': Y= ',num2str(al),'+',...
num2str(bl),'*X
R^2 : ',num2str(R2l)])
subplot(2,1,2)
plot(x,REl,'go')
grid on
title('RESIDUOS DEL AJUSTE LINEAL POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('RESIDUOS')
figure
subplot(2,1,1)
plot(x,y,'ro',x,fxs,'b')
grid on
title('AJUSTE CUADRATCO POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('y')
legend(': DATOS TABULADOS',[': Y= ',num2str(as),'+',...
num2str(bs),'*X+',num2str(cs),'*X^2
R^2 : ',num2str(R2s)])
subplot(2,1,2)
plot(x,REs,'go')
grid on
title('RESIDUOS DEL AJUSTE CUADRATICO POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('RESIDUOS')
figure
subplot(2,1,1)
plot(x,y,'ro',x,fxc,'b')
grid on
title('AJUSTE CUBICO POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('y')
legend(': DATOS TABULADOS',[': Y= ',num2str(ac),'+',num2str(bc),'*X+',...
num2str(cc),'*X^2+',num2str(dc),'*X^3
R^2 : ',num2str(R2c)])
subplot(2,1,2)
plot(x,REc,'go')
grid on
title('RESIDUOS DEL AJUSTE CUBICO POR MINIMOS CUADRADOS')
xlabel('x'), ylabel('RESIDUOS')

El pantallazo de salida del programa es el siguiente:

Figura Tres. Resultado de salida en la ventana de comandos de MATLAB, punto dos.

Las grficas generadas por la rutina en MATLAB son las siguientes:

Figura Cuatro. Ajuste por mnimos cuadrados de tabulacin de puntos a una funcin polinomica de primer orden.

Figura Cinco. Ajuste por mnimos cuadrados de tabulacin de puntos a una funcin polinomica de segundo orden.

Figura Seis. Ajuste por mnimos cuadrados de tabulacin de puntos a una funcin polinomica de tercer orden.

De acuerdo a los valores de R encontrados para cada una de las aproximaciones, se tiene:

APROXIMACION

VALOR DE R

OBSERVACION

LINEAL

0.6836

CORRELACION MODERADA

CUADRATICA

0.87982

CORRELACION FUERTE ALTA, IMPORTANTE

CUBICA

0.92691

CORRELACION MUY FUERTE, MUY ALTA

De lo anterior se puede concluir que entre las funciones trabajadas, la funcin polinomica de tercer orden o
cbica es la que mejor se ajusta a los datos tabulados.

PUNTO TRES - VALE 40%


El cdigo en MATLAB de la rutina es el siguiente:
%
% PUNTO TRES - (VALE 40) GRAFICA DE RESULTADOS EN TIEMPO Y EN FRECUENCIA DE
% ANALISIS DE SEAL CON "WAVELETS" ELABORADO POR RAFHAEL ANDRES LUNA TEZNA
%
clc, clear all, close all
% CARGA DE DATOS
amp = load('2dofreen.txt');
wv = load('wavtimefreq.txt');
% VECTOR DE TIEMPO
mt = length(amp);
ht = 0.01;
t

= 0:ht:(mt-1)*ht;

% VECTOR DE FRECUENCIA
fi
ff
hf

= 0.1;
= 6;
= 0.1;

= fi:hf:ff;

% GAFICA DE AMPLITUD vs TIEMPO


Figure
plot(t,amp)
grid on
title('GAFICA DE AMPLITUD vs TIEMPO')
xlabel('TIEMPO [seg]'), ylabel('AMPLITUD')
% GRAFICA TRIDIMENSIONAL
Figure
surf(t,f,wv,'FaceColor','interp','EdgeColor','none','FaceLighting',...
'phong')
colormap jet;

grid on
title('GAFICA TRIDIMENSIONAL COEFICIENTES ANALISIS WAVELET')
xlabel('TIEMPO [seg]'), ylabel('FRECUENCIA [Hz]'), zlabel('C')
% GRAFICA WAVELET MAP
Figure
surf(t,f,wv,'FaceColor','interp','EdgeColor','none','FaceLighting',...
'phong')
colormap jet;
grid on
title('GRAFICA WAVELET MAP')
xlabel('TIEMPO [seg]'), ylabel('FRECUENCIA [Hz]'), zlabel('C')
view([0 90])
% GRAFICA SKELETON DEL ANALISIS WAVELET
pc = 30;
wv1=wv(1:pc,:);
wv2=wv(pc+1:2*pc,:);
for i=1:mt
max1 = max(wv1(:,i));
pos
= find(wv1(:,i)==max1);
f1(i) = f(pos);
max2 = max(wv2(:,i));
pos
= find(wv2(:,i)==max2);
f2(i) = f(pos)+f(pc);
end
figure
plot(t,f1,t,f2)
grid on
title('GAFICA SKEKETON DE FRECUENCIAS')
xlabel('TIEMPO [seg]'), ylabel('FRECUENCIA [Hz]')
% CREACION DE ARCHIVO DE SALIDA
outtable = [t' f1' f2'];
save('outfile.txt', 'outtable', '-ASCII')

Las grficas generadas por la rutina en MATLAB son las siguientes:

Figura Siete. Grafica de Amplitud vs Tiempo de la seal del sistema de 2 GDL.

Figura Ocho. Grfica del anlisis por Wavelet en frecuencia y tiempo del sistema de 2 GDL.

Figura Nueve. Grfica en planta del anlisis por Wavelet en frecuencia y tiempo del sistema de 2 GDL

Figura Diez. Grfica Skeketon del anlisis por Wavelet en frecuencia y tiempo del sistema de 2 GDL

Se debe tener en cuenta que la rutina en MATLAB genera un archivo de salida outfile.txt que contiene los
valores de tiempo y skeleton de frecuencias.

Você também pode gostar