setting up constraints in matlab - matlab

I have the following problem:
weights vector =[0.05,0.05,0.15, 0.05,0.22, 0.08, 0.4]
weights vector header =[A11 ,A12 , A21, A22, A31,A32,B1 ] I have the following constraints
0≤A11+A12 + A21+ A22 + A31+A32 ≤0.3
0≤B1≤0.8
A11+A12 =A21+A22 =A31+A32
A11+A12 + A21+ A22 + A31+A32 + B1=1
I want to minimize sum of squared difference to the original weight vector subject to the above constraints
how do I specify this in quadprog in MATLAB the constraints?
Apologies for not formatting well, I am fairly new to stack overflow
Here is some sample basic code
weights.data = [0.05 0.05 0.15 0.05 0.22 0.08 0.4]
weights.header = {'A11', 'A12', 'A21', 'A22', 'A31', 'A32', 'B1'}
w0 = weights.data
Constraints = [
1 0 1
1 0 1
1 0 1
1 0 1
1 0 1
1 0 1
0 1 1
]
ub = [0.3 0.8 1]
lb = [0 0 1]
A0 = Constraints;
%%Inequality constraints
A_le = [-A0, A0]'; % lower bound first
b_le = [-lb, ub];
n = 7
[solution, fval, exitflag,output] = quadProg(eye(n), -w0', A_le,b_le,[],[],[],[],w0, options);
Thanks for your help!

Related

Population Modelling MATLAB

I have a Leslie Matrix
LeslieMatrixA = [0 0.4 0.7 0.5;
x 0 0 0;
0 0.8 0 0;
0 0 0.7 0]
and an initial population vector [10;10;10;10] where 0.7<=x<=0.9.
How can I create MATLAB code to show behaviour of the population over time?
Any help appreciated!
Thanks.
If you just want to show how the population changes you could just plot it right?
x=0.7;
LeslieMatrixA = [0 0.4 0.7 0.5; x 0 0 0; 0 0.8 0 0; 0 0 0.7 0];
P = [10;10;10;10];
for ct = 1:10
bar([1:4],P)
title(sprintf('iteration: %.0f',ct))
pause
P=LeslieMatrixA*P;
end

Why does my 3 axes system coordinate orientation change x with y values?

I am using Matlab and Euler Angles in order to reorient a 3axes coordinate system. Specifically,
Rz = [cos(ψ) sin(ψ) 0;-sin(ψ) cos(ψ) 0;0 0 1];
Ry = [cos(φ) 0 -sin(φ);0 1 0;sin(φ) 0 cos(φ)];
Rx = [1 0 0;0 cos(θ) -sin(θ);0 sin(θ) cos(θ)];
Rtotal = Rz*Ry*Rz
Then I loop through my old system coordinates (x,y,z) and make a vector coord_old. Then I get the reoriented system with (xn,yn,zn)
for i=1:size(num,1)
coord_old = [x(i,1);y(i,1);z(i,1)];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
My issue is that when θ,φ,ψ≃0 then x->-y and y->x and when θ,φ≃0 and ψ=90 then x and y will not rotate! That means that when x,y should rotate they don't and when they shouldn't rotate they stay as they were!
--EDIT--
For example, when ψ=20.0871, φ=0.0580 and θ=0.0088 I get these results
See that x->-y and y->x while z doesn't change at all!
Any thoughts?
Ok, I see two main problems here:
Rtotal = Rz*Ry*Rz is probably not what you want since Rz is multiplied twice. I think you mean Rtotal = Rz*Ry*Rx.
Your rotation matrix seems to be incorrect. Check this Wikipedia artice to get the correct signs.
Here a corrected rotation matrix:
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rx;
With this matrix I get the correct results:
x=1; y=2; z=3;
psi=0; phi=0; theta=0;
[xn,yn,zn] >> 1 2 3
x=1; y=2; z=3;
psi=90/180*pi; phi=0; theta=0;
[xn,yn,zn] >> -2 1 3
And here a full graphical example of a cube in 3d-space:
% Create cube (not in origin)
DVert = [0 0 0; 0 1 0; 1 1 0; 1 0 0 ; ...
0 0 1; 0 1 1; 1 1 1; 1 0 1];
DSide = [1 2 3 4; 2 6 7 3; 4 3 7 8; ...
1 5 8 4; 1 2 6 5; 5 6 7 8];
DCol = [0 0 1; 0 0.33 1; 0 0.66 1; ...
0 1 0.33; 0 1 0.66; 0 1 1];
% Rotation angles
psi = 20 /180*pi; % Z
phi = 45 /180*pi; % Y
theta = 0 /180*pi; % X
% Rotation matrix
Rz = [cos(psi) -sin(psi) 0; sin(psi) cos(psi) 0; 0 0 1];
Ry = [cos(phi) 0 sin(phi); 0 1 0; -sin(phi) 0 cos(phi)];
Rx = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
Rtotal = Rz*Ry*Rz;
% Apply rotation
DVertNew = Rtotal * DVert';
% Plot cubes
figure;
patch('Faces',DSide,'Vertices',DVert,'FaceColor','flat','FaceVertexCData',DCol);
patch('Faces',DSide,'Vertices',DVertNew','FaceColor','flat','FaceVertexCData',DCol);
% Customize view
grid on;
axis equal;
view(30,30);
When I use your code and insert 0 for all angles, I get Rtotal:
Rtotal =
1 0 0
0 1 0
0 0 1
This is the identity matrix and will not change your values.
You have an error in your matrix multiplication. I think you should multiply: Rtotal*coord_old. I think you are missing the _old. depending on what is in you coordvariable, this may be the bug.
When I run:
for i=1:size(1,1)
coord_old = [1;2;3];
coord_new = Rtotal*coord_old;
xn(i,1) = coord_new(1,1);
yn(i,1) = coord_new(2,1);
zn(i,1) = coord_new(3,1);
end
I get the correct result:
coord_new =
1
2
3
Thank you both #Steffen and #Matt. Unfortunately, my reputation is not high enough to vote Up your answers!
The problem was not with Rtotal as #Matt correctly stated. It should be as it was Rz*Ry*Rx. However, both your ideas helped me test my code with simple examples (5 sets of coordinates and right hand rule), and realize where my (amateur) mistake was.
I had forgotten I had erased parts of codes where I was expressing my angles to degrees... I should be using sind & cosd instead of sin and cos.

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.

Plot square surface in Matlab

How to plot a square surface in Matlab?
More exactly I want to plot a square square with value 0.5 surface which is located at X:-1 to X=1 and Y:2.5 to 3.5.
I tried the following
[X,Y] = meshgrid(-3.5:.5:3.5);
Z = zeros(15);
Z(end-2:end,5:9) = 0.5;
surf(X,Y,Z);
This doesn't result in a perpendicular edge. How to archive that?
This is what the patch function is for.
Matlab documentation
so for your case:
X = [ -1 -1 1 1];
Y = [3.5 2.5 2.5 3.5];
Z = [0.5 0.5 0.5 0.5];
patch(X,Y,Z,'red')
view(45,45)
You need to provide multiple Z-values together with the same X, Y values. A small example:
>> [X, Y]= meshgrid([1,2,2,3,4], 1:2)
X =
1 2 2 3 4
1 2 2 3 4
Y =
1 1 1 1 1
2 2 2 2 2
>> Z = [0,0,1,1,0;0,0,1,1,0]
Z =
0 0 1 1 0
0 0 1 1 0
>> surf(X, Y, Z)
Yields this:
This should be the same in 2D, you just need to wrap you head around which X and Y values to duplicate and adjust the Z-Matrix accordingly.
I ended up with
figure;
hold on;
X = [ -2 -2 2 2];
Y = [2 4 4 2];
Z = [0 0 0 0];
patch(X,Y,Z,'blue');
X = [ -1 -1 1 1];
Y = [3.5 2.5 2.5 3.5];
Z = [0.5 0.5 0.5 0.5];
h = patch(X,Y,Z,'red');
X = [ -1 -1 1 1];
Y = [2.5 2.5 2.5 2.5];
Z = [0 0.5 0.5 0];
patch(X,Y,Z,'red');
X = [1, 1, 1, 1];
Y = [2.5 2.5 3.5 3.5];
Z = [0 0.5 0.5 0];
patch(X,Y,Z,'red');
view(45,30)
legend(h, 'F(u,v)')
xlabel('u')
ylabel('v')
zlabel('F(u,v)')

Matlab, linear programming

I want to solve this linear programming (simplex) problem using MATLAB 7, but it returns
Exiting: the problem is unbounded.
This function
f = 2(15 s0 + 8s1 + 2576s2 + 744s3 + 427s4 + 8s5)
Should be minimized in such a way that two constraints for each observation are
satisfied
0.1s0 + 0.1s1 + 14.5s2 + 4s3 + 2.4s4 – a0 − a1 − 145a2 − 40a3 − 24a4 ≥ −2.2
0.1s0 + 0.1s1 + 14.5s2 + 4s3 + 2.4s4 + a0 + a1 + 145a2 + 40a3 + 24a4 ≥ 2.2
S5 and a5 are 0. I used
f = [15 8 2576 744 427 8 15 8 2576 744 427 8];
b = [-2.2; 2.2];
a = [0.1 0.1 14.5 0.4 2.4 0 -1 -1 -145 -40 -24 0 ; 0.1 0.1 14.5 4 2.4 0 1 1 145 40 24 0];
[x, fval, exitflag, output, lambda] = linprog(f, a, b)
What is the right way to solve this problem?
You didn't constrain your s5 and a5 to actually be zero, since you set the corresponding coefficients in the a matrix to zero. Thus, they can take on any value, and the LP is unbounded.
To fix, add an equality constraint:
beq = [0; 0];
aeq = [0 0 0 0 0 1 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 1];
[x,fval,exitflag,output,lambda] = linprog(f,a,b,aeq,beq)
Or, just drop s5 and a5 from the LP since they don't contribute at all.