%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Este programa ha sido realizado por Ramón Rojas Díaz como parte de su     %
%%Proyecto Fin de Carrera. Con él se pretende determinar,localizar y        %
%%cuantificar la presencia de daño en estructuras. Para ello se determina   %
%%la variación de sus propiedades dinámicas y se aplican diversos métodos   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

opcion=menu('¿Que método desea emplear?','1.-Cambios en las frecuencias','2.-Cambios en los modos',...
    '3.-Matriz de flexibilidad','4.-Matriz de rigidez','5.-Método Stubbs','6.-Cambio en la curvatura de los modos',...
    '7.-MAC','8.-Salir');

%Para el formato de datos normalizado, el bucle siguiente
%extrae los modos con y sin daño, así como las frecuencias naturales.
%matsin y matcon son dos matrices donde están almacenados
%los modos de vibración de la estructura sin daño y con daño
%respectivamente
for i=1:1:m %m es el nº de modos
    wsin(1,i)=2*pi*phisin((i-1)*n+3*i-1);
    wcon(1,i)=2*pi*phicon((i-1)*n+3*i-1);
    realsin(:,i)=phisin(((i-1)*n+3*i+1):i*(n+3)).*cos(angsin(((i-1)*n+3*i+1):i*(n+3)));
    imagsin(:,i)=phisin(((i-1)*n+3*i+1):i*(n+3)).*sin(angsin(((i-1)*n+3*i+1):i*(n+3)));
    modoauxsin(:,i)=phisin(((i-1)*n+3*i+1):i*(n+3))+sqrt(-1)*angsin(((i-1)*n+3*i+1):i*(n+3));
    realcon(:,i)=phicon(((i-1)*n+3*i+1):i*(n+3)).*cos(angcon(((i-1)*n+3*i+1):i*(n+3)));
    imagcon(:,i)=phicon(((i-1)*n+3*i+1):i*(n+3)).*sin(angcon(((i-1)*n+3*i+1):i*(n+3)));
    modoauxcon(:,i)=phicon(((i-1)*n+3*i+1):i*(n+3))+sqrt(-1)*angcon(((i-1)*n+3*i+1):i*(n+3));
    matsin2(:,i)=realsin(:,i)+imagsin(:,i)*pinv(realsin(:,i))*imagsin(:,i);
    matcon2(:,i)=realcon(:,i)+imagcon(:,i)*pinv(realcon(:,i))*imagcon(:,i);
    
    
    %Corrección de desfase entre modos
    if matsin2(:,i)'* matcon2(:,i)<0
       matcon2(:,i)=-matcon2(:,i);
    end
end
    
    %Suavizado de modos mediante curve-fitting
   %[matsin2,matcon2] = curve_fitting_v2 (matsin2,matcon2,m);
   

indice=0;
for k=1:1:length(n_puntos_barra)
    for j=1:1:m
        matsin(indice+1:indice+n_puntos_barra(k),j)=matsin2(inic_fin(k,1):inic_fin(k,2),j);
        matcon(indice+1:indice+n_puntos_barra(k),j)=matcon2(inic_fin(k,1):inic_fin(k,2),j);
    end
    indice=n_puntos_barra(k)+indice;
end
%mats=matsin;
%for i=1:m
 %   for j=1:1:n
  %      matsin(j,i)=matsin(j,i)*(1/sqrt(masa(j,j)));
   %     matcon(j,i)=matcon(j,i)*(1/sqrt(masa(j,j)));
   %end
   %end
%matsin2(:,2)=-matsin2(:,2);
%matsin2(:,4)=-matsin2(:,4);
%matsin2(:,8)=-matsin2(:,8);
%matsin2(:,9)=-matsin2(:,9);
%matsin2(:,10)=-matsin2(:,10);
%Hay que determinar h

coorde=[];
for k=1:1:nbarra
    coorde=[coorde;coordenadas(inic_fin(k,1):inic_fin(k,2),:)];
end
[fil_coord,col_coord]=size(coorde);
for i=1:1:fil_coord-1
    if coorde(i,:)==coorde(i+1,:)
        if coord(i-1)==coorde(i-1,1)
            coord(i)=coorde(i,1);
        else
            coord(i)=coorde(i,2);
        end 
    else
        if coorde(i,1)==coorde(i+1,1);
            coord(i)=coorde(i,2);
        else
            coord(i)=coorde(i,1);
        end
    end
end
if coord(fil_coord-1)==coorde(fil_coord-1,1)
    coord(fil_coord)=coorde(fil_coord,1);
else
    coord(fil_coord)=coorde(fil_coord,2);
end

%coord(10)=1.59;%%%%%%%%%%%%%%%OJOOOOOO


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 1%%%%%%%%%%%%%%%%%
%%%%Cambios en las frecuencias naturales%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if opcion==1
    frecuencias;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 2%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%Cambios en los modos%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if opcion==2
    cambios_modos; 
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 3%%%%%%%%%%%%%%%%%
%%%%%%%%%%%Matriz de flexibilidad%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if opcion==3
    flexibilidad;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 4%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%Matriz de rigidez%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if opcion==4
    rigidez;
end%Este es el en del if opcion==3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 5%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%STUBBS%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if opcion==5
    stubbs;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%MÉTODO 6%%%%%%%%%%%%%%%%%
%%%Cambios en la curvatura de los modos%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if opcion==6
    cambios_curvatura;
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%MÉTODO 7%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%MAC%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if opcion==7
    funcion_MAC;
end                               


if opcion==8
    disp('Gracias por usar "DIbEMA".')
    disp('Este programa ha sido realizado por RAMÓN ROJAS DÍAZ')
    disp('como parte de su Proyecto Fin de Carrera.')
    disp('Para más información, escriba a <<reimonrd@hotmail.com>>')
    break
end

algo_mas=menu('¿Desea realizar algún cálculo más?','Sí','No');
clc
close all
if algo_mas==1
    dibema;
end
if algo_mas==2
    disp('Gracias por usar "DIbEMA".')
    disp('Este programa ha sido realizado por RAMÓN ROJAS DÍAZ')
    disp('como parte de su proyecto fin de carrera.')
    disp('Para mas información, escriba a <<reimonrd@hotmail.com>>')
end