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



%%%%%%%%%%%%%%% LJUSTES BÖJNING OCH INTERFERENS, HEMUPPGIFT 4 %%%%%%%%%%%%%
%%%                           Calle Preger
%%%                      calle.preger@ftf.lth.se


%%%  Här behöver ni inte ändra något, det enda ni behöver göra är att köra
%%%  programmet genom att trycka på "editor" och sedan den gröna triangeln
%%%  "Run".


%%% Viktigt är att ni är inne i rätt mapp. Ni måste se både
%%% Hemuppgift4.m och Hemuppgift4.fig till vänster i "current folder"
%%% annars går programmet inte att köra

%%% Därefter följer ni uppgiftsbeskrivningen i labhandledningen
%%%
%%%


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

% Last Modified by GUIDE v2.5 16-Nov-2016 12:11:16

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Hemuppgift4_OpeningFcn, ...
                   'gui_OutputFcn',  @Hemuppgift4_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

function [theta, y1, I, int_fak, dif_fak] = intensity(N,d,b,lambda)
I_0 =1;
I=[];
theta = linspace(-pi/300,pi/300,200);
alpha = pi/lambda*d.*sin(theta);
beta = pi*b/lambda.*sin(theta);
int_fak = I_0*(sin(N.*alpha)./N./sin(alpha)).^2;
dif_fak = max(int_fak).*((sin(beta)./beta).^2);
I_p=I_0*(sin(beta)./beta).^2.*(sin(N.*alpha)./N./sin(alpha)).^2;
y = linspace(0,max(dif_fak),200);
y1=y';
for i = 1:200
    I(i,:) = I_0*((sin(beta)./beta).^2).*((sin(N.*alpha)/N./sin(alpha)).^2);
end

function makeplot(theta,y,I,int_fak,dif_fak,handles)
reset(gcf)
reset(gca)
y1=y';
contourf(handles.axes1,theta, y1, I,15, 'LineWidth',0.1,'LineStyle', 'none')
set(gcf, 'colormap', gray)
colorbar;
caxis([0 max(y)]/3)
hold on
plot(handles.axes1,theta,int_fak.*dif_fak,'r')
hold on
plot(handles.axes1,theta,dif_fak,'b')
set(gca,'Visible','off')



% --- Executes just before Hemuppgift4 is made visible.
function Hemuppgift4_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 Hemuppgift4 (see VARARGIN)

% handles.N = 2;
% handles.d = 10;
% handles.b = 100;
% handles.lambda = 0.4;
% set(handles.edit1,'String',num2str(2));
% set(handles.slider1,'Value',2);
% set(handles.slider2,'Value',10);
% set(handles.slider3,'Value',100);
% set(handles.slider4,'Value',0.4);
% intensity(handles.N, handles.d, handles.b, handles.lambda, handles)

reset(gca)
N =5;
b = 40;
d = 100;
lambda = 0.4;
I_0 =1;
I=[];
theta = linspace(-pi/300,pi/300,200);
alpha = pi/lambda*d.*sin(theta);
beta = pi*b/lambda.*sin(theta);
int_fak = I_0*(sin(N.*alpha)/N./sin(alpha)).^2;    %% den röda
dif_fak =(sin(beta)./beta).^2;        %% dne blåa
y = linspace(0,max(int_fak),200);
for i = 1:200
    I(i,:) = I_0*(sin(beta)./beta).^2.*(sin(N.*alpha)/N./sin(alpha)).^2;
end

