Beijer Electronics (formerly QSI Corporation)http://www.qsiforums.com/ Long equations...imaginary numbers?http://www.qsiforums.com/viewtopic.php?f=6&t=4383 Page 1 of 3

Author:  aaronkeit [ Wed Nov 23, 2011 12:32 am ]
Post subject:  Long equations...imaginary numbers?

I am using a QTERM G55 terminal to send commands to a Galil motion controller. The user defines the parameters of a certain move(duration, distance, accel and decel) and the G55 figures out the appropriate speed command to send to the Galil. I have attached a JPG with an image of the three possible equations I can use to solve for peak velocity (Vs)when I know the duration(t), distance(d),initial speed(Vi), # of accel units(MA) and # of decel units(MD). They are all ridiculously long.

The second two of these equations use an imaginary number at some point, so I attempted to write out the first equation in Qlarity to see if this would be the appropriate equation for me. Alas, I get an illegal square root error when I use this equation, and I believe I have written it out properly in Qlarity format, so I guess this is not the equation I need. If the correct equation is one of the two which use an Imaginary number is there any way to write this out in Qlarity?

This is the equation without an imaginary number, written up in sections. I get an illegal square root error because the number being 'square-root-ed' is negative.
Code:
func click()

dim dur as float
dim dist as float
dim MA as float
dim MD as float
dim initspeed as float
dim p1 as float
dim p2 as float
dim p3 as float
dim p4 as float
dim p5 as float
dim p6 as float
dim p7 as float
dim p8 as float
dim p9 as float
dim p10 as float
dim p11 as float
MA=200
MD=300
dur=20
dist=1000
initspeed=0

p1=(-1/dur)*.26456684199469993
p2=(-2*power(dist,3))-(6*MA*power(dist,2))+(6*MD*power(dist,2))-(6*power(MD,2)*dist)+(12*MA*MD*dist)+(18*MA*dur*initspeed*dist)-(2*power(MA,3))+(2*power(MD,3))-(6*MA*power(MD,2))-(27*MA*power(dur,2)*power(initspeed,2))+(6*power(MA,2)*MD)+(18*power(MA,2)*dur*initspeed)-(18*MA*MD*dur*initspeed)
p3=4*power(6*(MA*dur*initspeed)-power(dist+MA-MD,2),3)
p4=SQRT(p3+power(p2,2))
p5=power((p2+p4),1/3)
p6=p1*p5
p7=.3333333333333333*(dist+MA-MD)/dur
p8=(.41997368329829105*((6*MA*dur*initspeed)-power((dist+MA-MD),2)))
p9=(dur*p5)
p10=p7+(p8/p9)
p11=p6+p10

