Different result for same value in sigmoid function [MATLAB] - matlab

I am trying to find out the result of the sigmoid function. When I tried to compute it without loop. I did not get the expected result. On the other hand, using for loop getting an actual result.
The question is, Why I am getting different results?
%This is a hypothesis of AND operation
x1 = randi([0,1],[1,4]);
x2 = randi([0,1],[1,4]);
features = [x1;x2]';
theta = [-30 20 20];
x = [ones(length(features),1) features]
z = x * theta';
pred = zeros(length(z),1);
pred = 1 / (1 + exp(-z))
y1 = (pred >= 0.5)
fprintf('Using loop\n')
for i= 1:length(z)
pred(i) = 1 / (1 + exp(-z(i)));
end
pred
y1 = (pred >= 0.5)
Output:
x =
1 1 1
1 0 1
1 0 0
1 0 1
pred =
1.0e-13 *
0 0 0.9358 0
y1 =
0 0 0 0
Using loop
pred =
1.0000 0.0000 0.0000 0.0000
y1 =
1 0 0 0

Related

Generating discrete signal on Matlab

I am trying to generate a constant signal x[n] = 1 for n = 1, 2, 3 and x[n] = 0 otherwise using matlab.
N = -5:1:5;
X = -5:1:5;
i = 1;
for n = N
if (n >= 1 && n <= 3)
X[i] = 1;
else
X[i] = 0;
end
i = i + 1;
end
But it does not work. I am really new using Matlab for discrete signals, so any help would be welcome.
Thank you.
There is no need to use a for iteration in this case. You can accomplish the same using an indexed approach as follows:
N1 = -5:5;
X1 = zeros(1,numel(N1));
X1(N1 >= 1 & N1 <= 3) = 1
N2 = -8:2;
X2 = zeros(1,numel(N2));
X2(N2 >= 1 & N2 <= 3) = 1
N3 = 1:11;
X3 = zeros(1,numel(N3));
X3(N3 >= 1 & N3 <= 3) = 1
This will output:
X1 =
0 0 0 0 0 0 1 1 1 0 0
X2 =
0 0 0 0 0 0 0 0 0 1 1
X3 =
1 1 1 0 0 0 0 0 0 0 0

linprog (Matlab) vs glpk(Octave) - Why do I get different solutions?

I need to find an initial feasible solution for a network flow problem which is given by Nx = b, x >= 0. I used to the following OCTAVE code to do the job:
function [x,exitflag] = FIND_EXTREME_POINT(A,b)
%
% Function FIND_EXTREME_POINT
%
% Call: [x,fval] = FIND_EXTREME_POINT(A,b)
%
% MATLAB code for finding a extreme point of the set X = { x | Ax = b,
% x >= 0} by solving the auxiliary problem
%
% min 1'*y
% s.t. A*x + y = b
% x >= 0
% y >= 0
%
% either to receive a feasible basis solution or to return infeasibility
%
% Inputs:
% A - m*n matrix
% b = m*1 rhs
%
% Output:
% x - n*1 vector respresenting an extreme point
% exitflag - 0 if problem is infeasibile, 1 if the problem is feasible
%
% setup parameters for LP solver
[m,n] = size(A);
A_aux = [A eye(m)];
c_aux = [zeros(1,n) ones(1,m)]';
lb = [zeros(1,n+m)]';
ub = [];
ctype = repmat('S',1,m);
vartype = repmat('C',1,n+m);
% solve auxiliary LP
[xmin, fmin] = glpk(c_aux, A_aux, b, lb, ub, ctype, vartype, 1);
...% driving out artifical variables
The solution given by the glpk-solver matches to my handwritten notes.
For some reason I'm using MATLAB and I've got a problem with the following code:
function [x,exitflag] = FIND_EXTREME_POINT(A,b)
%
% Function FIND_EXTREME_POINT
%
% Call: [x,fval] = FIND_EXTREME_POINT(A,b)
%
% ....
%
% setup parameters for LP solver
[m,n] = size(A);
x = []; % output vector
A_aux = [A eye(m)];
c_aux = [zeros(1,n) ones(1,m)]';
lb = [zeros(1,n+m)]';
ub = [];
% solve auxiliary LP
[xmin,fval] = linprog(c_aux,[],[],A_aux,b,lb,ub);
... % driving out artifical variables
On the one hand the optimal solution xmin computed by the linprog-solver is feasible but on the other hand the function value fval is not zero but it should be so... Where is the mistake in my MATLAB code?
A little example:
N = [1 1 0 0 0 0 0;
-1 0 0 -1 0 0 0;
0 -1 1 0 0 -1 0;
0 0 -1 1 1 0 0;
0 0 0 0 0 1 1;
0 0 0 0 -1 0 -1];
b = [5 -5 0 0 0 0]';
OCTAVE gives me:
xmin =
5
0
0
0
0
0
0
0
0
0
0
0
0
fval =
0
MATLAB gives me:
xmin =
0.2184
4.7816
4.7816
4.7816
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
fval =
5.8775e-016

