SCILAB code for searching the center of figure - scilab

*Hello everyone, I need your help, I'm looking for the center of the figure in green in terms of a point with these coordinates (X, Y) that I posted on the following link: https://imgur.com/6841jk4
I tried to apply Guldin's method? ? , look for the center of gravity, the moment of inertia but in vain.
could you help me and provide me the code in scilab, because I've been looking for the solution in terms of code and mathematical analysis for a long time.
you will find attached the scilab code.
enter code here
function y = h(x)
if x < 50 | 210 < x then
error("Out of range");
elseif x <= 90 then
y= -57.376067 +9.3746343*x -0.2175008*x^2 +0.0013792*x^3
//disp('50-90')
return;
elseif x <=100 then
y= 10330.932 -336.90229*x +3.6300206*x^2 -0.0128709*x^3;
//disp('90-100')
elseif x <= 130 then
y=-6387.7416 +164.65791*x -1.3855814*x^2 +0.0038478*x^3;
//disp('100-130')
return;
else
y = 5028.1996 -98.786888*x +0.640917*x^2 -0.0013484*x^3;
//disp('130-210')
end
endfunction
t=[50:210];
plot(t,feval(t,h),'r*')
l=[50 60 90 100 130 150 210]
k=[40 20 30 70 55 80 60]
plot(l,k,'d')
for i=[40 20 30 70 55 80 60]
teta=[0: 220]
beta=linspace(100,100,221)
plot(teta,beta,'*')
teta1=[100:160:221]
beta1=linspace(100,160-2*rand(),221)
plot2d3(teta1,beta1)
end
a=gca()
a.sub_ticks = [5,5]
a.grid_thickness = [0.05,0.05];
a.grid = [-1,-1]
a.grid_position = "foreground"
//a.grid_thickness = [0.05,0.05]
xgrid(0)
C=[50 60 90 100 130 150 210]
//for j=1:size(C,'c')
//C(j)
//if c(k)<=50 then
// (m=k+1& c(m))
J=numderivative(h,t) /*jacobien*/
//f=C(j)
// J=numderivative(h,i) /*jacobien*/
deff('[z] = h2(k)', 'z = h(k)-100');
// disp(C(j))
//for i=[157.56011:204.1084]
[x,fx,v]=fsolve([150,200],h2)
disp(x)
disp(fx)
plot(x,fx+100,)
disp(v)
//plot(x,h2,'d')
//end
//po=[50 60 90 100 130 150 210]
//co=[40 20 30 70 55 80 60]
lh=linspace(157.56011,204.1084);
lpo=feval(lh,h);
xfpoly(lh,lpo)
e=gce()
e.background=13

Related

Circle degrees loop in Angular Leaflet

I am trying to create circle degrees in Angular leaflet maps
In my first step i have applied formulas for 30 45 degrees and so on
as
const x30: number = p.x + (radius) * (Math.cos(Math.PI / 6));
const y30: number = p.y + (radius) * (Math.sin(Math.PI / 6));
const x45: number = p.x + (radius) * (Math.cos(Math.PI / 4));
const y45: number = p.y + (radius) * (Math.sin(Math.PI / 4));
but now i want to have a for loop that starts according to my selected values of drop-down
like if i select 10 then my loop should start from 10 and should have distance of 10 degree
means now i have to find (x,y) for 10 20 30 degrees and so on
now my question is that what should be the series of degrees that i should use in for loop?
or what should be the option to get degree form Math Library?
It sounds like your question is on converting degrees to radians. Recall that there are 2PI radians in a circle and 360 degrees. 2PIradians and 360degrees measure the same angle. So, 2PIradians = 360degrees. To convert from degrees to radians, you need to divide by 360 and then multiply by 2PI; equivalently, multiply by PI/180. Using this formula, we can calculate the corresponding angle measure in radians for any angle measure given in degrees. Here's a table:
degrees radians
0 (PI/180)*0 = 0
5 (PI/180)*5 = PI/36
10 (PI/180)*10 = PI/18
15 (PI/180)*15 = PI/12
20 (PI/180)*20 = PI/9
25 (PI/180)*25 = 5PI/36
30 (PI/180)*30 = PI/6
35 (PI/180)*35 = 7PI/36
40 (PI/180)*40 = 4PI/18
45 (PI/180)*45 = PI/4
50 (PI/180)*50 = 5*PI/18
55 (PI/180)*55 = 11*PI/36
60 (PI/180)*60 = PI/3
65 (PI/180)*65 = 13*PI/36
70 (PI/180)*70 = 7*PI/18
75 (PI/180)*75 = 15*PI/36
80 (PI/180)*80 = 4*PI/9
85 (PI/180)*85 = 17*PI/36
90 (PI/180)*90 = PI/2

