|
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
Task: To make up a program drawing a snowflake. Following values are given by user: number of units,
branches, coefficient of every branch unit decrease.
Algorithm:
- Verify the necessary parameters, declare them:
N number of units, P number of branches, L length of the inner unit branch,
K - coefficient of every unit decrease.
- Place a snowflake center in the origin of coordinates.
- Start drawing from the center of the snowflake: drawing a first interval (of L length), then if its not the last
unit draw the next unit interval (of length L/K) and continue till drawing of the last unit interval (L/Kn-1)
is finished.
- Draw the smallest snowflake at the last unit.
- Return to the next to last interval and draw the smallest snowflake unit.
- Continue these actions till the whole snowflake is drawn. This drawing logic is easily realized by recursion.
Listing of a branch drawing recursive function:
% fi - turn angle, depends on P
% Zc complex point, containing "parrent"-interval end coordinates
% drawn in function branches
function branch(Zc,K,P,N)
global fi;
z[1]=Zc[2];
z[2]=zz=(abs(Zc[2]-Zc[1])/K)*exp(1i*
(angle(real(Zc[2]-Zc[1]),imag(Zc[2]-Zc[1]))-PI));
if (N > 1)
for (n=1:P)
zz*=fi;
z[2]=zz+Zc[2];
plot2d #SNOWFLAKE (real(z),imag(z));
branch(z,K,P,N-1);
end
end
end |
Input data is declared in the very beginning of a program text file:
P=6;
K=4;
N=3;
L=10;
global fi;
fi=exp(1i*2*PI/P); |
The fi variable is declared global as far as it is used both in the function and in
the main program body. The program listing is following:
z[1]=0;
z[2]=L*1i;
posplot 1,1;
hldplot on; % Use this for quick output
for (k=1:P)
plot2d #SNOWFLAKE (real(z),imag(z),axisstyle="boxed",axis="equal");
branch(z,K,P,N);
z[2]*=fi;
end |
Setting different values of P, K, N and L variables gives us different snowflake
looks at the output (graphical console). For example, values mentioned above will give us a snowflake shown at the left and values
P=6, K=3, N=5, L=10 give us a snowflake shown at the right.
<< To the top
|