endfunc

 Attachments: speed formulas with ac dc counts.jpg [ 243.38 KiB | Viewed 17634 times ]

 Author: Jeremy [ Wed Nov 23, 2011 7:02 am ] Post subject: Re: Long equations...imaginary numbers? Qlarity has no built-in support for imaginary numbers. It is, of course possible to handle them manually. In your case it might be something like this:Code:dim tmp as floatdim p4Real, p4Imag as floattmp = p3+(p2*p2)if tmp >= 0 then    p4Real = sqrt(tmp)    p4Imag = 0else    p4Real = 0    p4Imag = sqrt(-tmp)endifNaturally there is additional code required to handle the two terms of P4 throughout the calculation. I think you are the first person who has asked about imaginary numbers for Qlarity.

 Author: aaronkeit [ Wed Nov 23, 2011 11:03 am ] Post subject: Re: Long equations...imaginary numbers? Thanks jeremy,Notice in the jpg I attached the little 'ii' symbol for the imaginary number that appear twice in the lower two equations? Both equations go something like: [(0.132283 - 0.229122 ii) * (long equation)] - [(0.209987 + 0.363708 ii) * (long equation) ]So if I need to write out these equations in Qlarity should I just ignore the 'i' and use your method to make any negative numbers positive that reside inside a square root? I understand that (i x i =-1) so I could substitute in -1 if the 'i' symbols are multiplied by each other, but in this case one is subtracted from the other.Thanks again, you've been very helpful and I appreciate it.Aaron

 Author: Jeremy [ Wed Nov 23, 2011 11:08 am ] Post subject: Re: Long equations...imaginary numbers? Unfortunately the only way to deal with imaginary numbers is to carry the imaginary terms separately. Computers fundamentally have no understanding of imaginary numbers. A number of programming languages do provide library solutions for complex numbers -- and the library solutions are doing exactly what I describe -- tracking the real and imaginary terms separately. It is possible to write a Qlarity object that will do the same and handle all of this fairly seamlessly. However, there has not really been any demand for it until now.

 Author: aaronkeit [ Wed Nov 23, 2011 11:35 am ] Post subject: Re: Long equations...imaginary numbers? OK I will try to write out these other equations and see what happens. I am not quite clear how to bring the term that equals zero (p4real=0) or (p4Imag=0) back in to the equation. Do I just look for an opportunity to take two imaginary number terms that cancel each other out, equaling -1?Aaron

 Author: Jeremy [ Wed Nov 23, 2011 12:14 pm ] Post subject: Re: Long equations...imaginary numbers? Its been a long time since I have used imaginary numbers. If I recall correctly, any complex number is expressed in the form of (x + yi). This includes all real numbers (where y=0) and all pure imaginary numbers (where x=0). If you think along those lines, you would carry both terms until you cancel them out. Hopefully they will always cancel out at some point, because I am not sure what meaning your formula will have if it results in an imaginary numbers.

 Author: aaronkeit [ Wed Nov 23, 2011 1:05 pm ] Post subject: Re: Long equations...imaginary numbers? OK... so I wrote up the last equation of the three... but all I did was replace the P4 term with P4img... so I know I'm not doing it right yet. It appears that the two terms in each equation that contain an imaginary number are in the form of (x+yi), but x does not equal zero. For example, the first term:(.13228342099734997 + 0.22912160616643376 i )Should I be rewriting this as:229real=0229imag=.22912160616643376(.13228342099734997 + 229real +229imag)hmm... I have yet to grasp this.Code:func click()dim dur as floatdim dist as floatdim MA as floatdim MD as floatdim initspeed as floatdim p1 as floatdim p2 as floatdim p3 as floatdim p4 as floatdim p5 as floatdim p6 as floatdim p7 as floatdim p8 as floatdim p9 as floatdim p10 as floatdim p11 as floatdim tmp as floatdim P4real as floatdim P4img as floatdim imag as floatMA=20MD=30dur=20dist=1000initspeed=0p1=(-1/dur)*(.13228342099734997 + 0.22912160616643376)p2=(-2*power(dist,3))-(6*MA*power(dist,2))+(6*MD*power(dist,2))-(6*power(MD,2)*dist)+(12*MA*MD*dist)+(18*MA*dur*initspeed*dist)-(2*power(MA,3))+(2*power(MD,3))-(6*MA*power(MD,2))-(27*MA*power(dur,2)*power(initspeed,2))+(6*power(MA,2)*MD)+(18*power(MA,2)*dur*initspeed)-(18*MA*MD*dur*initspeed)p3=4*power(6*(MA*dur*initspeed)-power(dist+MA-MD,2),3)tmp=(p3+power(p2,2))if tmp<0 then    P4real=0    P4img=sqrt(-tmp)    else    P4real=sqrt(tmp)    P4img=0endifp5=power((p2+p4img),1/3)p6=p1*p5p7=(1/3)*(dist+MA-MD)/durp8=((.20998684164914552 - 0.3637078786572404)*((6*MA*dur*initspeed)-power((dist+MA-MD),2)))p9=(dur*p5)p10=p7-(p8/p9)p11=p6+p10endfunc

 Author: aaronkeit [ Wed Nov 23, 2011 4:51 pm ] Post subject: Re: Long equations...imaginary numbers? One more question...The equations contain several parts with powers to the 1/3... should this be written up in Qlarity as power(x,1/3)?I notice when I wrote it up as power(x,.3333333) I got a "math function domain error."Aaron

 Author: Jeremy [ Thu Nov 24, 2011 7:40 am ] Post subject: Re: Long equations...imaginary numbers? I don't think you can take a negative number to a fractional power. Unfortunately Qlarity is not a scientific calculation package -- its math is geared at HMI and display functions with enough functions to provide a decent general mathematical coverage.

 Author: aaronkeit [ Thu Nov 24, 2011 9:57 pm ] Post subject: Re: Long equations...imaginary numbers? Jeremy,I wasn't using a negative number... this is assuming the number is positive.AAron

 Author: Jeremy [ Fri Nov 25, 2011 7:30 am ] Post subject: Re: Long equations...imaginary numbers? When I get back to the office on Monday I can give it a shot. However, I am fairly sure that the error message you received is the standard one you get when you call power on a negative number with a non-integer exponent

 Author: Jeremy [ Fri Nov 25, 2011 9:44 am ] Post subject: Re: Long equations...imaginary numbers? When I get back to the office on Monday I can give it a shot. However, I am fairly sure that the error message you received is the standard one you get when you call power on a negative number with a non-integer exponent.You should be able to raise any positive number to an arbitrary power (subject to size and precision limitations of 32 bit floating point numbers).