How to compute floor(log2(5**x)) without floating point arithmetic or long integer computation

The problem is how do we compute the integer value of floor(log2(5^x)) without floating point arithmetic or long integer computation? I'm looking for a simple, efficient and mathematically elegant way.
Observations:
The formula is just the number of bits in 5**x (plus 1)
Attempts:
I tried to simplify it to:
floor(x*log2(5))
In my use case, x is not extremely large, probably just 1-100. While an elegant formula that works for small values would suffice me, I would be interested in a formula/algorithm that works for any value of x
I'm making a reference software implementation of universal numbers (type III). I want to make everything easily convertible to microcode by purely using bitwise and basic operations. This is one of the formulas i need to simplify.
As you correctly note, log2(5**x) == x * log2(5). log2(5) is a constant, which can be approximated to 2.3219281.
However, floats aren't allowed per the question. Not an issue!
log2_5 = 23219281;
scale = 10000000; // note log2_5/scale is the actual value
result = x * log2_5;
output = (result - (result % scale)) / scale;
By reducing result by result % scale, dividing it by scale will be an integer division, not a float.
for a simple, efficient and mathematically elegant way... floor(x*log2(5))
Since x has integer values 1 to 100, various tests can to made to find the "best" that uses an integer multiply and a divide by power_of_2.
f(x) = x*a integer_divide power_of_2
For
f(x) = floor(x*log2(5)) = floor(x*some_float_c) the value of some_float_c is bounded by 100 minimum and maximums below.
x f(x) mn mx
f(x)/x (f(x) + 1)/x
1 2 2.00000 3.00000
2 4 2.00000 2.50000
3 6 2.00000 2.33333
...
59 136 2.30508 2.32203
...
87 202 2.32184 2.33333
...
98 227 2.31633 2.32653
99 229 2.31313 2.32323
100 232 2.32000 2.33000
The maximum min is 2.32184 and the minimum max is 2.32203, :
2.32184... <= some_float_c < 2.32203...
Since we cannot use float, find some_integer/some_power_of_2
2.32184... <= some_integer/some_power_of_2 < 2.32203...
ceil(some_power_of_2 * 2.32184...) <= some_integer < floor(some_power_of_2 * 2.32203...)
min max
2.32184 2.32203
2 5 4
4 10 9
8 19 18
...
1024 2378 2377
2048 4756 4755
4096 9511 9511 < first one where min <= max
8192 19021 19022
So 9511/4096 is the "simplest" and is a "best" candidate.
f(x) = (x*9511) integer_divide_by_power_of_2 4096
// In C
unsigned y = (x*9511u) >> 12;
Here is a very rough approximation, but it can help if you want to obtain it mentally
5^3 = 125
2^7 = 128
So for raising to the power of n:
5^n ~~ 2^(7n/3)
So 5^12 is near 2^28 might require up to 29 bits.
It's a bit overestimated because 2^7 > 5^3, so 28 bits are enough, a good usage is to simply round the fraction upper.
If I evaluate in Smalltalk:
(1 to: 50) reject: [:i | (5 raisedTo: i) highBit = (i*7/3) ceiling].
I get:
#(31 34 37 40 43 46 49)
You see that the very simple formulation works up to 5^30 which is not that bad.

How to print output of .bas file to text