How to write a varying matrix in matlab?

I have this equation system a set of 1 ≤ n ≤ 30
−(2 + α)x1 + x2 = b1,
xj−1 − (2 + α)xj + xj+1 = bj , for 2 ≤ j ≤ 29,
x29 − (2 + α)x30 = b30.
α = 1
We assume that the membrane is held at the end points (i.e x0 = 0 and x31 = 0). There is no weight on the membrane so all bj = 0 for j = 1 . . . 30 except for j = 6 where a load is applied: b6 = 2.
I want to calculate LU factorization of the system .
I do not know how to implement the left side of the system in matlab.
The right side I made it like this :
b=[0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]';
How to do the left side?
Thanks
It's unclear why you have included the entire linear system if you are only interested in the LU factorization of A? Regardless, here is some code which generates your A matrix as described above, and solves the linear system and shows the LU factorization.
% equation A*X = b
b = zeros(30,1);
b(6) = 2;
alpha = 1;
A = zeros(30, 30);
A(1, 1) = -(2 + alpha);
A(1, 2) = 1;
for i = 2:29
A(i, i-1) = 1;
A(i, i) = -(2 + alpha);
A(i, i+1) = 1;
end
A(30, 29) = 1;
A(30, 30) = -(2 + alpha);
You can then get the LU factorization using lu(A) or solve the linear system of equations using linsolve(A,b).

How to formulate this expression

