Matlab: fmincon converging to infeasible point, not meeting equality constraint - matlab

I am trying to use fmincon to optimize x such that R*x is minimized while x can be values between 0 and 1.5 and the sum(x) = 3
R = transpose([6 6 6 6 6 6 6 9 9 9 9 13 13 13 13 9 9 9 9 6 6 6 6 6]);
x0 = zeros(24,1);
f='0';
for j=1:24
s='%d*x(%d)';
s=sprintf(s,R(j),j);
g='%s+%s';
f=sprintf(g,f,s);
end
A = ones(2,24);
A(2,:) = -1;
b = [1.5; 0]; % Want x's to be between 0 and 1.5
Aeq = ones(1,24);
beq = [3];
%Bounds
lb = zeros(24,1);
ub = ones(24,1);
x = fmincon(f, x0, A, b,Aeq, beq,lb,ub);
I want the sum of x to equal 3 (attempted to show this with equivalency matrices Aeq and beq). The following error was shown when I run the code:
converged to an infeasible point
It should be noted that this code shows that sum(x) = 2.25 instead of sum(x) = 3

First, your function definition can be shortened to:
R = [6 6 6 6 6 6 6 9 9 9 9 13 13 13 13 9 9 9 9 6 6 6 6 6];
f = #(x)R*x;
Second, your initial point x0 does not satisfy your constraints, so let's change it to:
x0 = zeros(24,1)+3/24;
so that the sum of x is equal to 3.
Next, since you have a constant upper and lower bounds on x, you don't need to use A and b matrices, so we get rid of them and replace them with empty matrices []. We will rely only on lb and ub instead:
lb = zeros(24,1);
ub = 1.5*ones(24,1);
Finally, we start fmincon by using
x = fmincon(f, x0,[],[],Aeq, beq,lb,ub)
Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the optimality tolerance,
and constraints are satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
x =
0.2500
0.2500
0.2500
0.2500
0.2500
0.2500
0.2500
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.0000
0.2500
0.2500
0.2500
0.2500
0.2500

Related

How do i perform operations on matrix rows while keeping the matrix intact?

