|
Examples of MatBasic calculation system application
MatBasic environment can be used for calculations of a wide range of complexity – from school-level sums to
sophisticated science problems solution. This part contains several examples of the environment application.
- Example: School-level sum "Snowflake"
- Program module of checking an algorithmic metering entry to a set polygonal area
- Program module of a frequency determination sensor
- Simulation model of a synchronous generator
Relay protection is purposed for reacting to monitored situations (injuries inside a monitored object) and
should be tuned out from alternative states (injuries not related to a monitored object). One of the most frequently used variants of
separating these two types of object states is setting of operation regions (adjustment features) of algorithmic parameters complex planes
(e.g. the plane of monitored phase impedance
) in a form of polygonal areas in a common case. An adjustment feature should include
meterings of monitored object states and exclude meterings of alternative states.
So it’s necessary to create a function which checks a complex algorithmic metering entry to a polygonal adjustment
feature. An adjustment feature is set as a complex numbers array containing operation region peaks first to last. Peaks are following one
another clockwise.
Function code:
%Point – complex point
%Region – region set by complex peaks
function [Result] PtInRegionCplx(Point,Region);
Eps = 1e-5;
NPoints = max(size(Region));
if (Region[1]!=Region[NPoints])
Region[++NPoints]=Region[1];
end
RightCount = 0;
LeftCount = 0;
for (n = 1:NPoints-1)
Rp=real(Point);
Ip=imag(Point);
R1=real(Region[n]);
I1=imag(Region[n]);
R2=real(Region[n+1]);
I2=imag(Region[n+1]);
if (Point==Region[n])
Result=1;
return;
end;
if (((Ip <= I1) & (Ip > I2)) | ((Ip > I1) & (Ip <= I2)))
XPoint = R1+(Ip-I1)*(R2-R1)/(I2-I1);
if (Rp < XPoint)
RightCount = RightCount+1;
elseif (Rp > XPoint)
LeftCount = LeftCount+1;
else
Result=1;
return;
end;
elseif ((abs(Ip-I1) < Eps) & (abs(Ip-I2) < Eps))
if ((Rp > R1) & (Rp > R2))
LeftCount = LeftCount+1;
elseif ((Rp < R1) & (Rp < R2))
RightCount = RightCount+1;
else
Result=1;
return;
end;
end;
end;
if ((mod(LeftCount,2)==0) | (mod(RightCount,2)==0))
Result = 0;
else
Result = 1;
end;
end; |
For checking the function working capacity the following code is used:
% Opening the file containing the set polygon points array
ifile=fopen("Data\Region0");
Rgn=fread(ifile);% - Data reading
posplot 1,1;
plot2d #RGN (trans(real(Rgn)),trans(imag(Rgn)),graphtype="#");
percetn=0.1;
rmin=min(real(Rgn));rmin-=percetn*(abs(rmin));
rmax=max(real(Rgn));rmax+=percetn*(abs(rmax));
imin=min(imag(Rgn));imin-=percetn*(abs(imin));
imax=max(imag(Rgn));imax+=percetn*(abs(imax));
% Creation, region entry check and points graphical output to a console
for (k=1:50)
Pnt[k]=rmin+rnd(abs(rmax)+abs(rmin))+1i*(imin+rnd(abs(imax)+abs(imin)));
Res[k]=PtInRegionCplx(Pnt[k],Rgn);
if (Res[k])
plot2d #RGN (real(Pnt[k]),imag(Pnt[k]),pointstyle="x",
graphcolor="red",linewidth=3,graphtype=".");
else
plot2d #RGN (real(Pnt[k]),imag(Pnt[k]),pointstyle="x",
graphcolor="blue",linewidth=3, graphtype=".");
end
end |
The code contains a random set of points within a set region provided by a built-in rnd
function, checking of a selected point entry inside the region set by Rgn variable and output of a point right on the graph.
Let’s use several files containing complex peaks arrays and get different results by changing a filename in an fopen function
delivered parameter.
 Convex area
Most frequently met characteristics of a relay actuation is made up in a form of quadrangular,
heptangular or hexangular convex areas and also in a form on an ellipse or a circle. Let us set a quadrangular convex domain in a "Region0" file:
[0+0i; -3+15i; 40+12i; 25-8i] |

Points within the area marked red. Conclusion – the algorithm is efficient for convex domains.
Let’s repeat a test involving more complex regions.
 Concave area
These areas can be met in relay protection less frequently but the point entry check function
should be usable for all regions. The "Region1" file contains an array of complex points of a concave area:
[-4-10i; -4-1i; -5+10i; -3-1i; 0+8i; 10+8i; 8-10i] |

As it is seen from the graph above the function has successfully accomplished the task of point
entry check involving a concave area. As a final test let’s try the function to check the point entry involving saw-toothed areas:
<< To the top
|