Author:  aaronkeit [ Sun Nov 27, 2011 2:57 pm ]
Post subject:  Re: Long equations...imaginary numbers?

Jeremy,

Perhaps you are right... I may have gotten confused and been using a negative number. I have the fractional exponents working now... I was lost for a while because I was writing the fractional part as power(x,(2/3)) which would always equal zero. I discovered that I must write this as: power(x,(2.0/3.0)) to have Qlarity recognize the (2/3) value as a float value of .66666 instead of an integer of zero.

In the mean time, I have identified the correct formula I need to use, and it contains two imaginary numbers, as you can see in the jpg I attached. I have written this up in Qlarity below, and confirmed that it is written up properly, with the exception that I did not include the two"i" imaginary numbers, and I changed "p5" to "-p5" and called it "p5img" because it was a negative number being subjected to a fractional power.

The correct value for p11 is 59.6704, but of course I am not getting that value because I am still unclear on how to compensate for the lack of the imaginary number in the formula. I would very much appreciate some further advice on how to write up this formula, compensating for the fact the Qlarity does not handle imaginary numbers.

Code:
func click()

dim dur as float
dim dist as float
dim MA as float
dim MD as float
dim initspeed as float
dim p1 as float
dim p2 as float
dim p3 as float
dim p4 as float
dim p5 as float
dim p6 as float
dim p7 as float
dim p8 as float
dim p9 as float
dim p10 as float
dim p11 as float
dim P5real as float
dim P5img as float
MA=100
MD=-100
dur=20
dist=1000
initspeed=2

p1=(1/(6*power(2,(1.0/3.0))*dur))*(1-SQRT(3))
p2=(-2*power(dist,3))-(6*MA*power(dist,2))-(6*dist*power(MA,2))-(2*power(MA,3))+(6*power(dist,2)*MD)+(12*dist*MA*MD)+(6*power(MA,2)*MD)-(6*dist*power(MD,2))-(6*MA*power(MD,2))+(2*power(MD,3))+(18*dist*MA*dur*initspeed)+(18*power(MA,2)*dur*initspeed)-(18*MA*MD*dur*initspeed)-(27*MA*power(dur,2)*power(initspeed,2))
p3=(4*power(((6*MA*dur*initspeed)-power(dist+MA-MD,2)),3))
p4=SQRT((p3+power(p2,2)))

p5img=-(p2+p4)