Question/problem summary:
Create a 10 by 10 matrix whose first column is the numbers 1,2,3,4,5,6,7,8,9,10
the next column contains the squares of first column: 1, 4, 9,...,100
the third column contains the 3rd power of first column: 1, 8, 27,..., 1000.
the 10th column contains the 10th power of the first column.
Background:
This is for a class assignment, intro to analytical programming. I have tried the following code, but i am not sure why it is not giving the correct output. Any advice or suggestions is appreciated.
row1 = [1:10]
tenXtenMatrix = repmat(row1,10,1)
[row col] = size(tenXtenMatrix)
for i=2:row
for j=1:col
tenXtenMatrix(i,:).^i
end
end
what is expected:
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
1 8 27 64 125 216 343 512 729 1000
1 16 81 256 625 1296 2401 4096 6561 10000
etc..
what i got:
0.0000 0.0000 0.0000 0.0001 0.0010 0.0060 0.0282 0.1074 0.3487 1.0000
0.0000 0.0000 0.0000 0.0001 0.0010 0.0060 0.0282 0.1074 0.3487 1.0000
0.0000 0.0000 0.0000 0.0001 0.0010 0.0060 0.0282 0.1074 0.3487 1.0000
0.0000 0.0000 0.0000 0.0001 0.0010 0.0060 0.0282 0.1074 0.3487 1.0000
etc...
Using implicit expansion:
x = 1:10
A = x.^(x.')
Where:
.^ is the element-wise power operator
.' is the transpose operator
More informations about implicit expansion here.
Fixes:
you run on j and not using it.
you calculate the power but not updating the matrix
row1 = [1:10];
tenXtenMatrix = repmat(row1,10,1);
[row col] = size(tenXtenMatrix);
for i=2:row
tenXtenMatrix(i,:) = tenXtenMatrix(i,:).^i;
end

Solve System of Linear Equations in MatLab with Matrix of Arbitrary Size for Finite Difference Calculation

I am trying to write a script in MatLab R2016a that can solve a system of linear equations that can have different sizes depending on the values of p and Q.
I have the following equations that I am trying to solve, where h=[-p:1:p]*dx. Obviously, there is some index m where h=0, but that shouldn't be a problem.
I'm trying to write a function where I can input p and Q and build the matrix and then just solve it to get the coefficients. Is there a way to build a matrix using the variables p, Q, and h instead of using different integer values for each individual case?
I would use bsxfun(in recent matlab versions this function may be implented to the interpreter, I don't know for sure):
p = 4;
Q = 8;
dx = 1;
h = -p:p*dx
Qvector = [Q,1:Q-1]'
Matrix = bsxfun(#(Qvector, h)h.^(Qvector)./factorial(Qvector), Qvector, h)
Output:
h =
-4 -3 -2 -1 0 1 2 3 4
Qvector =
8
1
2
3
4
5
6
7
Matrix =
1.6254 0.1627 0.0063 0.0000 0 0.0000 0.0063 0.1627 1.6254
-4.0000 -3.0000 -2.0000 -1.0000 0 1.0000 2.0000 3.0000 4.0000
8.0000 4.5000 2.0000 0.5000 0 0.5000 2.0000 4.5000 8.0000
-10.6667 -4.5000 -1.3333 -0.1667 0 0.1667 1.3333 4.5000 10.6667
10.6667 3.3750 0.6667 0.0417 0 0.0417 0.6667 3.3750 10.6667
-8.5333 -2.0250 -0.2667 -0.0083 0 0.0083 0.2667 2.0250 8.5333
5.6889 1.0125 0.0889 0.0014 0 0.0014 0.0889 1.0125 5.6889
-3.2508 -0.4339 -0.0254 -0.0002 0 0.0002 0.0254 0.4339 3.2508

Normalize each slice of a 3D matrix

How do I normalize each slice of a 3D matrix? I tried like this:
a=rand(1,100,3481);
a= (a - min(a)) ./ (max(a)-min(a)); %
By right each slice of matrix should ranges from 0 to 1. But that is not the case, I don't find 1 in some of the slices. As I inspected, min(a) and max(a) returned the respective value in 3D. Thus it should be of no issue using the code above. Is there something I missed for 3D matrix? Thanks in advance!
We need to find the minimum and maximum values for each of those 2D slices and then we can use bsxfun to do those operations in a vectorized manner with help from permute to let the singleton dims align properly to let bsxfun do its broadcasting job (or use reshape there).
Hence, the implementation would be -
mins = min(reshape(a,[],size(a,3)));
maxs = max(reshape(a,[],size(a,3)));
a_offsetted = bsxfun(#minus, a, permute(mins,[1,3,2]));
a_normalized = bsxfun(#rdivide, a_offsetted, permute(maxs-mins,[1,3,2]))
Sample input, output -
>> a
a(:,:,1) =
2 8 2 2
8 3 8 2
a(:,:,2) =
8 1 1 5
4 9 8 6
a(:,:,3) =
7 9 3 5
6 2 6 5
a(:,:,4) =
9 3 4 9
7 1 9 9
>> a_normalized
a_normalized(:,:,1) =
0 1.0000 0 0
1.0000 0.1667 1.0000 0
a_normalized(:,:,2) =
0.8750 0 0 0.5000
0.3750 1.0000 0.8750 0.6250
a_normalized(:,:,3) =
0.7143 1.0000 0.1429 0.4286
0.5714 0 0.5714 0.4286
a_normalized(:,:,4) =
1.0000 0.2500 0.3750 1.0000
0.7500 0 1.0000 1.0000
My option would be without reshaping as it is sometimes bit difficult to understand. I use min max with the dimension you want want to use for normalization with repmat to clone...:
a=rand(1,100,3481);
a_min2 = min(a,[],2);
a_max2 = max(a,[],2);
a_norm2 = (a - repmat(a_min2,[1 size(a,2) 1]) ) ./ repmat( (a_max2-a_min2),[1 size(a,2) 1]);
or if normalization on 3rd dim...
a_min3 = min(a,[],3);
a_max3 = max(a,[],3);
a_norm3 = (a - repmat(a_min3,[1 1 size(a,3)]) ) ./ repmat( (a_max3-a_min3),[1 1 size(a,3)]);

Generating a grid in matlab with a general number of dimensions

Problem
I have a vector w containing n elements. I do not know n in advance.
I want to generate an n-dimensional grid g whose values range from grid_min to grid_max and obtain the "dimension-wise" product of w and g.
How can I do this for an arbitrary n?
Examples
For simplicity, let's say that grid_min = 0 and grid_max = 5.
Case: n=1
>> w = [0.75];
>> g = 0:5
ans =
0 1 2 3 4 5
>> w * g
ans =
0 0.7500 1.5000 2.2500 3.0000 3.7500
Case: n=2
>> w = [0.1, 0.2];
>> [g1, g2] = meshgrid(0:5, 0:5)
g1 =
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
g2 =
0 0 0 0 0 0
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
>> w(1) * g1 + w(2) * g2
ans =
0 0.1000 0.2000 0.3000 0.4000 0.5000
0.2000 0.3000 0.4000 0.5000 0.6000 0.7000
0.4000 0.5000 0.6000 0.7000 0.8000 0.9000
0.6000 0.7000 0.8000 0.9000 1.0000 1.1000
0.8000 0.9000 1.0000 1.1000 1.2000 1.3000
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000
Now suppose a user passes in the vector w and we do not know how many elements (n) it contains. How can I create the grid and obtain the product?
%// Data:
grid_min = 0;
grid_max = 5;
w = [.1 .2 .3];
%// Let's go:
n = numel(w);
gg = cell(1,n);
[gg{:}] = ndgrid(grid_min:grid_max);
gg = cat(n+1, gg{:});
result = sum(bsxfun(#times, gg, shiftdim(w(:), -n)), n+1);
How this works:
The grid (variable gg) is generated with ndgrid, using as output a comma-separated list of n elements obtained from a cell array. The resulting n-dimensional arrays (gg{1}, gg{2} etc) are contatenated along the n+1-th dimension (using cat), which turns gg into an n+1-dimensional array. The vector w is reshaped into the n+1-th dimension (shiftdim), multiplied by gg using bsxfun, and the results are summed along the n+1-th dimension.
Edit:
Following #Divakar's insightful comment, the last line can be replaced by
sz_gg = size(gg);
result = zeros(sz_gg(1:end-1));
result(:) = reshape(gg,[],numel(w))*w(:);
which results in a significant speedup, because Matlab is even better at matrix multiplication than at bsxfun (see for example here and here).

Decreasing data points by half via downsampling

Let say I have a 6x5 matrix (my actual data is way bigger)
A B C D E
1 5 7 2 3
2 1 9 8 5
3 1 2 3 1
4 1 3 4 2
5 2 9 0 1
6 5 3 4 3
I have to make a plot with A on the x-axis and B,C,D on the y-axis. If I want to reduce the data points by half (by averaging each adjacent pair of data points), how do I do it? What if I want to decrease the points even further by averaging every five (or n) points?
I looked at the MATLAB help document but I'm still confused
I got what I needed, thanks for the input guys, it really helped
There you go:
M = [1 5 7 2 3
2 1 9 8 5
3 1 2 3 1
4 1 3 4 2
5 2 9 0 1
6 5 3 4 3]; % data
>>result = (M(1:2:end-1,:) + M(2:2:end,:))/2;
result =
1.5000 3.0000 8.0000 5.0000 4.0000
3.5000 1.0000 2.5000 3.5000 1.5000
5.5000 3.5000 6.0000 2.0000 2.0000
An even number of rows scenario is straightforward, using mean to do the work:
>> M = magic(4)
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> reshape(mean(reshape(M,2,[]),1),[],size(M,2))
ans =
10.5000 6.5000 6.5000 10.5000
6.5000 10.5000 10.5000 6.5000
For the odd number of rows scenario, let's say you want to retain the last row. Here's a general even/odd solution:
>> M = magic(5) % 5 rows!
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> Mp = [M; repmat(M(end,:),mod(size(M,1),2),1)]; % replicate last row if odd
>> Mthin = reshape(mean(reshape(Mp,2,[]),1),[],size(Mp,2))
Mthin =
20.0000 14.5000 4.0000 11.0000 15.5000
7.0000 9.0000 16.0000 20.5000 12.5000
11.0000 18.0000 25.0000 2.0000 9.0000
Alternatively, if you want to throw away the last row when encountered with an odd number of rows:
>> Mp = M(1:end-mod(size(M,1),2),:);
>> Mthin = reshape(mean(reshape(Mp,2,[]),1),[],size(Mp,2))
Mthin =
20.0000 14.5000 4.0000 11.0000 15.5000
7.0000 9.0000 16.0000 20.5000 12.5000
Now for averaging n points, retaining the average of the mod(size(M,1),n) last rows:
n = 5;
M = rand(972,5); % or whatever
p = mod(size(M,1),n);
r = repmat(mean(M(end-p+1:end,:),1),(p>0)*(n-p),1);
Mp = [M; r];
Mthin = reshape(mean(reshape(Mp,n,[]),1),[],size(Mp,2));
And for throwing out the last mod(size(M,1),n) rows:
Mp = M(1:end-mod(size(M,1),n),:);
Mthin = reshape(mean(reshape(Mp,n,[]),1),[],size(Mp,2));
>>> A= magic(5) %some "random" data
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>>> B=(A(1:2:end-1,:)+A(2:2:end,:))/2
B =
20.0000 14.5000 4.0000 11.0000 15.5000
7.0000 9.0000 16.0000 20.5000 12.5000
Takes average of each pair of rows, ignores the last row if row count is not even.
And some general solution:
%input data data
X=randi(30,30,5)
step=7
%extend matrix, until size matches step (could be done faster using repmat)
while(mod(size(X,1),step)~=0)
X(end+1,:)=X(end,:)
end
%Split data into segments of size "step"
C=mat2cell(X,repmat(step,floor(size(X,1)/step),1),size(X,2))
%Average over each segment:
AVG=cell2mat(cellfun(#(x)(mean(x,1)),C,'UniformOutput',false))