image thumbnail
from Mass Spring Oscillator Animated GUI by Matija Krznar
Animates three masses and four springs. Draws position and speed plots.

Mass_Spring_Oscilator(varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MASS-SPRING SIMULATOR ver: 9.7.2012. beta 0.9

% Copyright By Matija Krznar 20012. All Rights Reserved 2007/2011. 
% You may freely redistribute this file, but you are not permitted to
% modify them without the author's consent.

%Contributions to:

 % Gustavo Morales, gmorales@uc.edu.ve  - Spring() function used to animate 
 % springs
 
 % Jurica Cerovec, jcerovec@gmail.com   - help with GUI coding
 
 % Marko Mihovilic, mihovilic@gmail.com - programming ideas, useful
 % discussions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



function varargout = Mass_Spring_Oscilator(varargin)
% Mass_Spring_Oscilator M-file for Mass_Spring_Oscilator.fig
%      Mass_Spring_Oscilator, by itself, creates a new Mass_Spring_Oscilator or raises the existing
%      singleton*.
%
%      H = Mass_Spring_Oscilator returns the handle to a new Mass_Spring_Oscilator or the handle to
%      the existing singleton*.
%
%      Mass_Spring_Oscilator('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in Mass_Spring_Oscilator.M with the given input arguments.
%
%      Mass_Spring_Oscilator('Property','Value',...) creates a new Mass_Spring_Oscilator or
%      raises the
%      existing singleton*.  Starting from the left, property value pairs
%      are
%      applied to the GUI before GUI_4_timer_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Mass_Spring_Oscilator_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 Mass_Spring_Oscilator

% Last Modified by GUIDE v2.5 09-Jul-2012 18:50:01

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Mass_Spring_Oscilator_OpeningFcn, ...
                   'gui_OutputFcn',  @Mass_Spring_Oscilator_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 Mass_Spring_Oscilator is made visible.
function Mass_Spring_Oscilator_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% 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 Mass_Spring_Oscilator (see VARARGIN)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


plots = guihandles(hObject);

set(handles.start, 'Value',0);
set(handles.reset, 'Value',0);

set(handles.x1,'String', 0.1);
set(handles.slider1mass,'Value', 0.1);
set(handles.x2,'String', 0.0);
set(handles.slider2mass,'Value', 0.0);
set(handles.x3,'String', -0.15);
set(handles.slider3mass,'Value', -0.15);

set(handles.v01,'String', 0.00);
set(handles.slider1velocity,'Value', 0.0);
set(handles.v02,'String', 0.00);
set(handles.slider2velocity,'Value', 0.0);
set(handles.v03,'String', 0.00);
set(handles.slider3velocity,'Value', 0.0);

set(handles.m1,'String', 0.2);
set(handles.m2,'String', 0.25);
set(handles.m3,'String', 1);

set(handles.k1,'String', 10);
set(handles.k2,'String', 1);
set(handles.k3,'String', 16);
set(handles.k4,'String', 20);




axes(handles.animation)
cla;
axis off;


m1n=str2num(get(handles.m1,'String'));
m2n=str2num(get(handles.m2,'String'));
m3n=str2num(get(handles.m3,'String'));

k1n=str2num(get(handles.k1,'String'));
k2n=str2num(get(handles.k2,'String'));
k3n=str2num(get(handles.k3,'String'));
k4n=str2num(get(handles.k4,'String'));

 
% physical dimensions of system, y1 - distance form left wall to first
% mass, y2, distance form left wall to second mass, ... ect. , y4- distance
% from left to right wall
 y1=1;
 y2=2;
 y3=3;
 y4=4;
 

axes(handles.animation)
    cla
axis off;
hold('on')
daspect([1,1,1]); 
   Xx=[0.1 0 -0.15 0 0 0 ]';
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
     

handles.htimer = timer('TimerFcn',{@timerCallback,handles},'Period',0.01,'ExecutionMode','FixedRate');

global t;
t = 0;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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



% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Mass_Spring_Oscilator_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 slider movement.
function slider1mass_Callback(hObject, eventdata, handles)
% hObject    handle to slider1mass (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
value = get(hObject,'Value');

value_num1 = sprintf('%.1f',get(handles.slider1mass,'Value'));

set(findobj('Tag','x1'), 'String', value_num1);
handles.x0(1) = value;
t=0;

% conversion of inital data string -> num

x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]';
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;

Xx=[x1n v01n x2n v02n x3n v03n]';
axes(handles.animation);
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



% --- Executes on slider movement.
function slider2mass_Callback(hObject, eventdata, handles)
% hObject    handle to slider2mass (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = get(hObject,'Value');

value_num2 = sprintf('%.1f',get(handles.slider2mass,'Value'));

set(findobj('Tag','x2'), 'String', value_num2);
handles.x0(1) = value;
t=0;

% inicijalni uvjeti konverzija string -> num

x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]';
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;
Xx=[x1n v01n x2n v02n x3n v03n]';
axes(handles.animation)
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
     


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



% --- Executes on slider movement.
function slider3mass_Callback(hObject, eventdata, handles)
% hObject    handle to slider3mass (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = get(hObject,'Value');

value_num3 = sprintf('%.1f',get(handles.slider3mass,'Value'));

set(findobj('Tag','x3'), 'String', value_num3);
handles.x0(1) = value;


t=0;

% inicijalni uvjeti konverzija string -> num

x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]';
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;
Xx=[x1n v01n x2n v02n x3n v03n]';
axes(handles.animation)
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function x1_Callback(hObject, eventdata, handles)
% hObject    handle to x1 (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 x1 as text
%        str2double(get(hObject,'String')) returns contents of x1 as a double

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider1mass'),'Min');
max = get(findobj('Tag','slider1mass'),'Max');
if (value > get(findobj('Tag','slider1mass'),'Min'))  && (value < get(findobj('Tag','slider1mass'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider1mass'),'Min'))
        value = min
        set(findobj('Tag','x1'), 'String', min);
else
        (value > get(findobj('Tag','slider1mass'),'Max'))
        value = max;
        set(findobj('Tag','x1'), 'String', max)

end
        
 set(findobj('Tag','slider1mass'), 'Value', value);
x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]'
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;
Xx=[x1n v01n x2n v02n x3n v03n]'
axes(handles.animation)
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --- Executes during object creation, after setting all properties.
function x1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x2_Callback(hObject, eventdata, handles)
% hObject    handle to x2 (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 x2 as text
%        str2double(get(hObject,'String')) returns contents of x2 as a double


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider2mass'),'Min');
max = get(findobj('Tag','slider2mass'),'Max');
if (value > get(findobj('Tag','slider2mass'),'Min'))  && (value < get(findobj('Tag','slider2mass'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider2mass'),'Min'))
        value = min
        set(findobj('Tag','x2'), 'String', min);
else
        (value > get(findobj('Tag','slider2mass'),'Max'))
        value = max;
        set(findobj('Tag','x2'), 'String', max)

end
        
 set(findobj('Tag','slider2mass'), 'Value', value);
 x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]'
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;
Xx=[x1n v01n x2n v02n x3n v03n]';
axes(handles.animation)
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% --- Executes during object creation, after setting all properties.
function x2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x3_Callback(hObject, eventdata, handles)
% hObject    handle to x3 (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 x3 as text
%        str2double(get(hObject,'String')) returns contents of x3 as a double



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider3mass'),'Min');
max = get(findobj('Tag','slider3mass'),'Max');
if (value > get(findobj('Tag','slider3mass'),'Min'))  && (value < get(findobj('Tag','slider3mass'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider3mass'),'Min'))
        value = min
        set(findobj('Tag','x3'), 'String', min);
else
        (value > get(findobj('Tag','slider3mass'),'Max'))
        value = max;
        set(findobj('Tag','x3'), 'String', max)

end
        
 set(findobj('Tag','slider3mass'), 'Value', value);
x1n=str2num(get(handles.x1,'String'));
x2n=str2num(get(handles.x2,'String'));
x3n=str2num(get(handles.x3,'String'));
Xx=[x1n 0 x2n 0 x3n 0]'
   x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=0;

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];
y1=1;
y2=2;
y3=3;
y4=4;
Xx=[x1n v01n x2n v02n x3n v03n]'
axes(handles.animation)
     cla;
axis off;
hold('on');
daspect([1,1,1]); 
   
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r') 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function x3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x3 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function k1_Callback(hObject, eventdata, handles)
% hObject    handle to k1 (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 k1 as text
%        str2double(get(hObject,'String')) returns contents of k1 as a double


% --- Executes during object creation, after setting all properties.
function k1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to k1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function k2_Callback(hObject, eventdata, handles)
% hObject    handle to k2 (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 k2 as text
%        str2double(get(hObject,'String')) returns contents of k2 as a double


% --- Executes during object creation, after setting all properties.
function k2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to k2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function k3_Callback(hObject, eventdata, handles)
% hObject    handle to k3 (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 k3 as text
%        str2double(get(hObject,'String')) returns contents of k3 as a double


% --- Executes during object creation, after setting all properties.
function k3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to k3 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function m1_Callback(hObject, eventdata, handles)
% hObject    handle to m1 (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 m1 as text
%        str2double(get(hObject,'String')) returns contents of m1 as a double


% --- Executes during object creation, after setting all properties.
function m1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function m2_Callback(hObject, eventdata, handles)
% hObject    handle to m2 (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 m2 as text
%        str2double(get(hObject,'String')) returns contents of m2 as a double


% --- Executes during object creation, after setting all properties.
function m2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function m3_Callback(hObject, eventdata, handles)
% hObject    handle to m3 (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 m3 as text
%        str2double(get(hObject,'String')) returns contents of m3 as a double


% --- Executes during object creation, after setting all properties.
function m3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m3 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v01_Callback(hObject, eventdata, handles)
% hObject    handle to v01 (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 v01 as text
%        str2double(get(hObject,'String')) returns contents of v01 as a double

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider1velocity'),'Min');
max = get(findobj('Tag','slider1velocity'),'Max');
if (value > get(findobj('Tag','slider1velocity'),'Min'))  && (value < get(findobj('Tag','slider1velocity'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider1velocity'),'Min'))
        value = min
        set(findobj('Tag','v01'), 'String', min);
else
        (value > get(findobj('Tag','slider1velocity'),'Max'))
        value = max;
        set(findobj('Tag','v01'), 'String', max)

end
        
 set(findobj('Tag','slider1velocity'), 'Value', value);
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function v01_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v01 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v02_Callback(hObject, eventdata, handles)
% hObject    handle to v02 (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 v02 as text
%        str2double(get(hObject,'String')) returns contents of v02 as a double


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider2velocity'),'Min');
max = get(findobj('Tag','slider2velocity'),'Max');
if (value > get(findobj('Tag','slider2velocity'),'Min'))  && (value < get(findobj('Tag','slider2velocity'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider2velocity'),'Min'))
        value = min
        set(findobj('Tag','v02'), 'String', min);
else
        (value > get(findobj('Tag','slider2velocity'),'Max'))
        value = max;
        set(findobj('Tag','v02'), 'String', max)

end
        
 set(findobj('Tag','slider2velocity'), 'Value', value);
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes during object creation, after setting all properties.
function v02_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v02 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v03_Callback(hObject, eventdata, handles)
% hObject    handle to v03 (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 v03 as text
%        str2double(get(hObject,'String')) returns contents of v03 as a double

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = str2double(get(hObject,'String'));
min = get(findobj('Tag','slider3velocity'),'Min');
max = get(findobj('Tag','slider3velocity'),'Max');
if (value > get(findobj('Tag','slider3velocity'),'Min'))  && (value < get(findobj('Tag','slider3velocity'),'Max'))
    value = str2double(get(hObject,'String'));
elseif  (value < get(findobj('Tag','slider3velocity'),'Min'))
        value = min
        set(findobj('Tag','v03'), 'String', min);
else
        (value > get(findobj('Tag','slider3velocity'),'Max'))
        value = max;
        set(findobj('Tag','v03'), 'String', max)

end
        
 set(findobj('Tag','slider3velocity'), 'Value', value);
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% --- Executes during object creation, after setting all properties.
function v03_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v03 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





% --- Executes on slider movement.
function slider1velocity_Callback(hObject, eventdata, handles)
% hObject    handle to slider1velocity (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = get(hObject,'Value');

value_num = sprintf('%.2f',get(handles.slider1velocity,'Value'));

set(findobj('Tag','v01'), 'String', value_num);

t=0;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'));
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider2velocity_Callback(hObject, eventdata, handles)
% hObject    handle to slider2velocity (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = get(hObject,'Value');

value_num = sprintf('%.2f',get(handles.slider2velocity,'Value'));

set(findobj('Tag','v02'), 'String', value_num);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider3velocity_Callback(hObject, eventdata, handles)
% hObject    handle to slider3velocity (see GCBO)
% eventdata  reserved - to be defined in a futumre version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

value = get(hObject,'Value');

value_num = sprintf('%.2f',get(handles.slider3velocity,'Value'));

set(findobj('Tag','v03'), 'String', value_num);

t=0;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function k4_Callback(hObject, eventdata, handles)
% hObject    handle to k4 (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 k4 as text
%        str2double(get(hObject,'String')) returns contents of k4 as a double


% --- Executes during object creation, after setting all properties.
function k4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to k4 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x1c_Callback(hObject, eventdata, handles)
% hObject    handle to x1c (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 x1c as text
%        str2double(get(hObject,'String')) returns contents of x1c as a double


% --- Executes during object creation, after setting all properties.
function x1c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x1c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x2c_Callback(hObject, eventdata, handles)
% hObject    handle to x2c (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 x2c as text
%        str2double(get(hObject,'String')) returns contents of x2c as a double


% --- Executes during object creation, after setting all properties.
function x2c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x2c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function x3c_Callback(hObject, eventdata, handles)
% hObject    handle to x3c (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 x3c as text
%        str2double(get(hObject,'String')) returns contents of x3c as a double


% --- Executes during object creation, after setting all properties.
function x3c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to x3c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v1c_Callback(hObject, eventdata, handles)
% hObject    handle to v1c (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 v1c as text
%        str2double(get(hObject,'String')) returns contents of v1c as a double


% --- Executes during object creation, after setting all properties.
function v1c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v1c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v2c_Callback(hObject, eventdata, handles)
% hObject    handle to v2c (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 v2c as text
%        str2double(get(hObject,'String')) returns contents of v2c as a double


% --- Executes during object creation, after setting all properties.
function v2c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v2c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v3c_Callback(hObject, eventdata, handles)
% hObject    handle to v3c (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 v3c as text
%        str2double(get(hObject,'String')) returns contents of v3c as a double


% --- Executes during object creation, after setting all properties.
function v3c_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v3c (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)

stop(handles.htimer);
    start(handles.htimer);

global t;

x1value = str2double(get(handles.x1,'String'));
v01value = str2double(get(handles.v01,'String'));


         

% hObject    handle to start (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 reset.


function reset_Callback(hObject, eventdata, handles)
% hObject    handle to reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%--------------------------------------------------------------------------
% This function resets all to default values.
%  
%--------------------------------------------------------------------------


% 

set(handles.x1,'String', 0.1);
set(handles.slider1mass,'Value', 0.1);
set(handles.x2,'String', 0.0);
set(handles.slider2mass,'Value', 0.0);
set(handles.x3,'String', -0.15);
set(handles.slider3mass,'Value', -0.15);
set(handles.v01,'String', 0.00);
set(handles.slider1velocity,'Value', 0.0);
set(handles.v02,'String', 0.00);
set(handles.slider2velocity,'Value', 0.0);
set(handles.v03,'String', 0.00);
set(handles.slider3velocity,'Value', 0.0);

set(handles.m1,'String', 0.2);
set(handles.m2,'String', 0.25);
set(handles.m3,'String', 1);

set(handles.k1,'String', 10);
set(handles.k2,'String', 1);
set(handles.k3,'String', 16);
set(handles.k4,'String', 20);



Xx=[0 0 0 0 0 0]';
  set(handles.x1c,'String', '');
     set(handles.x2c,'String', '');
     set(handles.x3c,'String', '');

     set(handles.v1c,'String', '');
     set(handles.v2c,'String', '');
     set(handles.v3c,'String', '');


stop(handles.htimer);

global t;
t = 0;
axes(handles.animation)
     cla;
hold('on');

axes(handles.axes1)
     cla;
hold('on');
 
axes(handles.axes2)
     cla;

hold('on');

axes(handles.animation)
    cla
axis off;
hold('on')
daspect([1,1,1]); 
   Xx=[0.1 0 -0.15 0 0 0 ]';
   y1=1;
   y2=2;
   y3=3;
   y4=4;
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
% 



% --- Executes on button press in stop.
function stop_Callback(hObject, eventdata, handles)
% hObject    handle to stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Stop the timer

stop(handles.htimer);
         
function generateGraphics(handles,Xx, y1, y2, y3, y4)
%--------------------------------------------------------------------------
% This function draws animation
%  
%--------------------------------------------------------------------------
cla;
axis off;
hold('on');
daspect([1,1,1]); 

global t;

 
    % springs animation
    xa1 = 0.5; ya1 = 0; xb1 = Xx(1,1)+y1; yb1 = 0; ne1 = 4; a1 = 0.5; ro1 = 0.08;
    [xs1,ys1] = spring(xa1,ya1,xb1,yb1,ne1,a1,ro1); plot(xs1,ys1,'LineWidth',2)
    
    xa2 = Xx(1,1)+(y1+0.5); ya2 = 0; xb2 = Xx(3,1)+(y2); yb2 = 0; ne2 = 4; a2 = 0.5; ro2 = 0.08;
    [xs2,ys2] = spring(xa2,ya2,xb2,yb2,ne2,a2,ro2); plot(xs2,ys2,'LineWidth',2)
     
    xa3 = Xx(3,1)+(y2)+0.5 ; ya3 = 0; xb3 = Xx(5,1)+y3; yb3 = 0; ne3 = 4; a3 = 0.5; ro3 = 0.08;
    [xs3,ys3] = spring(xa3,ya3,xb3,yb3,ne3,a3,ro3); plot(xs3,ys3,'LineWidth',2)
    
    xa4 = Xx(5,1)+(y3)+0.5 ; ya4 = 0; xb4 = y4; yb4 = 0; ne4 = 4; a4 = 0.5; ro4 = 0.08;
    [xs4,ys4] = spring(xa4,ya4,xb4,yb4,ne4,a4,ro4); plot(xs4,ys4,'LineWidth',2)  
    
    % mases animation
     rectangle('Position',[Xx(1,1)+y1,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(3,1)+y2,-0.25, 0.5, 0.5],'FaceColor','r')
     rectangle('Position',[Xx(5,1)+y3,-0.25, 0.5, 0.5],'FaceColor','r')
     
     
     % position plot
     axes(handles.axes1)
     plot(t, Xx(4,1)+y1, '.', t, Xx(3,1)+y1,'.', t, Xx(5,1)+y1,'.'), hold('on')
     
     % speed plot
     axes(handles.axes2)
     
     plot(t, Xx(2,1)+y1, '.', t, Xx(4,1)+y1,'.', t, Xx(6,1)+y1,'.'), hold('on')
     
     set(handles.x1c,'String', sprintf('%.2f',Xx(1,1)));
     set(handles.x2c,'String', sprintf('%.2f',Xx(3,1)));
     set(handles.x3c,'String', sprintf('%.2f',Xx(5,1)));

     set(handles.v1c,'String', sprintf('%.2f',Xx(2,1)));
     set(handles.v2c,'String', sprintf('%.2f',Xx(4,1)));
     set(handles.v3c,'String', sprintf('%.2f',Xx(6,1)));

function timerCallback(obj, event, handles)
%--------------------------------------------------------------------------
% This function triggers calculation and drawing function.
% 
%  
%--------------------------------------------------------------------------
% Declare time
global t;

    % Update ui controls

    x1n=str2num(get(handles.x1,'String'));
    x2n=str2num(get(handles.x2,'String'));
    x3n=str2num(get(handles.x3,'String'));

    v01n=str2num(get(handles.v01,'String'));
    v02n=str2num(get(handles.v02,'String'));
    v03n=str2num(get(handles.v03,'String'));

    m1n=str2num(get(handles.m1,'String'));
    m2n=str2num(get(handles.m2,'String'));
    m3n=str2num(get(handles.m3,'String'));

    k1n=str2num(get(handles.k1,'String'));
    k2n=str2num(get(handles.k2,'String'));
    k3n=str2num(get(handles.k3,'String'));
    k4n=str2num(get(handles.k4,'String'));

    x0=[x1n v01n x2n v02n x3n v03n]';

    K1=[0 1 0 0 0 0; -((k1n+k2n)/m1n) 0 (k2n/m1n) 0 0 0; 
        0 0 0 1 0 0; (k2n/m2n) 0 -((k2n+k3n)/m2n) 0 k3n/m2n 0; 
        0 0 0 0 0 1; 0 0 k3n/m3n 0 -((k3n+k4n)/m3n) 0];


    y1=1;
    y2=2;
    y3=3;
    y4=4;

% Calculate current state with respect to 't'
 Xx=calcExp(K1,x0,t);

% Draw current state

 axes(handles.animation)
 generateGraphics(handles,Xx, y1, y2, y3, y4)

% Increment time
t = t + 0.05;


function Xx = calcExp(K1, x0, t) 
%--------------------------------------------------------------------------
% This function calculates matrix exponential on a given input matrix,
% initial conditions, and time vector
%  
%--------------------------------------------------------------------------
Xx=zeros(6,1);
% system solutions via matrix exponential
X=expm(K1*t)*x0;
Xx(:,1)=X;

function [xs ys] = spring(xa,ya,xb,yb,varargin)
% SPRING         Calculates the position of a 2D spring
%    [XS YS] = SPRING(XA,YA,XB,YB,NE,A,R0) calculates the position of
%    points XS, YS of a spring with ends in (XA,YA) and (XB,YB), number
%    of coils equal to NE, natural length A, and natural radius R0. 
%    Useful for mass-spring oscillation animations.
% USAGE: in a first call in your code, call it with the full parameters.
% Then, only you have to give it the coordinates of the ends.
% EXAMPLE:
% xa = 0; ya = 0; xb = 2; yb = 2; ne = 10; a = 1; ro = 0.1;
% [xs,ys] = spring(xa,ya,xb,yb,ne,a,ro); plot(xs,ys,'LineWidth',2)
%...
% [xs,ys]=spring(xa,ya,xb,yb); plot(xs,ys,'LineWidth',2)
%
%   Made by:            Gustavo Morales   UC  08-17-09 gmorales@uc.edu.ve
%
persistent ne Li_2 ei b
if nargin > 4 % calculating some fixed spring parameters only once time
    [ne a r0] = varargin{1:3};                  % ne: number of coils - a = natural length - r0 = natural radius
    Li_2 =  (a/(4*ne))^2 + r0^2;                % (large of a quarter of coil)^2
    ei = 0:(2*ne+1);                            % vector of longitudinal positions
    j = 0:2*ne-1; b = [0 (-ones(1,2*ne)).^j 0]; % vector of transversal positions
end
R = [xb yb] - [xa ya]; mod_R = norm(R); % relative position between "end_B" and "end_A"
L_2 = (mod_R/(4*ne))^2; % (actual longitudinal extensin of a coil )^2
if L_2 > Li_2
   error('Spring:TooEnlargement', ...
   'Initial conditions cause pulling the spring beyond its maximum large. \n Try reducing these conditions.')
else
    r = sqrt(Li_2 - L_2);   %actual radius
end
c = r*b;    % vector of transversal positions
u1 = R/mod_R; u2 = [-u1(2) u1(1)]; % unitary longitudinal and transversal vectors 
xs = xa + u1(1)*(mod_R/(2*ne+1)).*ei + u2(1)*c; % horizontal coordinates
ys = ya + u1(2)*(mod_R/(2*ne+1)).*ei + u2(2)*c; % vertical coordinates

Contact us