My Math Forum  

Go Back   My Math Forum > High School Math Forum > Geometry

Geometry Geometry Math Forum


Thanks Tree8Thanks
Reply
 
LinkBack Thread Tools Display Modes
May 8th, 2016, 05:26 PM   #11
jks
Senior Member
 
jks's Avatar
 
Joined: Jul 2012
From: DFW Area

Posts: 642
Thanks: 99

Math Focus: Electrical Engineering Applications
Here is the code. I did not bother to write subs or anything to streamline it. For the Cartesian coordinates integral note that:

$\displaystyle \large{ \begin{align*}
a&=radius_1 \\
b&= radius_2 \\
& \\
& \\
\frac{x^2}{a^2}+\frac{y^2}{b^2}&=1 \quad \text{take the derivative} \\
\frac{2x}{a^2}+\frac{2y}{b^2} \cdot \frac{dy}{dx}&=0 \\
\frac{dy}{dx}&=-\frac{x}{y} \cdot \frac{b^2}{a^2}
\end{align*}}$

Also note that the distance is calculated using:

$\displaystyle \large \text{distance}=\int_{x1}^{x2} \sqrt{1+ \left (\frac{dy}{dx} \right)^2} \ \ dx$

The output is:

1.1430648330286153
0.8603881198782394
0.5777114067278637

2.0487611764144185
1.8297036794251913

2.0487607291792025
1.8297072744644216

The first three values are the parametric t values for S1, T, and S2 which are output only for a check. The next two values are the calculated values of the elliptic integral for S2_T and S1_T, respectively. The last two values are the calculated values of the Cartesian coordinates integral for S2_T and S1_T, respectively. All compare nicely with the W|A values.

This problem is not easy, and I realize that I have posted a lot of information (I learned a lot myself too). If you have any questions, please post and I, or another forum member, will try to answer.


Code:
# ellipse lengths posted on 2016-05-08. quadrant 1 only!

include Math

alpha = Math::PI/5.0    # set here.
radius_1 = 8.0 
radius_2 = 5.0
L = 2.0

t_alpha = atan(radius_1*tan(alpha)/radius_2)  # get parametric t value.
x_alpha = radius_1*cos(t_alpha)
y_alpha = radius_2*sin(t_alpha)

slope = -radius_2/tan(t_alpha)/radius_1
slope_angle = atan(slope)
x_s1_L = x_alpha - L*cos(slope_angle)
y_s1_L = y_alpha - L*sin(slope_angle)
x_s2_L = x_alpha + L*cos(slope_angle)
y_s2_L = y_alpha + L*sin(slope_angle)

t_s1_alpha = atan(radius_1*y_s1_L/radius_2/x_s1_L)
t_s2_alpha = atan(radius_1*y_s2_L/radius_2/x_s2_L)

puts t_s1_alpha
puts t_alpha
puts t_s2_alpha

# integration using elliptic integral.
# s2.
delta_t = 0.00001
sum = 0.0
t = t_alpha

while t<t_s1_alpha
	m_sq = 1.0 - (radius_1/radius_2)**2.0
	sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t
end

puts 
puts sum

# s1.
sum = 0.0
t = t_alpha

while t>t_s2_alpha
	m_sq = 1.0 - (radius_1/radius_2)**2.0
	sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t -= delta_t
end

puts sum

# integrate using Cartesian coordinates. derivative is -x*radius_2^2/y/radius_1^2
# s1.
delta_x = 0.0001
x_s1 = radius_1*cos(t_s1_alpha)
y_s1 = radius_2*sin(t_s1_alpha) 
sum = 0.0
x = x_alpha

while x>=x_s1 
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
	sum += sqrt(1.0+deriv**2.0)*delta_x
  x -= delta_x
end

puts
puts sum

# s2.
x_s2 = radius_1*cos(t_s2_alpha)
y_s2 = radius_2*sin(t_s2_alpha)
sum = 0.0
x = x_alpha

while x<=x_s2 
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
	sum += sqrt(1.0+deriv**2.0)*delta_x
  x += delta_x
end

puts sum
Thanks from stgeorge
jks is offline  
 
May 11th, 2016, 01:34 PM   #12
Member
 
Joined: Jun 2012

Posts: 73
Thanks: 2

Dear @jks,

This is wonderful!!!
Thank you so much for your effort and your time!

