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

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

% Last Modified by GUIDE v2.5 21-Feb-2003 11:55:36

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @Reliability_OpeningFcn, ...
    'gui_OutputFcn',  @Reliability_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 Reliability is made visible.
function Reliability_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 Reliability (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Reliability_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;



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)

% 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)
% 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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% 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)
global LDCload tt
LL=LDCload';
n=1:tt;
% Coef=polyfit(LDCload,n,4);
% fit=Coef(1)*x.^4+Coef(2)*x.^3+Coef(3)*x.^2+Coef(4)*x+Coef(5);
% f=polyval(Coef,LDCload);
plot(LL,n./8736,'r')
legend('LDC Curve')
grid on

% figure(2)
% plot(n,LDCload,'k-')
% grid on


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
global LDCload tt
clear LDC
yload=[
    1 86.2
    2 90.0
    3 87.8
    4 83.4
    5 88.0
    6 84.1
    7 83.2
    8 80.6
    9 74.0
    10 73.7
    11 71.5
    12 72.7
    13 70.4
    14 75.0
    15 72.1
    16 80.0
    17 75.4
    18 83.7
    19 87.0
    20 88.0
    21 85.6
    22 81.1
    23 90.0
    24 88.7
    25 89.6
    26 86.1
    27 75.5
    28 81.6
    29 80.1
    30 88.0
    31 72.2
    32 77.6
    33 80.0
    34 72.9
    35 72.6
    36 70.5
    37 78.0
    38 69.5
    39 72.4
    40 72.4
    41 74.3
    42 74.4
    43 80.0
    44 88.1
    45 88.5
    46 90.9
    47 94.0
    48 89.0
    49 94.2
    50 97.0
    51 100.0
    52 95.2
    ];
wload=[
    1 93
    2 100
    3 98
    4 96
    5 94
    6 77
    7 75
    ];
dload=[
    1 67
    2 63
    3 60
    4 59
    5 59
    6 60
    7 74
    8 86
    9 95
    10 96
    11 96
    12 95
    13 95
    14 95
    15 93
    16 94
    17 99
    18 100
    19 100
    20 96
    21 91
    22 83
    23 73
    24 63
    ];
ny=size(yload,1);
nw=size(wload,1);
nd=size(dload,1);
yload(:,2)=yload(:,2)/100;
wload(:,2)=wload(:,2)/100;
dload(:,2)=dload(:,2)/100;
t=0;
for i=1:ny(1)
    for j=1:nw(1)
        for k=1:nd(1)
            t=t+1;
            LDCload(t,1)=yload(i,2)*wload(j,2)*dload(k,2);
        end
    end
end
tt=t;
Peak=str2num(get(handles.edit1,'string'));
LDCload=sortrows(LDCload*Peak,-1);


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% 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)
global LOLP LOLE EENS Peak tt
format long g
% Peak=
t=tt;
%   MW   N   FOR
G=[ 12	 5	0.02
    20	 4	0.10
    50	 6	0.01
    76	 4	0.02
    100	 3	0.04
    155	 4	0.04
    197	 3	0.05
    350  1	0.08
    400	 2	0.12
    ];

set(handles.uitable1,'data',G);
Nt=size(G,1); % Total number of Power Generations
for i=1:Nt
    PG(i)=G(i,1);
    N(i)=G(i,2);
    FOR(i)=G(i,3);
end
% PG(in) and Prob(indv.) and Prob(cum.)
for i=1:Nt
    for x=1:(N(i)+1)
        PGin(x,i)=PG(i)*(N(i)-x+1);
        Ind_Prob(x,i)=binopdf((N(i)-x+1),N(i),(1-FOR(i))); % Individual Probability
        Cum_Prob(x,i)=binocdf((N(i)-x+1),N(i),(1-FOR(i))); % Cumulative Probability
    end
end
% Table
for i=1:Nt
    Table(:,2*i-1)=PGin(:,i);
    Table(:,2*i)=Ind_Prob(:,i);
end
% COPT
T=Table(1:N(1)+1,1:2);
for i=2:Nt
    T=mix(T,Table(1:N(i)+1,2*i-1:2*i));
end
COPT=T;
n=size(COPT,1);
COPT(:,3)=zeros(n,1);
% COPT(1,3)=1;
% for i=1:n-1
%     COPT(i+1,3)=COPT(i,3)-COPT(i,2);
% end
for i=1:n
    COPT(i,3)=sum(COPT(i:n,2));
end
COPT;
% LOLP and LOLE
LOLP=0;
COPT(1,1)
Peak=str2num(get(handles.edit1,'string'));
Reserve=COPT(1,1)-Peak
for r=1:n
    Outage=COPT(1,1)-COPT(r,1);
    if Outage>=Reserve
        LOLP=COPT(r,3);
        LOLE=LOLP*t;
        break
    end
end


% ?
LOLP=LOLP/100
LOLE=LOLE/100
% EENS
EENS=0; % Surface under Load Curve
for r=1:n
    Outage=COPT(1,1)-COPT(r,1);
    if Outage>=Reserve
        EENS=EENS+(Outage-Reserve)*COPT(r,2);
    end
end
EENS=EENS*t;

%?
EENS=EENS/100


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


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



function edit4_Callback(hObject, eventdata, 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 edit4_CreateFcn(hObject, eventdata, handles)
% 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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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


% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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 pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% 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)
global LOLP LOLE EENS
LOLP
LOLE
EENS
set(handles.edit2,'string',num2str(LOLP));
set(handles.edit3,'string',num2str(LOLE));
set(handles.edit4,'string',num2str(EENS));


function mixed=mix(u,v)
format long g
k=1;
for i=1:size(u,1)
    for j=1:size(v,1)
        mixed(k,1)=u(i,1)+v(j,1);
        mixed(k,2)=u(i,2)*v(j,2);
        k=k+1;
    end
end
mixed=sortrows(mixed,-1);
mixed=sortlike(mixed);



function [s,k]=sortlike(x)
n=size(x,1);
k=1;
i=1;
j=1;
if i==n
    s=x;
end
while i<n
    if x(i,1)==x((i+j),1)
        x(i,2)=x(i,2)+x((i+j),2);
        if (i+j)==n
            s(k,1:2)=[x(i,1) x(i,2)];
            break
        end
        j=j+1;
    else
        s(k,1:2)=[x(i,1) x(i,2)];
        i=i+j;
        if i==n
            k=k+1;
            s(k,1:2)=[x(i,1) x(i,2)];
            break
        end
        j=1;
        k=k+1;
    end
end