I am new to MATLAB and I want to formulate the following lease square expression in Matlab. I have some codes that I am typing here. But the optimization problem solution seems not to be correct. Does anyone has an idea why?
First, I want to solve the heat equation
$$T_t(x,t) = - L_x . T(x,t) + F(x,t)$$
where L_x is Laplacian matrix of the graph.
then find y from the following least square.
$$ \min_y \sum_{j} \sum_{i} (\hat{T}_j(t_i) - T_j(t_i, y))^2$$
Thanks in advance!!
Here is my code:
%++++++++++++++++ main ++++++++++++++++++++
% incidence matrix for original graph
C_hat = [ 1 -1 0 0 0 0;...
0 1 -1 0 0 -1;...
0 0 0 0 -1 1;...
0 0 0 1 1 0;...
-1 0 1 -1 0 0];
% initial temperature for each vertex in original graph
T_hat_0 = [0 7 1 9 4];
[M_bar,n,m_bar,T_hat_heat,T_hat_temp] = simulate_temp(T_hat_0,C_hat);
C = [ 1 1 -1 -1 0 0 0 0 0 0;...
0 -1 0 0 1 -1 1 0 0 0;...
0 0 1 0 0 1 0 -1 -1 0;...
0 0 0 1 0 0 -1 0 1 -1;...
-1 0 0 0 -1 0 0 1 0 1];
%
% initial temperature for each vertex in original graph
T_0 = [0 7 1 9 4];
%
% initial temperature simulation
[l,n,m,T_heat,T_temp] = simulate_temp(T_0,C);
%
% bounds for variables
lb = zeros(m,1);
ub = ones(m,1);
%
% initial edge weights
w0 = ones(m,1);
% optimization problem
% w = fmincon(#fun, w0, [], [], [], [], lb, ub);
%++++++++++++++++++++ function++++++++++++++++++++++++++++
function [i,n,m,T_heat,T_temp] = simulate_temp(T,C)
%
% initial conditions
delta_t = 0.1;
M = 20; %% number of time steps
t = 1;
[n,m] = size(C);
I = eye(n);
L_w = C * C';
T_ini = T';
Temp = zeros(n,1);
% Computing Temperature
%
for i=1:M
K = 2*I + L_w * delta_t;
H = 2*I - L_w * delta_t;
%
if i == 1
T_heat = (K \ H) * T_ini;
%
t = t + delta_t;
else
T_heat = (K \ H) * Temp;
%
t = t + delta_t;
end
% replacing column of T_final with each node temperature in each
% iteration. It adds one column to the matrix in each step
T_temp(:,i) = T_heat;
%
Temp = T_heat;
end
end
%++++++++++++++++++ function+++++++++++++++++++++++++++++++++++++++++
function w_i = fun(w);
%
for r=1:n
for s=1:M_bar
w_i = (T_hat_temp(r,s) - T_temp(r,s)).^2;
end
end
To give a more clear answer, I need more information about what form you have the functions F_j and E_j in.
I've assumed that you feed each F_j a value, x_i, and get back a number. I've also assumed that you feed E_j a value x_i, and another value (or vector) y, and get back a value.
I've also assumed that by 'i' and 'j' you mean the indices of the columns and rows respectively, and that they're finite.
All I can suggest without knowing more info is to do this:
Pre-calculate the values of the functions F_j for each x_i, to give a matrix F - where element F(i,j) gives you the value F_j(x_i).
Do the same thing for E_j, giving a matrix E - where E(i,j) corresponds to E_j(x_i,y).
Perform (F-E).^2 to subtract each element of F and E, then square them element-wise.
Take sum( (F-E).^2**, 2)**. sum(M,2) will sum across index i of matrix M, returning a column vector.
Finally, take sum( sum( (F-E).^2, 2), 1) to sum across index j, the columns, this will finally give you a scalar.

How can I generate the following matrix in MATLAB?

I want to generate a matrix that is "stairsteppy" from a vector.
Example input vector: [8 12 17]
Example output matrix:
[1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1]
Is there an easier (or built-in) way to do this than the following?:
function M = stairstep(v)
M = zeros(length(v),max(v));
v2 = [0 v];
for i = 1:length(v)
M(i,(v2(i)+1):v2(i+1)) = 1;
end
You can do this via indexing.
A = eye(3);
B = A(:,[zeros(1,8)+1, zeros(1,4)+2, zeros(1,5)+3])
Here's a solution without explicit loops:
function M = stairstep(v)
L = length(v); % M will be
V = max(v); % an L x V matrix
M = zeros(L, V);
% create indices to set to one
idx = zeros(1, V);
idx(v + 1) = 1;
idx = cumsum(idx) + 1;
idx = sub2ind(size(M), idx(1:V), 1:V);
% update the output matrix
M(idx) = 1;
EDIT: fixed bug :p
There's no built-in function I know of to do this, but here's one vectorized solution:
v = [8 12 17];
N = numel(v);
M = zeros(N,max(v));
M([0 v(1:N-1)]*N+(1:N)) = 1;
M(v(1:N-1)*N+(1:N-1)) = -1;
M = cumsum(M,2);
EDIT: I like the idea that Jonas had to use BLKDIAG. I couldn't help playing with the idea a bit until I shortened it further (using MAT2CELL instead of ARRAYFUN):
C = mat2cell(ones(1,max(v)),1,diff([0 v]));
M = blkdiag(C{:});
A very short version of a vectorized solution
function out = stairstep(v)
% create lists of ones
oneCell = arrayfun(#(x)ones(1,x),diff([0,v]),'UniformOutput',false);
% create output
out = blkdiag(oneCell{:});
You can use ones to define the places where you have 1's:
http://www.mathworks.com/help/techdoc/ref/ones.html