I was wondering if I could ask you just one more question: I am not familiar with Ruby programming language. If would like to convert it to some other language, but what confuses me are two types of indentations which exist in your code. For example:

Code:
...
while t<t_s1_alpha
	m_sq = 1.0 - (radius_1/radius_2)**2.0
	sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t
end
...
In Ruby, what is the difference between this indentation:

Code:
	m_sq = 1.0 - (radius_1/radius_2)**2.0
	sin_sq = sin(t)**2.0
And this one:

Code:
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t
?

Thank you once again.

Last edited by skipjack; May 11th, 2016 at 03:31 PM.
stgeorge is offline  
May 11th, 2016, 07:41 PM   #13
jks
Senior Member
 
jks's Avatar
 
Joined: Jul 2012
From: DFW Area

Posts: 642
Thanks: 99

Math Focus: Electrical Engineering Applications
Hi stgeorge,

I apologize for the confusion. Unlike some other languages, indentation in Ruby is of no consequence in determining how it is parsed. The editor that I use sorts out my random tabs and spaces and presents the code nicely. Unfortunately, this did not translate well between the code tags in my post and I did not take the time to clean it up. Again, I apologize, and the code is re-posted below, hopefully in more readable form. (Good grief, it didn't take long at all, I should have done this in the first place.)

Code:
# ellipse lengths posted on 2016-05-08. quadrant 1 only!

include Math

alpha = Math::PI/5.0    # set here.
radius_1 = 8.0 
radius_2 = 5.0
L = 2.0

t_alpha = atan(radius_1*tan(alpha)/radius_2)  # get parametric t value.
x_alpha = radius_1*cos(t_alpha)
y_alpha = radius_2*sin(t_alpha)

slope = -radius_2/tan(t_alpha)/radius_1
slope_angle = atan(slope)
x_s1_L = x_alpha - L*cos(slope_angle)
y_s1_L = y_alpha - L*sin(slope_angle)
x_s2_L = x_alpha + L*cos(slope_angle)
y_s2_L = y_alpha + L*sin(slope_angle)

t_s1_alpha = atan(radius_1*y_s1_L/radius_2/x_s1_L)
t_s2_alpha = atan(radius_1*y_s2_L/radius_2/x_s2_L)

puts t_s1_alpha
puts t_alpha
puts t_s2_alpha

# integration using elliptic integral.
# s2.
delta_t = 0.00001
sum = 0.0
t = t_alpha

while t<t_s1_alpha
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  in_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t
end

puts 
puts sum

# s1.
sum = 0.0
t = t_alpha

while t>t_s2_alpha
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t -= delta_t
end

puts sum

# integrate using Cartesian coordinates. derivative is -x*radius_2^2/y/radius_1^2
# s1.
delta_x = 0.0001
x_s1 = radius_1*cos(t_s1_alpha)
y_s1 = radius_2*sin(t_s1_alpha) 
sum = 0.0
x = x_alpha

while x>=x_s1 
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x -= delta_x
end

puts
puts sum

# s2.
x_s2 = radius_1*cos(t_s2_alpha)
y_s2 = radius_2*sin(t_s2_alpha)
sum = 0.0
x = x_alpha

while x<=x_s2 
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x += delta_x
end

puts sum
Thanks from stgeorge
jks is offline  
May 12th, 2016, 03:09 PM   #14
Member
 
Joined: Jun 2012

Posts: 73
Thanks: 2

Dear @jks,

Thank you once again!!
I owe you a beer for all the help you gave me!

I tried to replicate your Ruby script to python, but for some reason, I am not getting the same result as you for "sum" variable (all four times). The t_s1_alpha, t_alpha and t_s2_alpha are ok, though.

Does Ruby's division character: "/" represent float division?
stgeorge is offline  
May 12th, 2016, 03:43 PM   #15
jks
Senior Member
 
jks's Avatar
 
Joined: Jul 2012
From: DFW Area

Posts: 642
Thanks: 99

Math Focus: Electrical Engineering Applications
Hi stgeorge,

First of all, I am hoping that you found that I was a bit too aggressive in reformatting the code in the s2 integration and 'in_sq' should be 'sin_sq' (good grief, again).

Yes, "/" represents float division as long as at least one of the numbers or variables is a Float (a member of the Float class). If both, or all are integers (class Fixnum), it is integer division.

Example outputs:

3.5/7.0 = 0.5
3.5/7 = 0.5
3/7 = 0

21.0/7/2 = 1.5
21/7/2 = 1

Also note that, for example 3.5/7.0/2.0 = 0.25 so it is interpreted as:

$\displaystyle \large \frac{3.5}{7.0 \cdot 2.0}=0.25$

and NOT

$\displaystyle \large \frac{3.5}{\frac{7.0}{2.0}}=\frac{3.5 \cdot 2.0}{7.0}=1.0$

Is this perhaps the reason that your answers are different?
Thanks from stgeorge
jks is offline  
May 12th, 2016, 04:26 PM   #16
Member
 
Joined: Jun 2012

Posts: 73
Thanks: 2

Once again thank you for the reply jks.

Quote:
First of all, I am hoping that you found that I was a bit too aggressive in reformatting the code in the s2 integration and 'in_sq' should be 'sin_sq' (good grief, again).
No, I did not notice that. I just tried to replicate the code from your reply #11 as it was.

Quote:
Yes, "/" represents float division as long as at least one of the numbers or variables is a Float (a member of the Float class). If both, or all are integers (class Fixnum), it is integer division.
You used floats everywhere so I guess this won't be a problem for me, when I am porting it to python

Quote:
Also note that, for example 3.5/7.0/2.0 = 0.25 so it is interpreted as:
Yes, I think python calculates the same like you presented.


I tried to replicate the code from your reply #13, and it looks I did not succeed. This is what I am getting printed:
Quote:
t_s1_alpha: 1.14306483303
t_alpha: 0.860388119878
t_s2_alpha: 0.577711406728

sum: 0.0
sum: 0.0

sum: 0.0
sum: 0.0
Which is incorrect.

Here is the python code. I am using 'for' loops instead, but they have the same purpose as your 'while' ones. It's just 'while' loops can sometimes crash my python interpreter, which I am trying to avoid.
Jks I thank you for your help, the code and your time.
I guess you can not do anything more to help me. I will try these days to see if I am making a mistake somewhere in the replicated python code. I will post the current one in here, just in case somebody notices what's wrong.
Thank you once again. I owe you two beers now!

Code:
# ellipse lengths posted on 2016-05-08. quadrant 1 only!
# ported from Ruby code by "jks" user from: http://mymathforum.com/geometry/330993-find-length-parts-ellipse-2.html 

from math import *

alpha = pi/5.0    # set here.
radius_1 = 8.0 
radius_2 = 5.0
L = 2.0

t_alpha = atan(radius_1*tan(alpha)/radius_2)  # get parametric t value.
x_alpha = radius_1*cos(t_alpha)
y_alpha = radius_2*sin(t_alpha)

slope = -radius_2/tan(t_alpha)/radius_1
slope_angle = atan(slope)
x_s1_L = x_alpha - L*cos(slope_angle)
y_s1_L = y_alpha - L*sin(slope_angle)
x_s2_L = x_alpha + L*cos(slope_angle)
y_s2_L = y_alpha + L*sin(slope_angle)

t_s1_alpha = atan(radius_1*y_s1_L/radius_2/x_s1_L)
t_s2_alpha = atan(radius_1*y_s2_L/radius_2/x_s2_L)

print "t_s1_alpha: ", t_s1_alpha
print "t_alpha: ", t_alpha
print "t_s2_alpha: ", t_s2_alpha

# integration using elliptic integral.
# s2.
delta_t = 0.00001
sum = 0.0
t = t_alpha

for num1 in range(1000):
  if (t<t_s1_alpha):
      break
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  in_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t

print " " 
print "sum: ", sum

# s1.
sum = 0.0
t = t_alpha

for num2 in range(1000):
  if (t>t_s2_alpha):
      break
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t -= delta_t

print "sum: ", sum

# integrate using Cartesian coordinates. derivative is -x*radius_2^2/y/radius_1^2
# s1.
delta_x = 0.0001
x_s1 = radius_1*cos(t_s1_alpha)
y_s1 = radius_2*sin(t_s1_alpha) 
sum = 0.0
x = x_alpha

for num3 in range(1000):
  if (x>=x_s1):
    break
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x -= delta_x

print " "
print "sum: ", sum

# s2.
x_s2 = radius_1*cos(t_s2_alpha)
y_s2 = radius_2*sin(t_s2_alpha)
sum = 0.0
x = x_alpha

for num4 in range(1000):
  if (x<=x_s2):
      break
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x += delta_x

print "sum: ", sum
stgeorge is offline  
May 12th, 2016, 04:40 PM   #17
jks
Senior Member
 
jks's Avatar
 
Joined: Jul 2012
From: DFW Area

Posts: 642
Thanks: 99

Math Focus: Electrical Engineering Applications
Hi again,

I just installed Python on my computer and I 'crash coursed' it (very basic). I only performed the S1 to T integrations (elliptic and Cartesian) and I came up with values similar to those of the Ruby script:

EDIT: Sorry, I was cutting and pasting erroneously. I did actually mean S1 to T; Have I said 'good grief' in this thread before?

1.1430648331

0.860388119964

0.577711406822

2.04876117647

2.04876072913

Does this code work similarly for you?

I will try to review your code in the meantime.

Code:
# ellipse lengths posted on 2016-05-08. quadrant 1 only!
# changed to Python.

from math import *

alpha = 3.141592654/5.0    # set here. note I just used 3.141592654 for Pi.
radius_1 = 8.0 
radius_2 = 5.0
L = 2.0

t_alpha = atan(radius_1*tan(alpha)/radius_2)  # get parametric t value.
x_alpha = radius_1*cos(t_alpha)
y_alpha = radius_2*sin(t_alpha)

slope = -radius_2/tan(t_alpha)/radius_1
slope_angle = atan(slope)
x_s1_L = x_alpha - L*cos(slope_angle)
y_s1_L = y_alpha - L*sin(slope_angle)
x_s2_L = x_alpha + L*cos(slope_angle)
y_s2_L = y_alpha + L*sin(slope_angle)

t_s1_alpha = atan(radius_1*y_s1_L/radius_2/x_s1_L)
t_s2_alpha = atan(radius_1*y_s2_L/radius_2/x_s2_L)

print t_s1_alpha, "\n"
print t_alpha, "\n"
print t_s2_alpha, "\n"

# integration using elliptic integral.
# s1.
delta_t = 0.00001
sum = 0.0
t = t_alpha

while t < t_s1_alpha:
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  sin_sq = sin(t)**2.0 
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t

print sum, "\n"

# integrate using Cartesian coordinates. derivative is -x*radius_2^2/y/radius_1^2
# s1.
delta_x = 0.0001
x_s1 = radius_1*cos(t_s1_alpha)
y_s1 = radius_2*sin(t_s1_alpha) 
sum = 0.0
x = x_alpha

while x >= x_s1: 
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x -= delta_x

print sum, "\n"

Last edited by jks; May 12th, 2016 at 04:58 PM.
jks is offline  
May 12th, 2016, 04:51 PM   #18
Member
 
Joined: Jun 2012

Posts: 73
Thanks: 2

Hi Jks,

Yes, your last code from reply #17 worked perfectly! I got the same numbers you got!

Maybe the confusion came due to my assumption that you corrected the 'in_sq' to 'sin_sq' in #s2, in your reply #13 code.

I thank you once again for your help and your time.
I truly owe you for this.

I can not help you with math issues, but if there is something I can help you with, send me a private message.

Kind regards from the other side of the Atlantic!!
Thanks from greg1313 and jks
stgeorge is offline  
May 12th, 2016, 05:01 PM   #19
Member
 
Joined: Jun 2012

Posts: 73
Thanks: 2

Quote:
EDIT: Sorry, I was cutting and pasting erroneously. I did actually mean S1 to T; Have I said 'good grief' in this thread before?
You might said it, but I did not understand you, as English is not my mother tongue.

Again thank you for the help and your spare time!
stgeorge is offline  
May 12th, 2016, 07:23 PM   #20
jks
Senior Member
 
jks's Avatar
 
Joined: Jul 2012
From: DFW Area

Posts: 642
Thanks: 99

Math Focus: Electrical Engineering Applications
Hi stgeorge,

Here is some code that gives the right answers for me and it uses 'for' loops since 'while' loops will sometimes crash your interpreter:

Code:
# ellipse lengths posted on 2016-05-08. quadrant 1 only!
# ported from Ruby code by "jks" user from: http://mymathforum.com/geometry/330993-find-length-parts-ellipse-2.html 
# (modified by jks 2016-05-09). see '#####' entries.

from math import *

alpha = pi/5.0    # set here.
radius_1 = 8.0 
radius_2 = 5.0
L = 2.0

t_alpha = atan(radius_1*tan(alpha)/radius_2)  # get parametric t value.
x_alpha = radius_1*cos(t_alpha)
y_alpha = radius_2*sin(t_alpha)

slope = -radius_2/tan(t_alpha)/radius_1
slope_angle = atan(slope)
x_s1_L = x_alpha - L*cos(slope_angle)
y_s1_L = y_alpha - L*sin(slope_angle)
x_s2_L = x_alpha + L*cos(slope_angle)
y_s2_L = y_alpha + L*sin(slope_angle)

t_s1_alpha = atan(radius_1*y_s1_L/radius_2/x_s1_L)
t_s2_alpha = atan(radius_1*y_s2_L/radius_2/x_s2_L)

print "t_s1_alpha: ", t_s1_alpha
print "t_alpha: ", t_alpha
print "t_s2_alpha: ", t_s2_alpha

# integration using elliptic integral.
# s1.                     ##### was s2, my error.
delta_t = 0.00001
sum = 0.0
t = t_alpha

range1 = int((t_s1_alpha - t_alpha)/delta_t + 0.5)   ##### added.

for num1 in range(range1):    ##### range1 instead of 1000.
#####  if (t<t_s1_alpha):   do not need (2 lines).
#####      break
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  sin_sq = sin(t)**2.0                      ##### sin_sq instead of in_sq, my error.
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t += delta_t

print " " 
print "sum: ", sum



# s2.          ##### was s1, my error.
sum = 0.0
t = t_alpha

range1 = int((t_alpha - t_s2_alpha)/delta_t + 0.5)   ##### added.

for num2 in range(range1):     ##### range1 instead of 1000.
#####  if (t>t_s2_alpha):    do not need (2 lines).
#####      break
  m_sq = 1.0 - (radius_1/radius_2)**2.0
  sin_sq = sin(t)**2.0
  sum += radius_2*(sqrt(1.0-m_sq*sin_sq)*delta_t)
  t -= delta_t

print "sum: ", sum

# integrate using Cartesian coordinates. derivative is -x*radius_2^2/y/radius_1^2
# s1.
delta_x = 0.0001
x_s1 = radius_1*cos(t_s1_alpha)
y_s1 = radius_2*sin(t_s1_alpha) 
sum = 0.0
x = x_alpha

range1 = int((x_alpha - x_s1)/delta_x + 0.5)   ##### added.

for num3 in range(range1):     ##### range1 instead of 1000.
#####  if (x>=x_s1):         do not need (2 lines).
#####    break
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x -= delta_x

print " "
print "sum: ", sum

# s2.
x_s2 = radius_1*cos(t_s2_alpha)
y_s2 = radius_2*sin(t_s2_alpha)
sum = 0.0
x = x_alpha

range1 = int((x_s2 - x_alpha)/delta_x + 0.5)   ##### added.

for num4 in range(range1):     ##### range1 instead of 1000.
#####  if (x<=x_s2):         do not need (2 lines).
#####      break
  y = radius_2*sqrt(1.0 - (x/radius_1)**2.0)
  deriv = -x*radius_2**2.0/y/radius_1**2.0
  sum += sqrt(1.0+deriv**2.0)*delta_x
  x += delta_x

print "sum: ", sum
jks is offline  
Reply

  My Math Forum > High School Math Forum > Geometry

Tags
ellipse, find, length, parts



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
n broken sticks produces 2n parts. Probability about some ways to bind that parts. beesee Probability and Statistics 3 January 20th, 2015 11:12 AM
Find real and imaginary parts szak1592 Complex Analysis 2 March 28th, 2013 09:47 AM
find length if angle and length is given... amin7905 Algebra 4 July 19th, 2012 01:08 PM
radius of curvature of ellipse? two parts? Vesperlynd Calculus 2 July 21st, 2011 09:29 PM
divide ellipse or circle by same length of segments losbellos Algebra 5 April 21st, 2010 02:52 PM





Copyright © 2019 My Math Forum. All rights reserved.