Professional Documents
Culture Documents
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)
Complement
>> complementA = 1-A;
>> plot(x,complementA)
Union
>> Union = max(A,B);
>> plot(x,Union)
Intersection
>> Intersection = min(A,B);
>> plot(x, Intersection)
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
𝑃𝐼 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
𝑠
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;
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;
% 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];
net = trainNetwork(xtrain,ytrain,layers,options);
net = predictAndUpdateState(net,xtrain);
[net,ypred] = predictAndUpdateState(net,xtst);
mu = mean(solarTargets);
sig = std(solarTargets);
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');
OUTPUT FIGURES