y1=y';
contourf(theta, y1, I,15, 'LineWidth',0.1,'LineStyle', 'none')
set(gcf, 'colormap', gray)
colorbar;
caxis([0 max(y1)/20])
hold on
plot(theta,int_fak.*dif_fak,'r')
hold on
plot(theta,max(int_fak)*dif_fak,'b')
set(gca,'Visible','off')
set(handles.slider1,'Min',1);
set(handles.slider2,'Min',0);
set(handles.slider3,'Min',0);
set(handles.slider4,'Min',100);
set(handles.slider1,'Max',20);
set(handles.slider2,'Max',300);
set(handles.slider3,'Max',100);
set(handles.slider4,'Max',1000);
set(handles.slider1,'SliderStep',[1/19,2/19] );
set(handles.slider2,'SliderStep',[1/300,1/30]);
set(handles.slider3,'SliderStep',[1/100,1/10]);
set(handles.slider4,'SliderStep',[1/900,2/900]);
set(handles.edit1,'String',num2str(N));
set(handles.edit2,'String',num2str(d));
set(handles.edit3,'String',num2str(b));
set(handles.edit4,'String',num2str(lambda*1000));
set(handles.slider1,'Value',N);
set(handles.slider2,'Value',d);
set(handles.slider3,'Value',b);
set(handles.slider4,'Value',lambda*1000);


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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = Hemuppgift4_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 slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%% Number of slits
N = get(handles.slider1,'Value');
if rem(N,1) ~= 0
    N=N-rem(N,1);
end
d = str2double(get(handles.edit2,'String'));
b = str2double(get(handles.edit3,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
set(handles.edit1,'String',num2str(N));
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)
% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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 slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


d = get(handles.slider2,'Value');
if rem(d,1) ~= 0
    d=d-rem(d,1);
end
N = str2double(get(handles.edit1,'String'));
b = str2double(get(handles.edit3,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
if (d<=b+7)
    set(handles.edit2,'String',num2str(d));
    set(handles.edit3,'String',num2str(d-7));
else
    set(handles.edit2,'String',num2str(d));
end
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)

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


% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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 slider3_Callback(hObject, eventdata, handles)
% hObject    handle to slider3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

b = get(handles.slider3,'Value');
if rem(b,1) ~= 0
    b=b-rem(b,1);
end
N = str2double(get(handles.edit1,'String'));
d = str2double(get(handles.edit2,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
if (d<=b+7)
    set(handles.edit2,'String',num2str(b+7));
    set(handles.edit3,'String',num2str(b));
else
    set(handles.edit3,'String',num2str(b));
end
set(handles.edit3,'String',num2str(b));
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)

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


% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (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 slider4_Callback(hObject, eventdata, handles)
% hObject    handle to slider4 (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

lambda = get(handles.slider4,'Value')/1000;
if rem(lambda,0.001) ~= 0
    lambda=lambda-rem(lambda,0.001);
end
N = str2double(get(handles.edit1,'String'));
d = str2double(get(handles.edit2,'String'));
b = str2double(get(handles.edit3,'String'));
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)
set(handles.edit4,'String',num2str(lambda*1000));

% --- Executes during object creation, after setting all properties.
function slider4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider4 (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 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

N = str2double(get(handles.edit1,'String'));
if rem(N,1) ~= 0
    N=N-rem(N,1);
    set(handles.edit1,'String',num2str(N));
end
d = str2double(get(handles.edit2,'String'));
b = str2double(get(handles.edit3,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
set(handles.slider1,'Value',N);
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)

% --- 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


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

N = str2double(get(handles.edit1,'String'));
d = str2double(get(handles.edit2,'String'));
if rem(d,1) ~= 0
    d=d-rem(d,1);
    set(handles.edit2,'String',num2str(d));
end
b = str2double(get(handles.edit3,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
set(handles.slider2,'Value',d);
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)

% --- 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

N = str2double(get(handles.edit1,'String'));
d = str2double(get(handles.edit2,'String'));
b = str2double(get(handles.edit3,'String'));
if rem(b,1) ~= 0
    b=b-rem(b,1);
    set(handles.edit3,'String',num2str(b));
end
lambda = str2double(get(handles.edit4,'String'))/1000;
set(handles.slider3,'Value',b);
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)


% --- 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

N = str2double(get(handles.edit1,'String'));
d = str2double(get(handles.edit2,'String'));
b = str2double(get(handles.edit3,'String'));
lambda = str2double(get(handles.edit4,'String'))/1000;
if rem(lambda,0.001) ~= 0
    lambda=lambda-rem(lambda,0.001);
    set(handles.edit4,'String',num2str(lambda)*1000);
end
set(handles.slider4,'Value',lambda*1000);
[theta, y, I, int_fak, dif_fak]=intensity(N,d,b,lambda);
makeplot(theta, y, I, int_fak, dif_fak,handles)

% --- 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
