clear;
clg;
g=9.81;

s10=input('Input Ball initial position: ');
v10=input('Input Ball initial velocity: ');

x1=s10;
x2=v10;

TL=0.5;
TM=0.4;
TS=0.2;

%/*    Membership    */
if (x1<-0.1)
        u1N=1;              %//x1:N
        u1Z=0;              %//x1:Z
        u1P=0;              %//x1:P
elseif(x1>=-0.1 &  x1<0)
        u1N=-x1/0.1;
        u1Z=(x1+0.1)/0.1;
        u1P=0;
elseif(x1>=0 & x1<=0.1)
        u1N=0;
        u1Z=-(x1-0.1)/0.1;
        u1P=x1/0.1;
elseif (x1>0.1)
        u1N=0;
        u1Z=0;
        u1P=1;
end

if (x2<-0.1)
        u2N=1;              %//x2:N
        u2Z=0;              %//x2:Z
        u2P=0;              %//x2:P
elseif(x2>=-0.1 & x2<0)
        u2N=-x2/0.1;
        u2Z=(x2+0.1)/0.1;
        u2P=0;
elseif(x2>=0 & x2<=0.1)
        u2N=0;
        u2Z=-(x2-0.1)/0.1;
        u2P=x2/0.1;
elseif (x2>0.1)
        u2N=0;
        u2Z=0;
        u2P=1;
end


mA1=min(u1N,u2N);  % //Rule1:  x1:N   x2:N
mA2=min(u1N,u2Z);  % //Rule2:  x1:N   x2:Z
mA3=min(u1N,u2P);  % //Rule3:  x1:N   x2:P
mA4=min(u1Z,u2N);  % //Rule4:  x1:Z   x2:N
mA5=min(u1Z,u2Z);  % //Rule5:  x1:Z   x2:Z
mA6=min(u1Z,u2P);  % //Rule6:  x1:Z   x2:P
mA7=min(u1P,u2N);  % //Rule7:  x1:P   x2:N
mA8=min(u1P,u2Z);  % //Rule8:  x1:P   x2:Z
mA9=min(u1P,u2P);  % //Rule9:  x1:P   x2:P

y1=TL;
y2=(TL+TM)/2;
y3=TM;
y4=(TM+TS)/2;
y5=TS;
y6=(TM+TS)/2;
y7=TM;
y8=(TL+TM)/2;
y9=TL;

A=mA1+mA2+mA3+mA4+mA5+mA6+mA7+mA8+mA9;
Treg=(mA1*y1+mA2*y2+mA3*y3+mA4*y4+mA5*y5+mA6*y6+mA7*y7+mA8*y8+mA9*y9)/A;

T1=Treg
T2=T1

w1=2*pi/T1;
w2=2*pi/T2;
theta1M=-28/(5*g)/(T1*(T1+T2))*(s10+(T1+T2/2)*v10)
theta2M= 28/(5*g)/(T2*(T1+T2))*(s10+( T1/2  )*v10)

%
t1=0:.002:T1;
theta1=theta1M/2*( 1-cos(w1*t1) );
a1=5/7*g*sin(theta1);
v1=v10+5/14*g*theta1M*( t1-1/w1*sin(w1*t1) );
s1=s10+v10*t1+5/14*g*theta1M*( t1.^2/2+1/w1^2*(cos(w1*t1)-1) );

%
v20=v1(length(t1));
s20=s1(length(t1));
t2=T1:.002:(T1+T2);
theta2=theta2M/2*( 1-cos(w2*(t2-T1)) );
a2=5/7*g*sin(theta2);
v2=v20+5/14*g*theta2M*( (t2-T1)-1/w2*sin(w2*(t2-T1)) );
s2=s20+v20*(t2-T1)+5/14*g*theta2M*( (t2-T1).^2/2+1/w2^2*(cos(w2*(t2-T1))-1) );

t=[t1 t2];
theta=[theta1 theta2];
a=[a1 a2];
v=[v1 v2];
s=[s1 s2];

plot(t,theta,'-',t,a,'--',t,v,'-.',t,s,'.');
xlabel('Time (sec)');
ylabel('Theta(rad): -  a(m/s^2): --  v(m/s): -.  s(m): .');
%meta ball
