function varargout = simulador2(varargin)
% simulador2 M-file for simulador2.fig
%      simulador2, by itself, creates a new simulador2 or raises the existing
%      singleton*.
%
%      H = simulador2 returns the handle to a new simulador2 or the handle to
%      the existing singleton*.
%
%      simulador2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in simulador2.M with the given input arguments.
%
%      simulador2('Property','Value',...) creates a new simulador2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before simulador2_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to simulador2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help simulador2

% Last Modified by GUIDE v2.5 03-Oct-2006 10:26:58

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @simulador2_OpeningFcn, ...
                   'gui_OutputFcn',  @simulador2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before simulador2 is made visible.
function simulador2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
%%% Inicializar variables

handles.valores.masa = 1;
handles.valores.viscosidad = 1;
handles.valores.elastica = 1;
handles.valores.w=1;
handles.valores.F=1;
handles.Tmax=10;
handles.valores.wmax=10;
set(handles.text8,'String','T.Max') 
set(handles.text3,'String','Viscosidad')
set(handles.text3,'FontSize',18);set(handles.text1,'FontSize',18);set(handles.text4,'FontSize',18)
set(handles.edit3,'FontSize',18);set(handles.edit2,'FontSize',18);set(handles.edit1,'FontSize',18)
set(handles.text5,'FontSize',18);set(handles.text7,'FontSize',18)
set(handles.text8,'FontSize',18);set(handles.edit8,'FontSize',18);
set(handles.text10,'FontSize',18);set(handles.edit11,'FontSize',18);
set(handles.text9,'FontSize',18);set(handles.edit10,'FontSize',18);
set(handles.text11,'FontSize',18);set(handles.text11,'String','Q:')
handles=set_time_values(handles);
guidata(hObject,handles)

% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to simulador2 (see VARARGIN)

% Choose default command line output for simulador2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes simulador2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = simulador2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
axes(handles.axes1);ptype='posicion';
oscilador2(handles.valores.masa,handles.valores.viscosidad,handles.valores.elastica,handles.valores.w,handles.valores.F,handles.Tmax,ptype)

% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
masa = str2double(get(hObject, 'String'));
if isnan(masa)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.valores.masa = masa;
handles=set_time_values(handles);
guidata(hObject,handles)
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
set(hObject, 'String', '1');
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit2_Callback(hObject, eventdata, handles)
viscosidad = str2double(get(hObject, 'String'));
if isnan(viscosidad)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.valores.viscosidad = viscosidad;
handles=set_time_values(handles);
guidata(hObject,handles)

% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
set(hObject, 'String', '1');

% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end





function edit3_Callback(hObject, eventdata, handles)
elastica = str2double(get(hObject, 'String'));
if isnan(elastica)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.valores.elastica = elastica;
handles=set_time_values(handles);
guidata(hObject,handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
set(hObject, 'String', '1');
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

function handles=set_time_values(handles)
M=handles.valores.masa;V=handles.valores.viscosidad;K=handles.valores.elastica;
st=(2*pi)/(sqrt(K/M));
handles.valores.Posc = st;
handles.valores.wo =  sqrt(K/M);
handles.valores.Q=sqrt(handles.valores.masa*handles.valores.elastica)/handles.valores.viscosidad;
r=num2str(st,'%0.2g')
set(handles.text7,'String',strcat('Per.Osc: ',r))
set(handles.text5,'String',strcat('Frecuencia: ',num2str(handles.valores.wo,'%0.2g')))
set(handles.pushbutton1,'String','Simular')
set(handles.text11,'String',strcat('Q:',num2str(handles.valores.Q,'%0.2g')))

function [Z,fi]=espectro(m,gama,k,wmax)
w=[0:0.01:wmax];
Z=sqrt(gama*gama+power((w*m-k./w),2));
fi=atan((w*m-k./w)/gama);
Z=1./Z;



function edit8_Callback(hObject, eventdata, handles)
Tmax = str2double(get(hObject, 'String'));
if isnan(Tmax)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.Tmax = Tmax;
guidata(hObject,handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double



% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
print -djpeg -r300 simulacion1
%printdlg(handles.figure1)



% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --------------------------------------------------------------------
function Filemenu_Callback(hObject, eventdata, handles)
% hObject    handle to Filemenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes during object creation, after setting all properties.
function text1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called




% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end





function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit10_Callback(hObject, eventdata, handles)
F = str2double(get(hObject, 'String'));
if isnan(F)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.valores.F = F;
handles=set_time_values(handles);
guidata(hObject,handles)

% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit11_Callback(hObject, eventdata, handles)
w = str2double(get(hObject, 'String'));
if isnan(w)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end
handles.valores.w = w;
handles=set_time_values(handles);
guidata(hObject,handles)
% hObject    handle to edit11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit11 as text
%        str2double(get(hObject,'String')) returns contents of edit11 as a double


% --- Executes during object creation, after setting all properties.
function edit11_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit11 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
[Z,fi]=espectro(handles.valores.masa,handles.valores.viscosidad,handles.valores.elastica,handles.valores.wmax);
Wvals=[0:0.01:handles.valores.wmax];
axes(handles.axes2);plot(Wvals,Z,'LineWidth',2);
line([handles.valores.w handles.valores.w],[0,max(Z)],'Color','g','LineWidth',3);
axes(handles.axes3);plot(Wvals,fi+pi/2,'LineWidth',2);
line([0,handles.valores.wmax],[pi/2,pi/2],'Color','r','LineWidth',2)
line([handles.valores.wo handles.valores.wo],[0,pi],'Color','g','LineStyle','--','LineWidth',2);
line([handles.valores.w handles.valores.w],[0,pi],'Color','g','LineWidth',3);
axis([0 handles.valores.wmax  0 pi])
%oscilador2(handles.valores.masa,handles.valores.viscosidad,handles.valores.elastica,handles.valores.w,handles.valores.F,handles.Tmax)

% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
axes(handles.axes1);cla;axes(handles.axes2);cla;axes(handles.axes3);cla;axes(handles.axes4);cla;
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
axes(handles.axes4);
oscilador2(handles.valores.masa,handles.valores.viscosidad,handles.valores.elastica,handles.valores.w,handles.valores.F,handles.Tmax,'velocidad')
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)




% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
[Z,fi]=espectro(handles.valores.masa,handles.valores.viscosidad,handles.valores.elastica,handles.valores.wmax);
axes(handles.axes3);
%Q=sqrt(handles.valores.masa*handles.valores.elastica)/handles.valores.viscosidad;
Q=handles.valores.Q;
line([handles.valores.wo *(1-1/Q) handles.valores.wo *(1-1/Q)],[0,pi],'Color','g','LineWidth',3,'LineStyle','--');
axes(handles.axes3);
line([handles.valores.wo *(1+1/Q) handles.valores.wo*(1+1/Q)],[0,pi],'Color','g','LineWidth',3,'LineStyle','--');

axes(handles.axes2);
line([handles.valores.wo *(1-1/Q) handles.valores.wo *(1-1/Q)],[0,max(Z)],'Color','g','LineWidth',3,'LineStyle','--');
axes(handles.axes2);
line([handles.valores.wo *(1+1/Q) handles.valores.wo *(1+1/Q)],[0,max(Z)],'Color','g','LineWidth',3,'LineStyle','--');

% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


