Sparse Extreme Learning Machine - matlab

I tried to develop code for sparse ELM proposed by following paper. I used random hidden neurone kernel. and sigmoid activation function.
But the code is not performing well in terms of time. It is taking much more than expected time to get trained .
I think there is some problem in updating d
Paper :- http://www.ntu.edu.sg/home/egbhuang/pdf/Sparse-ELM-IEEE-T-Cybernetics.pdf
Here is my code implementation :-
function [model]=selm2(tr,tr_c,L,seed,C)
[N, In] = size(tr);
[tempH, W_ih, BiasofHiddenNeurons]=Selm_Hiddenoutput(tr,L, 'sigmoid',seed);
H=[tempH ones(N,1)];
delta = H*H';
% f=funcpredict(alpha,tr_c,tr,delta);
G=(tr_c*tr_c').*delta;
%%%%%%%%%%%%%%%%%%%%% algo starts %%%%%%%%%%%%%%%%%%%%%
alpha=zeros(N,1);
g=G*alpha-1;
J=g;
d=ones(N,1);
while min(J(:))<-(10^-3)
J=g.*d;
[mini ind]=min(J);
% temp=G*alpha;
alpha(ind)=alpha(ind) + ((-g(ind))/delta(ind,ind));
if alpha(ind)<0
alpha(ind)=0;
elseif alpha(ind)>C
alpha(ind)=C;
end
g=G*alpha-1;
d=-sign(g);
d(alpha==C)=-1;
d(alpha==0)=1;
% % for i=1:N
% if alpha(ind)==0
% d(ind)=1;
% elseif alpha(ind)==C
% d(ind)=-1;
% % else
% % d(ind)= -sign(g(i));
% end
% end
end
support= (alpha~=0);
supportvec=[tr(support,:) tr_c(support)];
model.wih=W_ih;
model.bias=BiasofHiddenNeurons;
model.alpha=alpha(support);
model.vectors=supportvec;

Related

Obtaining different results when using parfor in matlab

I am trying to parallelize the code below in matlab:
%reduce the size of A if necessary.
A=rand(100,60);
B=int8(rand(size(A,1),size(A,2))>0.5);
N=10;
G=15;
lambda_tol_vector= zeros(G,1);
conto = 1;
for h=-G:0.1:G
lambda_tol_vector(conto)=2^(h);
conto = conto+1;
end
M=4;
tol = 1e-9;
CompletedMat_nopar1={};
tic
for RIPETIZIONE = 1:10
%using normal for loop CompletedMat is saved
for k = 1:size(lambda_tol_vector,1)
%fprintf('Completion using nuclear norm regularization... \n');
%sequential code:
[CompletedMat34,flag] = matrix_completion_nuclear_GG_vec(A.*double(B),double(B),N,lambda_tol_vector(k),tol);
%parallel code thread-based:
if flag==1
CompletedMat_nopar1{RIPETIZIONE,k}=zeros(size(A));
end
CompletedMat_nopar1{RIPETIZIONE,k}=CompletedMat34;
end
end
toc
After having vectorized whereby possible I ended up with the following code:
A = parallel.pool.Constant(A);
lambda_tol_vector=parallel.pool.Constant(lambda_tol_vector);
R=10;
K=size(lambda_tol_vector.Value,1);
CompletedMat_parvec=cell(R,K);
idx=cellfun('isempty', CompletedMat_parvec);
CompletedMat_parvec(idx)={zeros(size(A.Value))};
B=double(B);
tic
parfor n=1:R*K
[~,k]=ind2sub([R,K],n);
%fprintf('Completion using nuclear norm regularization... \n');
[CompletedMat,flag] = matrix_completion_nuclear_GG_vec( ...
A.Value.*B,B, N, lambda_tol_vector.Value(k), tol);
if flag~=0
CompletedMat_parvec{n}=CompletedMat;
end
end
toc
The problem here is that CompletedMat_parvec and CompletedMat_nopar1 provide totally different results while they are expected to give the same result.
How is this possible?
For the sake of completeness I will provide matrix_completion_nuclear_GG_vec below:
%%
% We need a functional environment so that the MAtimesVec subfunction can
% access variables in workspace
function [Anew,objective,flag,iteration] = matrix_completion_nuclear_GG_vec(A,B,N,lambda_tol,tol)
%%
%This code ttries to vectorize and make more efficient the code in
%matrix_completion_nuclear_GG_alt
%% Singular value thresholding for structured (sparse + low rank) matrix
%clear;
%tol=10^-7;
%lambda_tol=0.5;
%%
% Read in sparse matrices downloaded from The University of Florida Sparse
% Matrix Collection
%data = load('mhd4800b.mat');
%A = data.mhd4800b;
%A=A(1:100,1:100);
%A=rand(100,5)*rand(5,100);
%[sA,vA,dA]=svd(A)
%%
% initialization
%B = rand(size(A))<0.3; % remove 10% of the entries
mat=A.*B;
m = size(mat,1);
n = size(mat,2);
L = zeros(m,1);
R = zeros(n,1);
iteration=0;
criterion=0;
flag=0;
Anew=L*R';
objective=zeros();
while (criterion==0 && iteration<N && flag==0)
Aold=Anew;
iteration=iteration+1;
%iteration
%%
% Generation of structured matrix (sparse plus low rank)
%LR = L*R'; % generation of low rank matrix
%Amat = mat + LR; % sparse + low rank
%%
% Find all singular values >= lambda_tol by svt (deflation method). Function
% MAtimesVec is defined at end of this file.
%tic;
%[u,s,v] = svt(#MAtimesVec,'m',m,'n',n,'lambda',lambda_tol);
[u,s,v] = svd(mat+Anew);
%toc;
%display(size(s));
if size(s,1)==0
Anew=NaN*A;
objective=NaN;
flag=1;
return
elseif max(max(s))<=lambda_tol
Anew=NaN*A;
objective=NaN;
flag=1;
return
else
for k=1:min(size(s,1),size(s,2))
if s(k,k)<lambda_tol
s(k,k)=0;
else
s(k,k)=s(k,k)-lambda_tol;
end
end
%size(s,1)
%s=s-lambda_tol*eye(size(s,1));
L=u*s;
R=v;
mat=(A-u*s*v').*B;
Anew=L*R';
objective(iteration)=1/2*(norm(mat,'Fro'))^2+lambda_tol*(sum(diag(s)));
%objective(iteration)
if (norm(Anew-Aold,'Fro')^2/(norm(Aold,'Fro')^2))<tol
criterion=1;
end
%norm(A-Anew,'Fro')
end
end
if flag==1
Anew=[];
objective=[];
end
%%
% Subfunction for exploiting matrix structure of sparse plus low rank
%function MAvec = MAtimesVec(vec, trans)
% if trans
% MAvec = (vec'*mat)' + R*(vec'*L)';
% else
% MAvec = mat*vec + L*(R'*vec);
% end
%end
end

Obtaining steady state solution for spring mass dashpot system

I'm trying to solve the following problem using MATLAB but I faced multiple issues. The plot I obtained doesn't seem right even though I tried to obtain the steady-state solution, I got a plot that doesn't look steady.
The problem I'm trying to solve
The incorrect plot I got.
and here is the code
% system parameters
m=1; k=1; c=.1; wn=sqrt(k/m); z=c/2/sqrt(m*k); wd=wn*sqrt(1-z^2);
% initial conditions
x0=0; v0=0;
%% time
dt=.001; tMax=8*pi; t=0:(tMax-0)/999:tMax;
% input
A=1
omega=(2*pi)/10
F=A/2-(4*A/pi^2)*cos(omega*t); Fw=fft(F);
F=k*A*cos(omega*t); Fw=fft(F);
% normalize
y = F/m;
% compute coefficients proportional to the Fourier series coefficients
Yw = fft(y);
% setup the equations to solve the particular solution of the differential equation
% by the method of undetermined coefficients
N=1000
T=10
k = [0:N/2];
w = 2*pi*k/T;
A = wn*wn-w.*w;
B = 2*z*wn*w;
% solve the equation [A B;-B A][real(Xw); imag(Xw)] = [real(Yw); imag(Yw)] equation
% Note that solution can be obtained by writing [A B;-B A] as a scaling + rotation
% of a 2D vector, which we solve using complex number algebra
C = sqrt(A.*A+B.*B);
theta = acos(A./C);
Ywp = exp(j*theta)./C.*Yw([1:N/2+1]);
% build a hermitian-symmetric spectrum
Xw = [Ywp conj(fliplr(Ywp(2:end-1)))];
% bring back to time-domain (function synthesis from Fourier Series coefficients)
x = ifft(Xw);
figure()
plot(t,x)
Your forcing function doesn't look like the triangle wave in the problem. I edited the %% time section of your code into the following and appeared to give a steady state response.
%% time
TP = 10; % forcing time period (10 s)
dt=.001;
tMax= 3*TP; % needs to be multiple of the time period
t=0:(tMax-0)/999:tMax;
% input
A=1; % Forcing amplitude
omega=(2*pi)/TP;
% forcing is a triangle wave
% generate a triangle wave with min/max values of 0/1.
F = 0*t;
for i = 1:length(t)
if mod(t(i), TP) <= TP/2
F(i) = mod(t(i), TP)/(TP/2);
else
F(i) = 2 - mod(t(i), TP)/(TP/2);
end
end
F = F*A; % scale triangle wave by amplitude
% you can also use MATLAB's sawtooth() function if you have the signal
% processing toolbox

What sig2ext in Matlab?

What is sig2ext.m file in Matlab? If I want to use the function, should I install any specific toolbox.
Basically, I am trying to do a rainflow counting.
It looks like the function is part of this package:
https://mathworks.com/matlabcentral/fileexchange/3026-rainflow-counting-algorithm?focused=5148893&tab=function
If you want to use that function, all you have to do is to download the package, change your current Matlab working directory to the folder in which you unzipped the package, and create a new script in which you invoke it.
Alternatively, you can just copy and paste the function code into a new script file you previouslty created:
function [ext, exttime] = sig2ext(sig, dt, clsn)
% SIG2EXT - search for local extrema in the time history (signal),
%
% function [ext, exttime] = sig2ext(sig, dt, clsn)
%
% SYNTAX
% sig2ext(sig)
% [ext]=sig2ext(sig)
% [ext,exttime]=sig2ext(sig)
% [ext,exttime]=sig2ext(sig, dt)
% [ext,exttime]=sig2ext(sig, dt, clsn)
%
% OUTPUT
% EXT - found extrema (turning points of the min and max type)
% in time history SIG,
% EXTTIME - option, time of extremum occurrence counted from
% sampling time DT (in seconds) or time vector DT.
% If no sampling time present, DT = 1 is assumed.
%
% INPUT
% SIG - required, time history of loading,
% DT - option, descripion as above, scalar or vector of
% the same length as SIG,
% CLSN - option, a number of classes of SIG (division is performed
% before searching of extrema), no CLSN means no division
% into classes.
%
% The function caused without an output draws a course graph with
% the searched extrema.
%
% By Adam Niesony
% Revised, 10-Nov-2009
% Visit the MATLAB Central File Exchange for latest version
error(nargchk(1,3,nargin))
% Is the time analysed?
TimeAnalize=(nargout==0)|(nargout==2);
% Sprawdzam czy przyrost dt jest podany prawidowo
if nargin==1,
dt=1;
else
dt=dt(:);
end
% Zamieniam dane sig na jedn kolumn
sig=sig(:);
% Dzielimy na klasy jeeli jest podane CLSN
if nargin==3,
if nargout==0,
oldsig=sig;
end
clsn=round(clsn);
smax=max(sig);
smin=min(sig);
sig=clsn*((sig-smin)./(smax-smin));
sig=fix(sig);
sig(sig==clsn)=clsn-1;
sig=(smax-smin)/(clsn-1)*sig+smin;
end
% Tworz wektor binarny w gdzie 1 oznacza ekstremum lub rwno,
% Uznaj e pierwszy i ostatni punkt to ekstremum
w1=diff(sig);
w=logical([1;(w1(1:end-1).*w1(2:end))<=0;1]);
ext=sig(w);
if TimeAnalize,
if length(dt)==1,
exttime=(find(w==1)-1).*dt;
else
exttime=dt(w);
end
end
% Usuwam potrjne wartoci
w1=diff(ext);
w=~logical([0; w1(1:end-1)==0 & w1(2:end)==0; 0]);
ext=ext(w);
if TimeAnalize,
exttime=exttime(w);
end
% Usuwam podwjne wartoci i przesuwam czas na rodek
w=~logical([0; ext(1:end-1)==ext(2:end)]);
ext=ext(w);
if TimeAnalize,
w1=(exttime(2:end)-exttime(1:end-1))./2;
exttime=[exttime(1:end-1)+w1.*~w(2:end); exttime(end)];
exttime=exttime(w);
end
% Jeszcze raz sprawdzam ekstrema
if length(ext)>2, % warunek: w tym momencie moe ju by mao punktw
w1=diff(ext);
w=logical([1; w1(1:end-1).*w1(2:end)<0; 1]);
ext=ext(w);
if TimeAnalize,
exttime=exttime(w);
end
end
if nargout==0,
if length(dt)==1,
dt=(0:length(sig)-1).*dt;
end
if nargin==3,
plot(dt,oldsig,'b-',dt,sig,'g-',exttime,ext,'ro')
legend('signal','singal divided in classes','extrema')
else
plot(dt,sig,'b-',exttime,ext,'ro')
legend('signal','extrema')
end
xlabel('time')
ylabel('signal & extrema')
clear ext exttime
end

bin2mat running slow need a faster solution

I am using bin2mat function from matlab file exchange, for some reason it runs very slow. Is it possible to make it run faster or is there an alternative? I am trying: zC = bin2mat(s.X,s.Y,s.Z,xC,yC); I am not sure where it gets bogged down. I need to do this on point cloud data to calculate volume and such.
Here is the code:
function ZG = bin2mat(x,y,z,XI,YI,varargin)
% BIN2MAT - create a matrix from scattered data without interpolation
%
% ZG = BIN2MAT(X,Y,Z,XI,YI) - creates a grid from the data
% in the (usually) nonuniformily-spaced vectors (x,y,z)
% using grid-cell averaging (no interpolation). The grid
% dimensions are specified by the uniformily spaced vectors
% XI and YI (as produced by meshgrid).
%
% ZG = BIN2MAT(...,#FUN) - evaluates the function FUN for each
% cell in the specified grid (rather than using the default
% function, mean). If the function FUN returns non-scalar output,
% the output ZG will be a cell array.
%
% ZG = BIN2MAT(...,#FUN,ARG1,ARG2,...) provides aditional
% arguments which are passed to the function FUN.
%
% EXAMPLE
%
% %generate some scattered data
% [x,y,z]=peaks(150);
% ind=(rand(size(x))>0.9);
% xs=x(ind); ys=y(ind); zs=z(ind);
%
% %create a grid, use lower resolution if
% %no gaps are desired
% xi=min(xs):0.25:max(xs);
% yi=min(ys):0.25:max(ys);
% [XI,YI]=meshgrid(xi,yi);
%
% %calculate the mean and standard deviation
% %for each grid-cell using bin2mat
% Zm=bin2mat(xs,ys,zs,XI,YI); %mean
% Zs=bin2mat(xs,ys,zs,XI,YI,#std); %std
%
% %plot the results
% figure
% subplot(1,3,1);
% scatter(xs,ys,10,zs,'filled')
% axis image
% title('Scatter Data')
%
% subplot(1,3,2);
% pcolor(XI,YI,Zm)
% shading flat
% axis image
% title('Grid-cell Average')
%
% subplot(1,3,3);
% pcolor(XI,YI,Zs)
% shading flat
% axis image
% title('Grid-cell Std. Dev.')
%
% SEE also RESHAPE ACCUMARRAY FEVAL
% A. Stevens 3/10/2009
% astevens#usgs.gov
%check inputs
error(nargchk(5,inf,nargin,'struct'));
%make sure the vectors are column vectors
x = x(:);
y = y(:);
z = z(:);
if all(any(diff(cellfun(#length,{x,y,z}))));
error('Inputs x, y, and z must be the same size');
end
%process optional input
fun=#mean;
test=1;
if ~isempty(varargin)
fun=varargin{1};
if ~isa(fun,'function_handle');
fun=str2func(fun);
end
%test the function for non-scalar output
test = feval(fun,rand(5,1),varargin{2:end});
end
%grid nodes
xi=XI(1,:);
yi=YI(:,1);
[m,n]=size(XI);
%limit values to those within the specified grid
xmin=min(xi);
xmax=max(xi);
ymin=min(yi);
ymax=max(yi);
gind =(x>=xmin & x<=xmax & ...
y>=ymin & y<=ymax);
%find the indices for each x and y in the grid
[junk,xind] = histc(x(gind),xi);
[junk,yind] = histc(y(gind),yi);
%break the data into a cell for each grid node
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN});
%evaluate the data in each grid using FUN
if numel(test)>1
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind,'uni',0);
else
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind);
end
It is slower on these two steps for one run it took:
ZG=cellfun(#(x)(feval(fun,x,varargin{2:end})),blc_ind); took 33 secs
blc_ind=accumarray([yind xind],z(gind),[m n],#(x){x},{NaN}); took 10 secs
You can change blc_ind = ... to
ZG=accumarray([yind xind],z(gind),[m n],#mean,NaN);
and delete other codes form here so the is no need to if numel(test)>1....

Undefined function 'qquadrature' for input arguments of type 'double'

I'm a new user of Matlab, can you please help:
I have the following code in an .M file:
`% free vibration of non-prismatic Euler beams with or without axial load
%using differential quadrature method
clc;
ne=50;
n=ne+1;
nn=2*n;
no=4;
m=zeros(n,1);
x=zeros(n,1);
xi=zeros(n,1);
c=zeros(n,n,no);
d=zeros(n+4,n+4);
e=zeros(n+4,n+4);
z=zeros(n+4,1);
f=zeros(n+4,1);
alp=zeros(n,n);
bet=zeros(n,n);
zz=zeros(n,1);
ki=zeros(n,n);
eta=zeros(n,n);
const=1.0;
l=12;
ymod=200e09;
rho=7800;
format long;
for i=1:n
xi(i)=0.5*(1-cos((i-1)*pi/ne));
%mi(i)=0.000038*(1-xi(i)^2/2);
ar(i)=1/rho;
mi(i)=0.000038;
ki(i,i)=ymod*mi(i);
end
c=qquadrature(xi,n,no);
%c = harquadrature(xi,n,no)
for i = 1:n
alp(i,i) = 0;
bet(i,i) = 0;
for j = 1:n
alp(i,i) = alp(i,i)+c(i,j,1)*ki(j,j)/l;
bet(i,i) = bet(i,i)+c(i,j,2)*ki(j,j)/l^2;
end
end
d=zeros(n+4,n+4);
% free vibration of the beam
% axial load on the beam t=+ if it is compressive t=- if it is tensile
% weight of the beam / unit length
t=520895.0;
d(1:n,1:n)=2.0*alp*c(:,:,3)/l^3+bet*c(:,:,2)/l^2+ki*c(:,:,4)/l^4+eta+t*c(:,:,2)/l^2;
% boundary conditions
% clamped - free
% d(n+1,1)=1.0;
% d(n+2:n+2,1:n)=alp(n,n)*c(n,1:n,2)/l^2+ki(n,n)*c(n,1:n,3)/l^3+t*c(n,1:n,1)/l;
% d(n+3:n+3,1:n)=c(1,1:n,1)/l;
% d(n+4:n+4,1:n)=ki(n,n)*c(n,1:n,2)/l^2;
% d(1,n+1)=1.0;
% for i=1:n
% d(i,n+2)=d(n+2,i);
% d(i,n+3)=d(n+3,i);
% d(i,n+4)=d(n+4,i);
% end
% pinned - pinned
d(n+1,1)=1.0;
d(n+2:n+2,1:n)=ki(n,n)*c(n,1:n,2)/l^2;
d(n+3:n+3,1:n)=ki(1,1)*c(1,1:n,2)/l^2;
d(n+4,n)=1.0;
d(n,n+4)=1.0;
d(1,n+1)=1.0;
d(n+4,n)=1.0;
for i = 1:n
d(i,n+2)=d(n+2,i);
d(i,n+3)=d(n+3,i);
end
e=zeros(n+4,n+4);
for i=1:n
e(i,i)=rho*ar(i);
end
z=d\e;
[ev,euv]=eig(z);
for i=1:n
zz(z)=ev(i,5);
end
omega=sqrt(1/euv(5,5));
sprintf(' natural frequency\n')
omega;
figure(1);
plot(xi,zz)
xlabel(' x/L ')
ylabel(' z')
title (' fundamental mode shape ')`
I have stored this file (Untitled.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.
Then I am trying to run this single M-files.
But "Undefined function 'qquadrature' for input arguments of type 'double'" message appears..
Can somebody show me where's the problem and how ti fix it?
Thankyou..