I am trying to print coordinate outputs of a program to a text file in order to use it in another program but I don't really know anything about GWBASIC and its my first time using MS-DOS. I need it to open a text file named plot.txt and print output there and save it without actually plotting on GWBASIC. Here is the program which I found in an old magazine.
810 REM MAKE A GLOBULAR
12 REM
14 R0=20: R2=R0*R0: R3=R2*R0
16 P1=3.14159265#
18 C0=P1*P1*R3/4
20 R1=R0/SQR(2)
22 XM=512: YM=512
24 X2=XM/2: Y2=YM/2: S=5
26 INPUT "HOW MANY STARS ";T
27 RANDOMIZE TIMER
28 CLS: REM CLEAR SCREEN
30 FOR I=1 TO T
32 C=C0*RND: R=R1
34 REM
36 REM NOW FIND R
38 FOR K=1 TO 5
40 GOSUB 100
42 R=R+(C-C1)/D
44 NEXT K
46 REM 3-DIMENSIONAL PLACE
48 X=RND-.5
50 Y=RND-.5
52 Z=RND-.5
54 S1=SQR(X*X+Y*Y+Z*Z)
56 IF S1>.5 THEN GOTO 48
58 REM POINT IS NOW IN SPHERE
60 R=R*S1: X=X*R: Y=Y*R: Z=Z*R
62 GOSUB 200
64 NEXT I
66 END
68 REM
100 REM NEWTON-RAPHSON ITERATION
105 A=R/R0
110 C1=ATN(A)*.5*R3
115 A=1+A*A
120 C1=C1+R*.5*R2/A
125 C1=P1*(C1-R*R2/(A*A))
130 D=4*P1*R*R/(A*A*A)
135 RETURN
140 REM
200 REM 2-DIMENSIONAL PLOT
203 SCREEN 9
205 X=X*S+X2: Y=Y*S+Y2
210 IF X<0 OR Y<0 THEN 225
215 IF X>=XM OR Y>=YM THEN 225
220 PSET(X,Y)
225 RETURN
230 REM ------------------------
240 REM APPEARED IN ASTRONOMICAL
250 REM COMPUTING, SKY & TELE-
260 REM SCOPE, APRIL, 1986
270 REM ------------------------
Here is a Python 3 paraphrase:
#globular.py
#Python paraphrase of model.bas from
#http://www.skyandtelescope.com/wp-content/uploads/model.bas
from math import pi, sqrt, atan
from random import uniform, random
#Global variables:
r0 = 20.0
r2 = r0**2
r3 = r0**3
c0 = pi**2*r3/4
r1 = r0/sqrt(2)
def NRI(c,r):
#Newton-Raphson Iteration
a = r/r0
c1 = atan(a)*0.5*r3
a = 1+a**2
c1 += r*0.5*r2/a
c1 = pi*(c1-r*r2/a**2)
d = 4*pi*r**2/a**3
return (c1,d)
def makeStars(t):
stars = []
for i in range(t):
c = c0*random()
r = r1
for k in range(5):
c1,d = NRI(c,r)
r += (c-c1)/d
while True:
x = uniform(-0.5,0.5)
y = uniform(-0.5,0.5)
z = uniform(-0.5,0.5)
s1 = sqrt(x**2 + y**2 + z**2)
if s1 <= 0.5: break
r *= s1
x *= r
y *= r
z *= r
stars.append((x,y,z))
return stars
def starsToFile(t,fname):
stars = makeStars(t)
f = open(fname,'w')
for star in stars:
print(*star, sep = ', ',file = f)
f.close()
I skipped the part about printing x and y and instead wrote a function makeStars to return a list of (x,y,z) tuples, as well as a related function which takes such an output and sends it to a text file. This last function is the only thing that used Python 3 instead of Python 2. If you are using Python 2 you can import Python 3's print function from the future.
Typing starsToFile(100,'stars.txt') in the Python shell gave me a text file which begins:
-0.32838465248713156, -0.3294895266926551, -1.2963580524762535
14.20224408569865, 1.4434961933043464, 6.450969593697097
1.6525937589658193, -0.24447292610082685, 1.0543647986350608
1.5707528567123823, 5.190972598268825, -2.0054790217091134
I don't have good 3-d scatter-plot graphing at my finger tips, but here is a screen shot of 50 points generated by the function and plotted using a computer algebra system called Derive:
Final remark: I wonder if there is a typo in the source code. The line
C0=P1*P1*R3/4
strikes me as suspicious since it is fairly rare in mathematics for pi to appear squared -- though it does happen. Maybe there should be only 1 factor of pi there (which would then have the effect of setting C0 proportional to the volume of the sphere of radius R0). On the other hand, I don't know exactly what is happening here, so I left it in. If the results seem problematic, you could maybe experiment with that line.
If you want a copy of the calculated coordinates simply add these lines:
1 OPEN "PLOT.TXT" FOR OUTPUT AS #1
65 CLOSE #1
221 PRINT #1, X + "," + Y
The program will work as before but in addition to this it outputs the coordinate to a file named plot.txt
Put them in an image with 640x350 size (that size is demanded by SCREEN 9) and you get the same result.

