Related
I am trying to implement special ordered set of type 2 SOS2 in my optimization problem to linearyze it.
I have: w = x1*x2 = y1^2 - y2^2
Therefore: y1 = 1/2(x1 + x2) and y1 = 1/2(x1 - x2)
I want to apply this method to y1^2 and y2^2 and I am considering x of my problem y1 and y2. I am using cplex as solver and is saying is infeasible.
My code is a model of a distributed system
%% Reset cmd
% Funciona
clear all
close all
clc
%% Define base values
SBase=5e3; % kVA
VBase=4160; % V
ZBase=VBase^2/(SBase)/1e3; % Ohm
YBase = 1/ZBase; % Siemens
IBase=SBase/(sqrt(3)*VBase);
%% Data
T= 24; %Time considered
N=3;
% kW
DA = -[0.0770 0.0256 0.0770 0.0320 0.0320 0.0770 0.0256 0.0320 0.0770 0.0320]/10;
DR = -[0.0440 0.0172 0.0440 0.0220 0.0220 0.0440 0.0172 0.0220 0.0440 0.0220]/10;
DA = [DA(1:6), DA(1:6), DA(1:9), DA(1:6), DA(1:2), DA, DR(1:6), DR(1:9), DA, DR(1:6), DR(1:9), DA ];
DR = [DR(1:6), DR(1:9), DR(1:9), DR(1:6), DR(1:9), DR(1:6), DR(1:9), DR, DR(1:6), DR(1:9) ];
% PV
P_res = [0 0 0 0 0.01 0.04 0.06 0.07 0.1 0.12 0.17 0.14 0.17 0.15 0.1 0.09 0.08 0.05 0.05 0.06 0 0 0 0]/20;
Q_res = P_res *tan(acos(0.95));
% 12 Nodes
Y = [ 3.9638 - 11.424i -3.9638 + 11.424i 0 + 0i
-3.9638 + 11.424i 42.193 - 61.341i -15.004 + 13.801i
0 + 0i -15.004 + 13.801i 40.009 - 36.803i ]*YBase;
%% Variables
Ir=sdpvar(N,T);
Ii=sdpvar(N,T);
Vr=sdpvar(N,T);
Vi=sdpvar(N,T);
V=sdpvar(N,T);
I=sdpvar(N,T);
P=sdpvar(N,T);
Q=sdpvar(N,T);
%% Constraints
distancia = 1000;
xi = linspace(-10,10,distancia)';
fi = xi.^2;
lambda1 = sdpvar(distancia, N*T);
lambda2 = sdpvar(distancia, N*T);
lambda3 = sdpvar(distancia, N*T);
lambda4 = sdpvar(distancia, N*T);
lambda5 = sdpvar(distancia, N*T);
lambda6 = sdpvar(distancia, N*T);
lambda7 = sdpvar(distancia, N*T);
lambda8 = sdpvar(distancia, N*T);
z1 = sdpvar(N,T);
z2 = sdpvar(N,T);
z3 = sdpvar(N,T);
z4 = sdpvar(N,T);
z5 = sdpvar(N,T);
z6 = sdpvar(N,T);
z7 = sdpvar(N,T);
z8 = sdpvar(N,T);
x1 = sdpvar(N,T);
x2 = sdpvar(N,T);
x3 = sdpvar(N,T);
x4 = sdpvar(N,T);
x5 = sdpvar(N,T);
x6 = sdpvar(N,T);
x7 = sdpvar(N,T);
x8 = sdpvar(N,T);
F = [
x1 == 1/2*(Vr + Ir),...
x2 == 1/2*(Vr - Ir),...
x3 == 1/2*(Vi + Ii),...
x4 == 1/2*(Vi - Ii),...
x5 == 1/2*(Vr + Ii),...
x6 == 1/2*(Vr - Ii),...
x7 == 1/2*(Vi + Ir),...
x8 == 1/2*(Vi - Ir)];
for n = 1:N
for t = 1:T
F = [F,...
sos2(lambda1(:,n*t)),...
sos2(lambda2(:,n*t)),...
sos2(lambda3(:,n*t)),...
sos2(lambda4(:,n*t)),...
sos2(lambda5(:,n*t)),...
sos2(lambda6(:,n*t)),...
sos2(lambda7(:,n*t)),...
sos2(lambda8(:,n*t)),...
];
end
end
for n = 1:N
for t = 1:T
F = [F,...
sum(lambda1(:,n*t)) == 1,...
lambda1(:,n*t) >= 0,...
sum(lambda2(:,n*t)) == 1,...
lambda2(:,n*t) >= 0,...
sum(lambda3(:,n*t)) == 1,...
lambda3(:,n*t) >= 0,...
sum(lambda4(:,n*t)) == 1,...
lambda4(:,n*t) >= 0,...
sum(lambda5(:,n*t)) == 1,...
lambda5(:,n*t) >= 0,...
sum(lambda6(:,n*t)) == 1,...
lambda6(:,n*t) >= 0,...
sum(lambda7(:,n*t)) == 1,...
lambda7(:,n*t) >= 0,...
sum(lambda8(:,n*t)) == 1,...
lambda8(:,n*t) >= 0,...
z1(n,t) == lambda1(:,n*t)'*fi,...
z2(n,t) == lambda2(:,n*t)'*fi,...
z3(n,t) == lambda3(:,n*t)'*fi,...
z4(n,t) == lambda4(:,n*t)'*fi,...
z5(n,t) == lambda5(:,n*t)'*fi,...
z6(n,t) == lambda6(:,n*t)'*fi,...
z7(n,t) == lambda7(:,n*t)'*fi,...
z8(n,t) == lambda8(:,n*t)'*fi,...
x1(n,t) == lambda1(:,n*t)'*xi,...
x2(n,t) == lambda2(:,n*t)'*xi,...
x3(n,t) == lambda3(:,n*t)'*xi,...
x4(n,t) == lambda4(:,n*t)'*xi,...
x5(n,t) == lambda5(:,n*t)'*xi,...
x6(n,t) == lambda6(:,n*t)'*xi,...
x7(n,t) == lambda7(:,n*t)'*xi,...
x8(n,t) == lambda8(:,n*t)'*xi,...
];
end
end
Constraint = [];
for n = 1:N
for t = 1:T
Constraint = [Constraint,...
P(n, t) == z1(n, t) - z2(n, t) + z3(n, t) - z4(n, t) ,...
Q(n, t) == -z5(n, t) + z6(n, t) + z7(n, t) - z8(n, t),...
];
end
end
Constraint = [Constraint,...
V==Vr+1i*Vi,...
I==Ir+1i*Ii,...
I == (Y*V),...%.*Ysum.*Loadsum;
Vr(1, :) == 1,... % 1<=
Vi(1, :) == 0,... % 0<=
%with EV
P(2, :) == DA(10:T+9),...
P(3, :) == DA(10:T+9)/2,...
Q(2, :) == DR(10:T+9),...
Q(3, :) == DR(10:T+9)/2,...
0.9<=Vr(:,:)<=1.1,...
-1.1<=Vi(:,:)<=1.1,...
];
%% Cost equation
L = sum(P(1, :));
%% Solution
ops = sdpsettings('solver','cplex');
tic
prob = optimize([Constraint, F], L, ops);
toc
Anyone knows something related to this?
Thanks
Write a function called day_diff that takes four scalar positive integer inputs, month1, day1, month2, day2. These represents the birthdays of two children who were born in 2015. The function returns a positive integer scalar that is equal to the difference between the ages of the two children in days. Make sure to check that the input values are of the correct types and they represent valid dates. If they are erroneous, return -1. An example call to the function would be:
dd = day_diff(1,30,2,1);
which would make dd equals 2. You are not allowed to use the built-in function datenum or datetime.
function dd = day_diff(month1, day1, month2, day2)
if (month1 && month2 > 0) || (month1 && month2 <= 12)
if month1 == 1 && month2 == 1
if day1 == day2
total1 = day1;
total2 = day2;
elseif day1 ~= day2
total1 = max(day1,day2);
total2 = min(day1,day2);
end
elseif month1 == 1 && month2 == 2
total1 = day1;
total2 = day2 + 31;
elseif (month1 == 2 && day1 <= 28) && month2 == 1
total1 = day1 + 31;
total2 = day2;
elseif month1 == 1 && month2 == 12
total1 = day1;
total2 = day2 + 334;
elseif month1 == 2 && month2 == 3
total1 = day1 + 31;
total2 = day2 + 59;
elseif month1 == 7 && month2 == 9
total1 = day1 + 181;
total2 = day2 + 243;
elseif month1 == 1 && month2 == 4
total1 = day1;
total2 = day2 + 90;
else
dd = -1;
return
end
end
if (day1<1 || day1>31) || (month1<1 || month1>12)
dd = -1;
return
elseif day2<1 || day2>31 || (month2<1 || month2>12)
dd = -1;
return
else
dd = (max(total1,total2)) - (min(total1,total2));
end
My function performs well unless I have a vector as an input argument (like (12,3,[3,4],5). I tried using ~isscalar but it doesn't seem to work the way I did it.
Thanks!.
I'd do something like:
function dd = day_diff(varargin)
if nargin~= 4 || any(~cellfun(#isscalar,varargin))
dd = -1; return;
end
month1 = varargin{1};
day1 = varargin{2};
month2 = varargin{3};
day2 = varargin{4};
... rest of your code
To check if any of the input is not an array you can do:
if any([length(month1)~=1, length(month2)~=1, length(day1)~=1, length(day2)~=1;])
dd=-1;
return;
end
Solution is not correct. For example if you will use (8, 29, 8 25) you must have 4 days but in your programm you will have -1. All programm was written with a lot of wrong
I have create uitable in Matlab with drop downmenu.
somehow the drop down menu doesn't get updated with switch/case
I tried substituting the switch/case with if else condition.
the drop down menu gets updated but it doesn't give me the desired output!
to simulate please run the code below
Any idea or pointers ?
function [] =foouitable()
f = figure('Position',[100 100 400 150]);
% Column names and column format
columnname = {'Available','Options','SubOptions'};
columnformat = {'logical','bank',{'CheckBox' 'SelectSubOptions'}};
% Define the data
d = {false 'Reconstruction' 'CheckBox';...
false 'Segmentation' 'CheckBox';...
false 'ComputerTomography' 'CheckBox';...
false, 'UltraSound', 'CheckBox';...
false, 'AcousticEmission', 'CheckBox'};
% Create the uitable
t = uitable('Data', d,...
'ColumnWidth', {70 120 100},...
'ColumnName', columnname,...
'ColumnFormat', columnformat,...
'ColumnEditable', [true false true],...
'RowName',[],...
'CellEditCallback',#edit)
set(t,'Tag','Config_table');
function edit(src,evt)
if evt.Indices(2) == 1
modifyPopup( src)
end
end
% Set width and height
t.Position(3) = t.Extent(3);
t.Position(4) = t.Extent(4);
function modifyPopup(src)
id_group_1 = {'A.1';'A.2';'A.3'};
id_group_2 = {'B.1';'B.2';'B.3'};
id_group_3 = {'C.1';'C.2';'C.3'};
id_group_4 = {'D.1';'D.2';'D.3'};
id_group_5 = {'E.1';'E.2';'E.3'};
id_default = {'CheckBox'};
config_data = get(src,'Data');
selector = config_data(1:5,1);
selector = cell2mat(selector);
config_format = get(src,'ColumnFormat');
if isequal(selector(1),1)
config_format{3} = id_group_1';
elseif isequal(selector(2),1)
config_format{3} = id_group_2';
elseif isequal(selector(3),1)
config_format{3} = id_group_3';
elseif isequal(selector(4),1)
config_format{3} = id_group_4';
elseif isequal(selector(5),1)
config_format{3} = id_group_5';
else
config_format{3} = id_default;
end
set(src,'Data',config_data);
set(src,'ColumnFormat',config_format);
end
end
Thanks in advance!
Thanks for the hint excaza. I implemented it
But the id_group_1 now consist of a 3x3 char array in one of its rows.
[1] 'Reconstruction' [3x3 char]
[0] 'Segmentation' 'CheckBox'
[0] 'ComputerTomography' 'CheckBox'
[0] 'UltraSound' 'CheckBox'
[0] 'AcousticEmission' 'CheckBox'
enter image description here
as set(src,'Data',config_data); does not permit a cell array. At the moment it seems like I cannot avoid using config_format inside the if/else condition!!
function modifyPopup(src)
id_group_1 = {true 'Reconstruction'...
['A.1'; 'A.2'; 'A.3'];...
false 'Segmentation' 'CheckBox';...
false 'ComputerTomography' 'CheckBox';...
false, 'UltraSound', 'CheckBox';...
false, 'AcousticEmission', 'CheckBox'};
id_group_2 = {false 'Reconstruction' 'CheckBox';...
true 'Segmentation' ['B.1'; 'B.2'; 'B.3'];...
false 'ComputerTomography' 'CheckBox';...
false, 'UltraSound', 'CheckBox';...
false, 'AcousticEmission', 'CheckBox'};
id_group_3 = {false 'Reconstruction' 'CheckBox';...
false 'Segmentation' 'CheckBox';...
true 'ComputerTomography'...
['C.1'; 'C.2'; 'C.3'];...
false, 'UltraSound', 'CheckBox';...
false, 'AcousticEmission', 'CheckBox'};
id_group_4 = {false 'Reconstruction' 'CheckBox';...
false 'Segmentation' 'CheckBox';...
false 'ComputerTomography' 'CheckBox';...
true, 'UltraSound',...
['D.1'; 'D.2'; 'D.3'];...
false, 'AcousticEmission', 'CheckBox'};
id_group_5 = {false 'Reconstruction' 'CheckBox';...
false 'Segmentation' 'CheckBox';...
false 'ComputerTomography' 'CheckBox';...
false, 'UltraSound', 'CheckBox';...
true, 'AcousticEmission', ['E.1'; 'E.2'; 'E.3']};
id_default = d;
config_data = get(src,'Data');
selector = config_data(1:5,1);
selector = cell2mat(selector);
config_format = get(src,'ColumnFormat')
if isequal(selector(1),1)
config_data = id_group_1;
elseif isequal(selector(2),1)
config_data = id_group_2;
elseif isequal(selector(3),1)
config_data = id_group_3;
elseif isequal(selector(4),1)
config_data = id_group_4;
elseif isequal(selector(5),1)
config_data = id_group_5;
else
config_data = id_default;
end
%set(src,'ColumnFormat', config_format)
set(src,'Data',config_data);
end
end
The value for the columns is 51 and 50, but when we use anything more than that the waitbar freezes due to index out of bound exception since its a large image and it wont fit in there, so the matlab dosent shut using the waitbar or anything. Need a way to shut the matlab when it encounters any error.
h = waitbar(0,'Progress','Name','Calculating Feature Heights...',...
'CreateCancelBtn','setappdata(gcbf,''canceling'',1)');
setappdata(h,'canceling',0); %initiallizes waitbar
s1 = size(A);
s2 = size(B);
if (s1(1) < s2(1))
n = s1(1);
else
n = s2(1); % ensures that bounds of i are within the bounds of both images
end
for i = 21:1:n % sets bounds for rows
if getappdata(h,'canceling') %checks for user pushing the cancel button on the waitbar
break
end
waitbar(i/(n-1),h) %progress bar
for j = 61:1:(m-60) % sets bounds for columns
if A(i,j) == A(i,j-1) %if adjacent pixels are the same,
Z(i,j) = Z(i,j-1); %they have the same height
disp(i,j) = disp(i,j-l);
elseif A((i), j) == B(i, j) && A(i,j) ~= A(i,j-1) && A(i,j-1) == B(i,j-1)
Z(i,j) = Z0; %condiions for pixels/features in the 'focal plane'
disp(i,j) = 0;
else
for l = 1:1:20 %sets scan range in rows for disparity
for k = 1:1:60 %sets disparity scan range in cols
if (A(i,j) == B(i-l, j-k) && B(i-l, j-k-1) == B(i-l, j-k))
Z(i,j) = Z(i-l,(j-k-1)); %allows for multipixel features
disp(i,j) = disp(i-l,(j-k-1));
break
elseif (A(i, j) == B(i-l, j-k) && B(i-l, j-k-1) ~= B(i-l, j-k))
xA = [i j];
xB = [i-l j-k];
d = xB-xA;
Z(i,j) = Z0 - (fl*shift)/sqrt((d(1)^2)+(d(2)^2));
disp(i,j) = sqrt((d(1)^2)+(d(2)^2));
break
elseif (A(i,j) == B(i-l, j+k) && B(i-l, j+k-1) == B(i-l, j+k))
Z(i,j) = Z(i-l,(j+k-1));
disp(i,j) = disp(i-l,(j+k-1));
break
elseif (A(i, j) == B(i-l, j+k) && B(i-l, j+k-1) ~= B(i-l, j+k))
xA = [i j];
xB = [i-l j+k];
d = xB-xA;
Z(i,j) = Z0 - (fl*shift)/sqrt((d(1)^2)+(d(2)^2));
disp(i,j) = sqrt((d(1)^2)+(d(2)^2));
break
else
continue
end
end
end
end
end
end
delete(h)
Use a try/catch block.
try
% whatever that might error
catch
delete(h)
end
I want to do a seam carving in an image using Matlab.
If i do it using only scripts and not by calling the function,it works.The problem comes when i make the script as a function and try to call it
The code i use
Final Script: Its the script i use to call the function over and over so i get the result
clear all;
close all;
%Image = 'mall.jpg';
Image_Array = imread('mall.jpg');
%We keep the dimension of the image array
[Row_Count,Column_Count,Colour] = size(Image_Array);
Image = double(Image_Array)/255;
Image = rgb2gray(Image);
for i = 1 : 10
[Image_Array,Row_Count,Column_Count]=Vertical_Seam(Image_Array,Row_Count,Column_Count);
imshow(Image_Array);
end
Vertical Seam: its the script i use to do the work of seaming.This is the file i would like to make it a function and call it in the Finalscript but i dont know how(You will notice that i tried to make a fucntion but as it didnt work i put it inside % )
%function [Final_Image,Row_Count,Column_Count] = Vertical_Seam(Image_Array,Row_Count,Column_Count)
Image_Array = imread('mall.jpg');
%We keep the dimension of the image array
[Row_Count,Column_Count,Colour] = size(Image_Array);
Image = double(Image_Array)/255;
Image = rgb2gray(Image);
for repeat = 1 : 10
%Calculate the energy
[E_x,E_y] = gradient(Image);
Energy = abs(E_x) + abs(E_y);
%We run the image and keep the minimum sum-energy in Image_Array
Cumulative_Energy = zeros(Row_Count,Column_Count);
% i represent rows
% j represent columns
for i = 1 : Row_Count
for j = 1 : Column_Count
if i==1
Cumulative_Energy(i,j) = Energy(i,j);
elseif i>1
if j==1
temp1 = [Cumulative_Energy(i-1,j),Cumulative_Energy(i-1,j+1)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp1);
elseif j>1 && j<Column_Count
temp2 = [Cumulative_Energy(i-1,j-1),Cumulative_Energy(i-1,j),Cumulative_Energy(i-1,j+1)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp2);
elseif j==Column_Count
temp3 = [Cumulative_Energy(i-1,j-1),Cumulative_Energy(i-1,j)];
Cumulative_Energy(i,j) = Energy(i,j) + min(temp3);
end
end
end
end
%Find the minimum value from last column
temp_min = Cumulative_Energy(Row_Count,1);
point_min = 1;
for j = 2 : Column_Count
if Cumulative_Energy(Row_Count,j)<temp_min
temp_min = Cumulative_Energy(Row_Count,j);
point_min = j;
end
end
%Start from the minimum value from last column and backtrack to find the minimum seam
%We need an array to keep minimum energy in every row while
%backtracking
Keep_Seam = zeros(Row_Count,1);
Keep_Seam(Row_Count) = temp_min;
%We need also the point of the minimum energy (the number of column)
Keep_Point = zeros(Row_Count,1);
Keep_Point(Row_Count) = point_min;
%Paint the pixel
Image_Array = Image_Colouring(Image_Array,Row_Count,point_min);
for i = Row_Count : -1 : 1
if i==1
if point_min==1
Tmp1 = [Cumulative_Energy(i,point_min),Cumulative_Energy(i,point_min+1)];
Keep_Seam(i) = min(Tmp1);
if min(Tmp1)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min==Column_Count
Tmp2 = [Cumulative_Energy(i,point_min-1),Cumulative_Energy(i,point_min)];
Keep_Seam(i) = min(Tmp2);
if min(Tmp2)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min-1;
point_min = point_min-1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min>1 && point_min<Column_Count
Tmp3 = [Cumulative_Energy(i,point_min-1),Cumulative_Energy(i,point_min),Cumulative_Energy(i,point_min+1)];
Keep_Seam(i) = min(Tmp3);
if min(Tmp3)==Cumulative_Energy(i,point_min)
Keep_Point(i) = point_min;
elseif min(Tmp3)==Cumulative_Energy(i,point_min-1)
Keep_Point(i) = point_min-1;
point_min = point_min-1;
elseif min(Tmp3)==Cumulative_Energy(i,point_min+1)
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
end
elseif i==Row_Count || i<Row_Count
if point_min==1
Temp1 = [Cumulative_Energy(i-1,point_min),Cumulative_Energy(i-1,point_min+1)];
Keep_Seam(i) = min(Temp1);
if min(Temp1)== Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min==Column_Count
Temp2 = [Cumulative_Energy(i-1,point_min-1),Cumulative_Energy(i-1,point_min)];
Keep_Seam(i) = min(Temp2);
if min(Temp2)==Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
else
Keep_Point(i) = point_min-1;
point_min = point_min-1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
elseif point_min>1 && point_min<Column_Count
Temp3 = [Cumulative_Energy(i-1,point_min-1),Cumulative_Energy(i-1,point_min),Cumulative_Energy(i-1,point_min+1)];
Keep_Seam(i) = min(Temp3);
if min(Temp3)==Cumulative_Energy(i-1,point_min)
Keep_Point(i) = point_min;
elseif min(Temp3)==Cumulative_Energy(i-1,point_min-1)
Keep_Point(i) = point_min-1;
point_min = point_min-1;
elseif min(Temp3)==Cumulative_Energy(i-1,point_min+1)
Keep_Point(i) = point_min+1;
point_min = point_min+1;
end
Image_Array = Image_Colouring(Image_Array,i,point_min);
end
end
end
imshow(Image_Array);
Image_Seam = zeros(Row_Count,Column_Count-1,Colour,'uint8');
for i = 1 : Row_Count
Image_Seam(i,:,:)=Image_Array(i,[1:Keep_Point(i)-1,Keep_Point(i)+1:Column_Count],:);
end
%imshow(Image_Seam);
Image_Array = Image_Seam;
end
% end
I would like to connect those two files so the Finalscript will call the Verticalseam as function and not as a script