You are on page 1of 13

TUTORIAL QUESTIONS ON FUZZY LOGIC

1. A triangular member function shown in figure is to be made in MATLAB.


Write a function.
Answer:
CODE
function y = trimf(x, parameter)
a = parameter(1);
b = parameter(2);
c = parameter(3);
y = max(min((x-a)/(b-a), (c-x)/(c-b)), 0);

COMMAND PROMPT
>> x = 0:100;
>> mf = trimf(x,[20,60,80]);
>> plot(x,mf)

RESULTING GRAPH
2. Create a Gaussian membership function with centre as 40 and spread 10 (x
varies from 0 to 100) and name it as fuzzy set A

CODE
function y = gauss_memf(x, parameter)
c = parameter(1);
s = parameter(2);
t = (x-c)/s;
y = exp(-t.*t/2);

>> x = 0:100;
>> A = gauss_memf(x,[40, 10]);
>> plot(x,A)

Create another Gaussian membership function with centre as 60 and spread 10 (x


varies from 0 to 100) and name it as fuzzy set B.
>> B = gauss_memf(x,[60, 10]);
>> plot(x,B)
Then plot complement of A, Union of A and B and Intersection of A and B

Complement
>> complementA = 1-A;
>> plot(x,complementA)

Union
>> Union = max(A,B);
>> plot(x,Union)
Intersection
>> Intersection = min(A,B);
>> plot(x, Intersection)

3. Plot in MATLAB a trapezoidal membership function shown below

CODE
function y = trapezoidal_memf(x, parameter)
a = parameter(1);
b = parameter(2);
c = parameter(3);
d = parameter(4);
y = max(min(min((x-a)/(b-a), (d-x)/(d-c)), 1), 0);
4. Design a fuzzy logic controller for the above system.

1
𝐺(𝑠) =
𝑠 2 + 3𝑠 + 2
i. Without fuzzy controller

𝑠+2
𝑃𝐼 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
𝑠

Unit Step response


ii. With fuzzy controller
Unit step response
SIMULATION ASSIGNMENT
1. Choose your own function approximation problem or a classification problem
from UCI Machine Learning Repository and conduct the experiments using:
i. ELM

Here, I have taken Skin Segmentation dataset from UCI repository.


CODE
T = readtable('skinsegment.csv');
array = table2array(T);
trndata1 = array(1:100,:);
trndata2 = array(50861:50960,:);
trndata = [trndata1; trndata2];
testdata1 = array(101:122,:);
testdata2 = array(50962:50984,:);
testdata = [testdata1; testdata2];
% x= array(:,1:4);
% y = array(:,4);
% [tr,ts] = dividerand(length(y),0.8,0.2);
xtrain = trndata(:,1:4);
ytrain = trndata(:,4);
xtest = testdata(:,1:4);
ytest = testdata(:,4);
xtrain = (xtrain - mean(xtrain))./std(xtrain);
mdl = extreme_learning_machine_classifier(xtrain, ytrain);

y = mdl.predict(xtest);
fprintf("ELM model accuracy: %.2f%%\n",...
100*sum(y==ytest)/length(ytest));

ELM CODE
classdef extreme_learning_machine_classifier
%EXTREME_LEARNING_MACHINE_CLASSIFIER Builds a classification
extreme
%learning machine.

properties
Win = [];
activation = {};
Wout = [];
functional = 0;
end

methods
function obj =
extreme_learning_machine_classifier(X,y,varargin)
%EXTREME_LEARNING_MACHINE_CLASSIFIER Trains an ELM for
%classification.
% Inputs:
% X - Input data
% y - output data
% hidden - the number of hidden units (default:
2*size(X,2))
% activation - activation function (default:
'sigmoid')
% other values: 'tanh', 'relu', 'rbf',
'linear'
% functional - functional link between input and
output
% (default: 0 [false]) other values: 1
[true]
% Output:
% obj - Trained ELM object

p = inputParser;

addRequired(p, 'X', @ismatrix);


addRequired(p, 'y', @ismatrix);
addParameter(p, 'hidden', 2*size(X,2), @isnumeric);
addParameter(p, 'activation', 'sigmoid',
@(x)any(validatestring(x,{'sigmoid','tanh','rbf','relu'})));
addParameter(p, 'functional', 0, @isnumeric);

parse(p,1,1,varargin{:});

obj.functional = p.Results.functional;

switch p.Results.activation
case 'sigmoid'
obj.activation = @(x)(1 ./ (1 + exp(-x)));
case 'tanh'
obj.activation = @(x)(tanh(x));
case 'relu'
obj.activation = @(x)(max(0,x));
case 'rbf'
obj.activation = @(x)(radbas(x));
case 'linear'
obj.activation = @(x)(x);
end

hidden = p.Results.hidden;

obj.Win = rand(size(X,2)+1, hidden) * 2 - 1;


H = obj.activation([X, ones(size(X,1),1)] * obj.Win);
if obj.functional; H = [H,X]; end
obj.Wout = pinv([H, ones(size(H,1),1)])* y;
end

function [y, p] = predict(obj, X)


%PREDICT Predicts the output of the trained model for
new input
%data
% Inputs:
% obj - trained ELM
% X - Input data

% Output:
% y - Binary output
% p - Numeric output (~probability)
H = obj.activation([X, ones(size(X,1),1)] * obj.Win);
if obj.functional; H = [H, X]; end
p = [H, ones(size(H,1),1)] * obj.Wout;
y = p >= 0.5;
end
end
end

RESULT
>> elmone
ELM model accuracy: 48.89%

ii. ANFIS
2. Time series prediction using neural networks (Prediction of solar activity). Use
the data set solar_dataset
CODE:
load solar_dataset
solarTargets = cell2mat(solarTargets);
solarTargets = (solarTargets -
mean(solarTargets))./std(solarTargets);
output = zeros(2893,1);
input = zeros(2893,6);
for i = 7:1:length(solarTargets)
output(i-6,1)=solarTargets(1,i);
end
for n = 7:1:length(solarTargets)
i = n-6;
k = 6;
for j = 1:1:6
input(i,j)=solarTargets(1,n-k);
k = k-1;
end
end
len = length(output);
trn = round(len*0.8);
tst = trn+1;
xtrain = transpose(input(1:trn,:));
ytrain = transpose(output(1:trn));
xtst = transpose(input(tst:len,:));
ytst = transpose(output(tst:len));

numFeatures = 6;
numResponses = 1;
numHiddenUnits = 200;

layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];

options = trainingOptions('adam', ...


'MaxEpochs',250, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');

net = trainNetwork(xtrain,ytrain,layers,options);

net = predictAndUpdateState(net,xtrain);
[net,ypred] = predictAndUpdateState(net,xtst);

mu = mean(solarTargets);
sig = std(solarTargets);

%solarTargetStandardized = (solarTargets - mu) / sig;

yforecast = cat(2,ytrain,ypred);
y0 = cat(2,ytrain,ytst);

y0 = y0*sig+mu;
yforecast = yforecast*sig+mu;

p1 = 1:trn;
p2 = tst:len;

subplot(2,1,1)
plot(1:len,y0)
title('Observed values')

ytrain = ytrain*sig+mu;
ypred = ypred*sig+mu;
ytst = ytst*sig+mu;

subplot(2,1,2)
plot(p1,ytrain,p2,ypred)
title('Forecast');

plot(p2,ypred, p2, ytst)


legend('Forecasted values','Observed values')

OUTPUT FIGURES

You might also like