Mathematical mind boggler, very confusing possible new mathematical breakthrough [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I'm not trying to make a joke here but I am very confused I been trying to figure this out for like 6 hours straight now got about 20 notepads opened up here, 15 calculators and I cant crunch it I'm always getting too much excess in the end.
Lets explain some variables here we got to work with.
Say we got
2566 min points / 2566 max points
0 min xp / 4835 max xp
There is 2 types of jobs that need to use both variables (points and xp)
Job (1) subtracts 32 points per click and adds 72 xp per click.
Job (2) subtracts 10 points per click and adds 14 xp per click.
I'm trying to figure out how to calculate the excess properly. So it would waste the minimal amount of Job(1)'s to still have enough points to do as much Job(2)'s as it possibly can and still reach max xp.
Thats the thing I dont want to run Job1's until there are no more points left because in doing so, the Job1's will exceeds the maximum XP (2566) and I will never get to do any Job2's.
I want to get the maximum possible Job2's in then using proper calculation achieve or overflow the MaxXP of 2566 with Job1's to always achieve max XP. Pretty much my situation is that I need to get 2566 MaxXP to be able to continue completing jobs. While keeping that in mind I want to place most priority on job2's and only use Job1's to achieve the necessary MaxXP of 2566 to reset the min points to max to redo the process all over. I am trying to automate this.
Here is my equations
amountOfJob1s = (minPoints / 32)
amountOfJob2s = (minPoints / 10)
excessXP = (amountOfJob1s * 72) - maxXP
if excessXP < 0 then break
Results
mustDoJob1s = ???
mustDoJob2s = ???
Thank you if anyone can help me figure this out so I can put a good equation here I'd appreciate it.
Either this is not mathematically possible or I just can't crunch it I do believe I have enough variables.
Let job1 be the amount of job1 and job2 be the amount of job2. We are left with two equations and two unknowns:
job1 * 32 + job2 * 10 = 2566
job1 * 72 + job2 * 14 = 4835
So:
job1 = 45.683...
job2 = 110.411...
Given job1 as the higher xp/point ratio and you wanna go over 4835 xp, round job1 up, compute job2 and round it down.
job1 = 46
job1 * 32 + job2 * 10 = 2566
job2 = 109.4
job2 = 109
Check:
job1 * 32 + job2 * 10 = 2562 points
job1 * 72 + job2 * 14 = 4838 xp
Done.
Two unknowns is hardly a 'new mathematical breakthrough' :)
I assume you want to get as much "XP" as possible, while spending no more than 2566 "points" by "clicking" an integer number of times {n1, n2} on each of two "jobs". Here is the answer in Mathematica:
In[8]:= Maximize[{72 n1 + 14 n2, n1 >= 0, n2 >= 0,
32 n1 + 10 n2 <= 2566}, {n1, n2}, Integers]
Out[8]= {5956, {n1 -> 80, n2 -> 0}}
Or, maybe you need to spend exactly 2566 points? Then the best you can do is:
In[9]:= Maximize[{72 n1 + 14 n2, n1 >= 0, n2 >= 0,
32 n1 + 10 n2 == 2566}, {n1, n2}, Integers]
Out[9]= {5714, {n1 -> 78, n2 -> 7}}
Is this what you wanted?
Let a be the number of Job 1 and b the number of Job 2.
XP = 72 a + 14 b
P = 32 a + 10 b
You appear to want to solve for a and b, such that XP <= 4835, P <= 2566 and b is as large as possible.
72 a + 14 b <= 4835
32 a + 10 b <= 2566
b will be largest when a = 0, i.e.
b <= 4835 ÷ 14, => b <= 345
b <= 2566 ÷ 10, => b <= 256
As b must be both below 345 and 256, it must be below 256.
Substitute back in:
72 a + 14 × 256 <= 4835, => a <= ( 4835 - 14 × 256 ) ÷ 72, => a <= 17
32 a + 10 × 256 <= 2566, => a <= ( 2566 - 10 × 256 ) ÷ 32, => a <= 0
so a = 0, XP is 2560 and points used is 3584.
Alternatively, you can solve for the closest satisfaction of the two inequalities
72 a + 14 b <= 4835 (1)
32 a + 10 b <= 2566 (2)
b <= ( 2566 - 32 a ) ÷ 10 (3) rearrange 2
72 a <= 4835 - 1.4 ( 2566 - 32 a ) (4) subst 3 into 1
27.2 a <= 1242.6
a <= 45.68
so choose a = 45 as the largest integer solution, giving b = 112, XP is 4808, points used is 2560
For either of these, there's no computer programming required; if the constants associated with the two jobs change, then the formulas change.
For harder to solve examples, the relevant area of mathematics is called linear programming