p6=power(p5img,(1.0/3.0))
p7=(dist+MA-MD)/(3*dur)
p8=(SQRT(3)+1)*((6*MA*dur*initspeed)-power(dist+MA-MD,2))
p9=(3*power(2,(2.0/3.0))*dur)*p6
p10=(p7-p8)/p9
p11=(p1*p6)+p10

endfunc

 Attachments: correct formula.jpg [ 74.3 KiB | Viewed 17594 times ]

 Author: Jeremy [ Mon Nov 28, 2011 7:51 am ] Post subject: Re: Long equations...imaginary numbers? It has been a very long time since I have attempted to apply complex algebraic formulas involving imaginary numbers, so I am quite rusty.I am not sure if I will be able to help you through your entire problem.I can see where you are deriving P1-P4. They appear to be calculating the bottom two lines of the equation. But I haven't the foggiest idea what P5 is supposed to represent. It seems like you are using that as an intermediate term for that massive cube root that occupies 90% of the bottom two rows of the equation. However an imaginary number is only for the square root of a negative number. You cannot call a cube root an imaginary number (in fact, negative numbers have real cube roots, even if the math package that Qlarity uses cannot calculate directly calculate them.**). So I am not sure how p5Img is supposed to work as it would only work if you were applying a square root to it.** If my middle school math doesn't fail me, you can indirectly calculate a cube root of a negative number like followsCode:dim tmp as floatdim cubeRoot as floattmp := -8if tmp < 0 then    cubeRoot = -power(-tmp, 1.0/3.0)else    cubeRoot = power(tmp, 1.0/3.0)endifI don't know that I can handle any other powers (e.g I have no idea if the a negative number to the 1/4 power is an imaginary number or something entirely different, and if you get to true fractional powers like -0.17837, I have no idea at all)

 Author: aaronkeit [ Mon Nov 28, 2011 10:46 am ] Post subject: Re: Long equations...imaginary numbers? Jeremy,Now that I am thinking about it... I understand that the cube root of p5 doesn't have to be imaginary... I just had to switch the P5 term to a negative because Qlarity would give an error when I would try to do a cube root on a negative.If I incorporate your method in to my code as follows:Code:p1=(1/(6*power(2,(1.0/3.0))*dur))*(1-SQRT(3))p2=(-2*power(dist,3))-(6*MA*power(dist,2))-(6*dist*power(MA,2))-(2*power(MA,3))+(6*power(dist,2)*MD)+(12*dist*MA*MD)+(6*power(MA,2)*MD)-(6*dist*power(MD,2))-(6*MA*power(MD,2))+(2*power(MD,3))+(18*dist*MA*dur*initspeed)+(18*power(MA,2)*dur*initspeed)-(18*MA*MD*dur*initspeed)-(27*MA*power(dur,2)*power(initspeed,2))p3=(4*power(((6*MA*dur*initspeed)-power(dist+MA-MD,2)),3))p4=SQRT((p3+power(p2,2)))tmp=(p2+p4)if tmp < 0 then    p6 = -power(-tmp, 1.0/3.0)else    p6 = power(tmp, 1.0/3.0)endif... I get a value for P6 of -1475.23 using the sample values given in the code I posted previously... resulting from -power(-(p2+p4), 1.0/3.0) :(p2) =-3373920000 (P4)= 9120000*Sqrt)^(1/3)The problem is that when I use Mathematica to evaluate (p2+p4)^1/3 I get: 737.613 + 1277.58 i...a complex number containing a real number component and an imaginary number component. Now since this is all beyond me, I naturally turn to Wikipedia where I learn that all cube roots of non zero real numbers have 1 real cube root and a pair of complex conjugate roots. Since Mathematica returns the correct answer in the end for the whole formula, I assume that I must be using this complex number root. Now if I can only determine how this complex root is obtained and write it up in Qlarity, somehow handling the imaginary unit produced, along with the imaginary units already present in the source formula, I'll be set!Any ideas?

 Page 1 of 3 All times are UTC - 7 hours Powered by phpBB® Forum Software © phpBB Grouphttp://www.phpbb.com/