What's wrong with my geometry definition that I have to mesh after using initmesh? - matlab

I can't get my geometry to mesh using MATLAB's mesher through the initmesh function. The error message is so brief that I can't identify the source of the error.
Here is the geometry definition :
%---------------- Geometric parameters ----------------------%
% Plate
Ep_plaque = 5e-3;
R_plaque = 20e-2;
% Coil
R_int_ind = 5e-2;
R_ext_ind = 15e-2;
Ep_ind = 2e-3;
Airgap = 2e-3;
% Airbox
R_air = 5*R_plaque;
H_air = 100*Ep_plaque;
% Plate
x1 = 0; y1 = 0;
x2 = R_plaque; y2 = 0;
x3 = R_plaque; y3 = Ep_plaque;
x4 = 0; y4 = Ep_plaque;
% Coil
x100 = R_int_ind; y100 = -Airgap-Ep_ind;
x101 = R_ext_ind; y101 = -Airgap-Ep_ind;
x102 = R_ext_ind; y102 = -Airgap;
x103 = R_int_ind; y103 = -Airgap;
% Air
x20 = 0; y20 = -H_air/2;
x21 = R_air; y21 = -H_air/2;
x22 = R_air; y22 = H_air/2;
x23 = 0; y23 = H_air/2;
% zone 1 : air
% zone 10 : plate
% zone 100 : coil
n_couches = 2;
Ep_layer = [5.2e-7 2.3e-6];
g_couche = [];
xs1 = R_plaque;
xs2 = 0;
xs3 = R_plaque;
xs4 = 0;
for i = 1:n_couches
ys1 = Ep_layer(i);
ys2 = ys1;
if i == 1
g_couche = [ g_couche ;
2 x1 x2 y1 y2 10 1 0 0 0;...
2 x2 xs1 y2 ys1 10 1 0 0 0;...
2 xs1 xs2 ys1 ys2 10 39 0 0 0;...
2 xs2 x1 ys2 y1 10 0 0 0 0;...
];
elseif i == n_couches
ys3 = Ep_layer(1);
ys4 = ys3;
g_couche= [ g_couche ;
2 xs3 xs1 ys3 ys1 39 1 0 0 0;...
2 xs1 xs2 ys1 ys2 39 50 0 0 0;...
2 xs2 xs4 ys2 ys4 39 0 0 0 0;...
] ;
end
end
g_plaque = [2 xs1 x3 ys1 y3 50 1 0 0 0; ...
2 x3 x4 y3 y4 50 1 0 0 0;...
2 x4 xs2 y4 ys2 50 0 0 0 0;...
];
g_ind_air = [2 x100 x101 y100 y101 100 1 0 0 0;...
2 x101 x102 y101 y102 100 1 0 0 0;...
2 x102 x103 y102 y103 100 1 0 0 0;...
2 x103 x100 y103 y100 100 1 0 0 0;...
2 x20 x21 y20 y21 1 0 0 0 0;...
2 x21 x22 y21 y22 1 0 0 0 0;...
2 x22 x23 y22 y23 1 0 0 0 0;...
2 x23 x4 y23 y4 1 0 0 0 0;...
2 x1 x20 y4 y20 1 0 0 0 0;...
];
g = [ g_couche.' g_ind_air.' g_plaque.'];
Here is how I call it and try to mesh it :
[p,e,t]=initmesh(g,'MesherVersion', 'R2013a');
You can visualize the geometry using :
pdegplot(g,'EdgeLabels','off');
When using the initmesh function I get the geometry error shown below. I checked the numbering of the different regions of my geometry and the definition of the matrices (especially for the plate).
Error using pdevoron
Geometry error.
Error in pderespe (line 30)
[p,t,c,h]=pdevoron(p,t,c,h,x,y,tol,Hmax,Hgrad);
Error in initmesh (line 166)
[p,e,t,c]=pderespe(g,p,e,t,c,Hmax,tol,tol2,Hmax,Hgrad);

Related

Undefined function or variable for 3dos mechanism

I tried to make a 3dof(3 degrees of freedom mechanism) in matlab but i get this error and i don't know why.
this is for a school project and i need to simulate a human finger.
the code is running normal but after i enter the values for the angles it says that A,B,C are
undefined and i don't know why
a1 = input('valuarea lui q1(grade):');
a2 = input('valuarea lui q2(grade):');
a3 = input('valuarea lui q3(grade):');
L1=35;
L2=45;
L3=30;
z = [-10 10];
plot(z,10);
grid ON;
O=[0;0;0;1];
m= linspace(pi/2,pi/2+a1*pi/180,100);
n = linspace(-pi/2,a2*pi/180,100);
k=linspace(-pi/2,a3*pi/180,100);
for a=1:100
[A1,B1,C1] = Transform(m(a),n(a),k(a),L1,L2,L3);
x = [O(1) A(1) B(1) C(1)];
y = [O(2) A(2) B(2) C(2)];
Cx(i)= C1(1);
Cy(i) = C1(2);
i=i+1;
Plot = plot(x,y,'r',...1
'LineWidth',1);
title('Sumularea unui deget');
plot(Cx,Cy,'--g',...
'LineWidth',1);
pause(0.075);
delete(Plot);
end
plot(x,y,'r',...
'LineWidth',3);
function [A,B,C ] = Transform( m,n,p,l1,l2,l3 )
P = [0;0;0;1];
T1 = [cos(m) -sin(m) 0 0;sin(m) cos(m) 0 0;0 0 1 0; 0 0 0 1];
T2 = [cos(n) -sin(n) 0 11;sin(n) cos(n) 0 0;0 0 1 0; 0 0 0 1];
T3 = [cos(p) -sin(p) 0 12;sin(p) cos(p) 0 0;0 0 1 0; 0 0 0 1];
T4 = [1 0 0 13;0 1 0 0; 0 0 1 0; 0 0 0 1];
A = T1*T2*P;
B = T1*T2*T3*P;
C = T1*T2*T3*T3*P;
end
In your main function, this is the first use of the variables:
x = [O(1) A(1) B(1) C(1)];
They are never written previously. Instead A1 is written, which is a different variable. I guess you mixed the two up.

How to simplify inputs for a decoder in verilog hdl

I'm making a decoder for an FPGA. The verilog code compiles, but the switches don't do anything. I quadruple-checked the pin assignments, and they are correct, so I guess there are some logic problems with my code.
The truth table is:
enable p a1 a0 y0 y1 y2 y3
0 0 0 0 1 0 0 0
0 0 0 1 0 1 0 0
0 0 1 0 0 0 1 0
0 0 1 1 0 0 0 1
0 1 0 0 0 1 1 1
0 1 0 1 1 0 1 1
0 1 1 0 1 1 0 1
0 1 1 1 1 1 1 0
1 x x x x z z z
This is my code:
module decoder2x4(
input enable,
input p,
input [1:0] a,
output [3:0] y
);
assign y[0] = (( ~p & ~a[1] & ~a[0]) | (~a[1] & a[0]) |(a[1] & ~a[0]) | ( p & a[1] & a[0]) ) & ~enable;
assign y[1] = (( ~p & ~a[1] & a[0]) | ( p & ~a[1] & ~a[0]) |( p & a[1] & ~a[0])| ( p & a[1] & a[0])) & ~enable;
assign y[2] = (( ~p & a[1] & ~a[0]) | ( p & ~a[1] & ~a[0]) |( p & ~a[1] & a[0])| ( p & a[1] & a[0])) & ~enable;
assign y[3] = (( ~p & a[1] & a[0]) | ( p & ~a[1] & ~a[0]) |( p & ~a[1] & a[0])| ( p & a[1] & ~a[0])) & ~enable;
endmodule
That is not the way you use HDL code, unless it is a school assignment.
I have copied this from the work Pierre.Vriens did to you table:
enable p a1 a0 y0 y1 y2 y3
0 0 0 0 1 0 0 0
0 0 0 1 0 1 0 0
0 0 1 0 0 0 1 0
0 0 1 1 0 0 0 1
0 1 0 0 0 1 1 1
0 1 0 1 1 0 1 1
0 1 1 0 1 1 0 1
0 1 1 1 1 1 1 0
1 x x x x z z z <<< Probably a typo here
The bottom line is a bit special so I use an if for that. The reset we can put in a case:
always #( * )
begin
if (enable)
{y0, y1, y2, y3} = 4'hz; // **
else
case ( {p, a1, a0 } )
3'b000 : {y0, y1, y2, y3} = 4'b1000;
3'b001 : {y0, y1, y2, y3} = 4'b0100;
3'b010 : {y0, y1, y2, y3} = 4'b0010;
3'b011 : {y0, y1, y2, y3} = 4'b0001;
3'b100 : {y0, y1, y2, y3} = 4'b0111;
3'b101 : {y0, y1, y2, y3} = 4'b1011;
3'b110 : {y0, y1, y2, y3} = 4'b1101;
3'b111 : {y0, y1, y2, y3} = 4'b1110;
endcase
end // always
Note that this copies the orignal table as much as possible, which reduced the possibilities of errors. An editor with column editing helps a lot!
** I assume you want all outputs to be 'z'. In your table you have xzzz.

Matlab CPLEX: add multiple SOCP constraints in cplexmiqcp

I have written my problem in MATLAB, using CPLEX as the solver. Due issues that are beyond my control (it is feasible), the CPLEX class API screws up when solving my problem. So, based on post found elsewhere on the internet, I am trying to solve using the toolbox API.
To solve my problem I need to use cplexmiqcp, which has the inputs:
cplexmiqcp(H,f,Aineq,bineq,Aeq,beq,l,Q,r,sostype,sosind,soswt,varLB,varUB,vartype,x0,options);
I have multiple SOCP constraints, and using the class API, I am able to define each of them using a structure, such as:
for n=1:numQCs
cplex.Model.qc(n).a=QC.a{n};
cplex.Model.qc(n).Q=QC.Q{n,1};
cplex.Model.qc(n).sense=QC.sense{n};
cplex.Model.qc(n).rhs=QC.rhs{n};
cplex.Model.qc(n).lhs=QC.lhs{n};
end
But how do I define multiple quadratic constraints for cplexmiqcp inputs? These are l,Q,r. When I try creating a structure as before, I get "Error: incorrect l,Q,r."
The documentation for the cplexmiqcp toolbox function is here. Quoting the documentation, for l, Q, and r, we have:
l: Double column vector or matrix for linear part of quadratic constraints
Q: Symmetric double matrix or row cell array of symmetric double matrices for quadratic constraints
r: Double or double row vector for rhs of quadratic inequality constraints
So, when we want to create one quadratic constraint, we can give a double column vector, a symmetric double matrix, and a double for l, Q, and r, respectively. When we want to create multiple quadratic constraints, we need to provide a matrix, a row cell array of symmetric double matrices, and a row vector for l, Q, and r, respectively.
Consider the following simple model:
Minimize
obj: x1 + x2 + x3 + x4 + x5 + x6
Subject To
c1: x1 + x2 + x5 = 8
c2: x3 + x5 + x6 = 10
q1: [ - x1 ^2 + x2 ^2 + x3 ^2 ] <= 0
q2: [ - x4 ^2 + x5 ^2 ] <= 0
Bounds
x2 Free
x3 Free
x5 Free
End
The MATLAB code would look like the following:
H = [];
f = [1 1 1 1 1 1]';
Aineq = []
bineq = []
Aeq = [1 1 0 0 1 0;
0 0 1 0 1 1];
beq = [8 10]';
l = [0 0;
0 0;
0 0;
0 0;
0 0;
0 0;];
Q = {[-1 0 0 0 0 0;
0 1 0 0 0 0;
0 0 1 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0], ...
[0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 -1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 0]};
r = [0 0];
sostype = [];
sosind = [];
soswt = [];
lb = [ 0; -inf; -inf; 0; -inf; 0];
ub = []; % implies all inf
ctype = []; % implies all continuous
options = cplexoptimset;
options.Display = 'on';
options.ExportModel = 'test.lp';
[x, fval, exitflag, output] = cplexmiqcp (H, f, Aineq, bineq, Aeq, beq,...
l, Q, r, sostype, sosind, soswt, lb, ub, ctype, [], options);
fprintf ('\nSolution status = %s \n', output.cplexstatusstring);
fprintf ('Solution value = %f \n', fval);
disp ('Values =');
disp (x');

How to multiple matrix column in next columns and set in a new matrix?

I want to make second matrix from the first matrix in MATLAB. Please see the following image:
AB = A and B
AC = A and C
BC = B and C
====>>>
code:
transactions={{'A','C'};{'A','B'};{'A','B','C'}};
items = unique([transactions{:}]); % A,B,C
for i = 1:size(transactions,1)
T(i,ismember(items,transactions{i,:})) = 1; %convert transactions to matrix
end
.
.
T1 = zeros(size(transactions,1), nchoosek(length(items),2));
for k=1:5
for i=1: length(items)
for j=i+1 : length(items)
z = bitand(T(k,i),T(k,j)))
% set z in matrix T1 %
end
end
end
How to set the new values and How to concate label in the result matrix?
Simply use Matlab's & operator (see documentation). Because Matlab is an interpreted language, it will automatically convert the 0's and 1's to a logical matrix.
For example:
X = [1 0 1; 1 0 0; 1 1 1; 1 0 0; 1 1 0];
Y(:,1) = X(:,1) & X(:,2);
Y(:,2) = X(:,1) & X(:,3);
Y(:,3) = X(:,2) & X(:,3);
Matrix Y will be of type logical and be the desired output:
>> Y
Y =
0 1 0
0 0 0
1 1 1
0 0 0
1 0 0
Alternatively, you can use a struct for readability:
>> X.A = [1 1 1 1 1];
>> X.B = [0 0 1 0 1];
>> X.C = [1 0 1 0 0];
>> Y.AB = X.A & X.B;
>> Y.AC = X.A & X.C;
>> Y.BC = X.B & X.C;
>> Y
Y =
AB: [0 0 1 0 1]
AC: [1 0 1 0 0]
BC: [0 0 1 0 0]
If you want to automate the process, you could do the following:
X.A = [1 1 1 1 1]';
X.B = [0 0 1 0 1]';
X.C = [1 0 1 0 0]';
names = fieldnames(X);
N = length(names);
combos = nchoosek(1:N,2);
for i=1:N
Y.(char([names(combos(i,1)) names(combos(i,2))])) = ...
X.(char(names(combos(i,1)))) & X.(char(names(combos(i,2))));
end
struct2table(Y)
Which gives the following:
ans =
AB AC BC
_____ _____ _____
false true false
false false false
true true true
false false false
true false false

Set specified indices to zero

I have two matrices (x1 and x2) with same size. I would like to use the elements equal to zero in x1 to put the sames elements to zero in x2.
The non working solution I've got now follows:
[i j] = find(x1 == 0);
x2(i,j) = 0;
I've also got a working solution which is:
[i j] = find(x1 == 0);
for n=1:length(i)
x2(i(n),j(n)) = 0;
end
thanks!
Try x2(x1 == 0) = 0. For example:
>> x1 = rand(5, 5)
x1 =
0.4229 0.6999 0.5309 0.9686 0.7788
0.0942 0.6385 0.6544 0.5313 0.4235
0.5985 0.0336 0.4076 0.3251 0.0908
0.4709 0.0688 0.8200 0.1056 0.2665
0.6959 0.3196 0.7184 0.6110 0.1537
>> x2 = randi(2, 5, 5) - 1
x2 =
0 1 1 0 1
0 1 0 0 1
1 1 1 1 0
0 1 1 1 1
1 0 0 0 0
>> x1(x2 == 0) = 0
x1 =
0 0.6999 0.5309 0 0.7788
0 0.6385 0 0 0.4235
0.5985 0.0336 0.4076 0.3251 0
0 0.0688 0.8200 0.1056 0.2665
0.6959 0 0 0 0