How to achieve 'donut holes' with paths in Raphael

I'd like to draw a shape which has holes in it such that I can fill the shape it and not have the holes filled with that colour (leave them transparent).
According to the W3 path spec:
Compound paths (i.e., a path with multiple subpaths) are possible to allow effects such as "donut holes" in objects.
Can somebody please give a very simple example of how to perform this with a vector path in Raphael?
Thanks very much.
This turns out to be quite straightforward if you know the trick. For example this doesn't work:
paper.path("M 50 50 L 50 150 L 150 150 L 150 50 z" +
" M 75 75 L 75 125 L 125 125 L 125 75 z")
.attr("fill", "#f00");
But this does work*:
paper.path("M 50 50 L 50 150 L 150 150 L 150 50 z" +
" M 75 75 L 125 75 L 125 125 L 75 125 z")
.attr("fill", "#f00");
The difference is that for the donut to appear the the inner path has to have it's vertices drawn in reverse order to the outer path (ie. draw one clockwise, the other anti-clockwise). A tidbit I found on the text.xml.svg.devel archives.
(*) At least, it works in Chrome, Opera and Firefox 4.0 beta, but not in 3.6
To make this work in Firefox 3.6, you need to close the hole; i.e. make the coordinates join back to themselves when defining the inner boundary. Curiously, this doesn't appear necessary for the outer boundary.
paper.path("M 50 50 L 50 150 L 150 150 L 150 50 z" +
" M 75 75 L 125 75 L 125 125 L 75 125 L 75 75 z")
.attr("fill", "#f00");
Just a quick note to follow up on the comment - the clockwise/counter-clockwise concept might seem strange at first, but it's pretty standard throughout GIS / CAD technologies.
I think the correct way to do this is setting the attribute "fill-rule" to the value "evenodd". Take a look at the svg spec:
Don't try to set it with "Raphael.Element.attr()". It doesn't work. I use the jQuery.attr() function instead:
// assuming paper is a Raphael.Paper object
path = paper.path('Mx,y{some path commands for the main shape}Z'
+'Mx,y{some path commands for the hole}Z'
);
// this doesn't work
path.attr({'fill-rule': 'evenodd'});
// neither this
path.attr({fillRule: 'evenodd'});
// if you inspect the object returned by paper.path
// you can see it has a reference to the DOM element
console.debug(path)
// so a bit of jQuery and it's done
$(path[0]).attr('fill-rule', 'evenodd');
I have used this on complex paths with successful results.
For anyone looking to do circular donuts, great easy plugin Raphael-donut-plugin
Gist:
Raphael.fn.donut = function(x, y, innerRadius, outerRadius) {
y -= outerRadius;
return this.path('M'+x+' '+y+'a'+outerRadius+' '+outerRadius +
' 0 1 0 1 0m-1 '+
(outerRadius - innerRadius)+
'a'+innerRadius+' '+innerRadius+
' 0 1 1 -1 0');
};

Resources