Jump to content

Ohm's Law Calculator


The Buzzard

Recommended Posts

Just a few things I have noticed Buzzard, these aren't crucial, but should make your program run a lot faster.

 

Firstly you have this to set the function:

 

       (setq SFN                                                              ;Set variable SFN ~ (Select Formula No.)
         (atof                                                                ;Convert the string into a real number
           (get_tile \"sFN\")                                                 ;Get the value from tile key sFN
         )                                                                    ;End atof
       )"                                                                     ;End setq
     

And later you use fix to convert the variable SFN into an integer - why not just use atoi in place of atof to remove a line of extra coding. :)

 

 

Also, instead of using loads of IF statements, the program would run a lot faster with just one COND. With a COND statement, the interpreter will only evaluate test statements until it finds a true test statement, in which case it will return the result of all the expressions in that condition - and will not carry on evaluating the remaining test statements unnecessarily.

 

Something like this for your OLC_CAI program:

 


(defun OLC_CAI ()
 (setq SFN (nth SFN SFN_list))

 (cond ((or (and (member SFN '("1" "4" "6"))
                 (zerop KV4))
            (and (= SFN "5")
                 (zerop KV3))
            (and (member SFN '("8" "10" "12"))
                 (zerop KV2))
            (and (= SFN "11")
                 (zerop KV1)))
        (OLC_DBZ))
       ((= "1" SFN)
        (setq E KV3 R KV4 I (/ E R) P (/ (* E E) R)))
       ((= "2" SFN)
        (setq I KV2 R KV4 E (* R I) P (* I I R)))
       ((= "3" SFN)
        (setq E KV3 I KV2 R (/ E I) P (* E I)))
       ((= "4" SFN)
        (setq P KV1 R KV4 E (sqrt (* P R)) I (sqrt (/ P R))))
       ((= "5" SFN)
        (setq P KV1 E KV3 R (/ (* E E) P) I (/ P E)))
       ((= "6" SFN)
        (setq E KV3 R KV4 P (/ (* E E) R) I (/ E R)))
       ((= "7" SFN)
        (setq I KV2 R KV4 P (* I I R) E (* R I)))
       ((= "8" SFN)
        (setq P KV1 I KV2 R (/ P (* I I)) E (/ P I)))
       ((= "9" SFN)
        (setq P KV1 R KV4 I (sqrt (/ P R)) E (sqrt (* P R))))
       ((= "10" SFN)
        (setq E KV3 I KV2 P (* E I) R (/ E I)))
       ((= "11" SFN)
        (setq P KV1 E KV3 I (/ P E) R (/ (* E E) P)))
       ((= "12" SFN)
        (setq P KV1 I KV2 E (/ P I) R (/ P (* I I)))))
 (OLC_DCO))

 

You could also apply this to your OLC_DCO program.

 

Just a few pointers Buzzard.

 

Lee

Link to comment
Share on other sites

  • Replies 104
  • Created
  • Last Reply

Top Posters In This Topic

  • The Buzzard

    56

  • Lee Mac

    29

  • CAB

    11

  • SEANT

    3

Top Posters In This Topic

Posted Images

Also, as Sean points out... you could delve into the world of imaginary numbers... :D

 

The coding wouldn't be too difficult, see my example:

 

(defun c$plex (num / i)
 (or (and (minusp num) (setq i "i")) (setq i ""))
 (strcat (rtos (sqrt (abs num))) i))

(defun c:test (/ num)
 (if (setq num (getreal "\nSpecify a Number: "))
   (alert (c$plex num)))
 (princ))

 

Change the i to a j if you are an engineer of course... o:)

Link to comment
Share on other sites

Also, as Sean points out... you could delve into the world of imaginary numbers... :D

 

The coding wouldn't be too difficult, see my example:

 

(defun c$plex (num / i)
 (or (and (minusp num) (setq i "i")) (setq i ""))
 (strcat (rtos (sqrt (abs num))) i))

(defun c:test (/ num)
 (if (setq num (getreal "\nSpecify a Number: "))
   (alert (c$plex num)))
 (princ))

 

Change the i to a j if you are an engineer of course... o:)

 

Thank You Very Much Lee,

 

If you do not mind can you explain to me what you mean by this world of imaginary numbers. I do not know what your driving at.

Link to comment
Share on other sites

Imaginary numbers are an extension of the Real Numbers, created for completeness in mathematics. Imaginary numbers and Real numbers are all contained within the Field of Complex Numbers. With the addition of Complex Number to mathematics, for example, every quadratic equation has a solution in the Complex field.

 

I can't quite explain it to you very well, this should take care of it nicely:

 

http://en.wikipedia.org/wiki/Complex_numbers

Link to comment
Share on other sites

Imaginary numbers are an extension of the Real Numbers, created for completeness in mathematics. Imaginary numbers and Real numbers are all contained within the Field of Complex Numbers. With the addition of Complex Number to mathematics, for example, every quadratic equation has a solution in the Complex field.

 

I can't quite explain it to you very well, this should take care of it nicely:

 

http://en.wikipedia.org/wiki/Complex_numbers

 

Lee,

 

If you do not mind I am not going in that direction. Sound interesting, But confusing. I will try to get along with the numbers I have at the moment if you do not mind.

 

Oh and thanks for the other fixes, I will get to them later since I was up all morning making the other changes to the program. My brain really hurts now.

Link to comment
Share on other sites

Lee,

 

If you do not mind I am not going in that direction. Sound interesting, But confusing. I will try to get along with the numbers I have at the moment if you do not mind.

 

Oh and thanks for the other fixes, I will get to them later since I was up all morning making the other changes to the program. My brain really hurts now.

 

Of course, its your program Buzzard - complex numbers just take care of instances where you would have negative square roots.

Link to comment
Share on other sites

Of course, its your program Buzzard - complex numbers just take care of instances where you would have negative square roots.

 

OK that make sense to me now. Next time explain it to me in english.:lol:

Link to comment
Share on other sites

OK that make sense to me now. Next time explain it to me in english.:lol:

 

Well, you did ask what imaginary numbers were... and it is a pet subject of mine o:)

Link to comment
Share on other sites

Well, you did ask what imaginary numbers were... and it is a pet subject of mine o:)

 

Since its a pet subject of yours, Your pet should be due for its walk or you will have a real mess soon.

Link to comment
Share on other sites

The reason I mentioned Complex Numbers was due their role whilst dealing with electrical impedance, resistance, reactance. It’s probably not something of concern, however.

 

http://www.economicexpert.com/a/Impedance.htm

 

 

 

Regardless their practicality, accounting for complex numbers while programming is interesting in a theoretical sense. They would also require redefinition of some of the standard operators as shown here.

 

http://msdn.microsoft.com/en-us/library/0352zzhd(VS.80).aspx

 

As a side note; Complex Numbers were used in Gamow’s mathematical solution to the challenge posted here:

 

http://www.cadtutor.net/forum/showthread.php?t=15248

Link to comment
Share on other sites

Since its a pet subject of yours, Your pet should be due for its walk or you will have a real mess soon.

 

This is interesting to me. I never thought you could write an equation like this in a conditional. I would have thought you would need (progn in there. So much more to know and understand.

 

(defun OLC_CAI ()
 (setq SFN (nth SFN SFN_list))

 (cond ((or (and (member SFN '("1" "4" "6"))
                 (zerop KV4))
            (and (= SFN "5")
                 (zerop KV3))
            (and (member SFN '("8" "10" "12"))
                 (zerop KV2))
            (and (= SFN "11")
                 (zerop KV1)))
        (OLC_DBZ))
       ((= "1" SFN)
        (setq E KV3 R KV4 I (/ E R) P (/ (* E E) R)))
       ((= "2" SFN)
        (setq I KV2 R KV4 E (* R I) P (* I I R)))
       ((= "3" SFN)
        (setq E KV3 I KV2 R (/ E I) P (* E I)))
       ((= "4" SFN)
        (setq P KV1 R KV4 E (sqrt (* P R)) I (sqrt (/ P R))))
       ((= "5" SFN)
        (setq P KV1 E KV3 R (/ (* E E) P) I (/ P E)))
       ((= "6" SFN)
        (setq E KV3 R KV4 P (/ (* E E) R) I (/ E R)))
       ((= "7" SFN)
        (setq I KV2 R KV4 P (* I I R) E (* R I)))
       ((= "8" SFN)
        (setq P KV1 I KV2 R (/ P (* I I)) E (/ P I)))
       ((= "9" SFN)
        (setq P KV1 R KV4 I (sqrt (/ P R)) E (sqrt (* P R))))
       ((= "10" SFN)
        (setq E KV3 I KV2 P (* E I) R (/ E I)))
       ((= "11" SFN)
        (setq P KV1 E KV3 I (/ P E) R (/ (* E E) P)))
       ((= "12" SFN)
        (setq P KV1 I KV2 E (/ P I) R (/ P (* I I)))))
 (OLC_DCO))

Link to comment
Share on other sites

The reason I mentioned Complex Numbers was due their role whilst dealing with electrical impedance, resistance, reactance. It’s probably not something of concern, however.

 

http://www.economicexpert.com/a/Impedance.htm

 

 

 

Regardless their practicality, accounting for complex numbers while programming is interesting in a theoretical sense. They would also require redefinition of some of the standard operators as shown here.

 

http://msdn.microsoft.com/en-us/library/0352zzhd(VS.80).aspx

 

As a side note; Complex Numbers were used in Gamow’s mathematical solution to the challenge posted here:

 

http://www.cadtutor.net/forum/showthread.php?t=15248

 

Very interesting Sean :thumbsup:

Link to comment
Share on other sites

This is interesting to me. I never thought you could write an equation like this in a conditional. I would have thought you would need (progn in there. So much more to know and understand.

 

With a COND statement, the first line is the test statement, which, if returns T the expressions following the test statement will be evaluated.

 

PROGN is a code "wrapper" such that a group of expressions may be evaluated as a single statement, hence the IF function take only three statements:

 

(if  (test expression)
 (T expression)
 (else expression)
) ; end IF

And so, if you want the IF statement to evaluate a group of expressions if the test expression returns T, then you must wrap these expressions in a PROGN function.

 

In my example above, I have used the OR function to group my test expressions, hence if any of the expressions within the OR return T, the test expression will return T.

Link to comment
Share on other sites

Lee,

 

Where the imaginary numbers are concerned, Is it important to this code and do I really need to take care of this or will this code function well enough without it.

 

That particular area is beyond me and at this stage I really do not feel I can get thru it on my own let alone get started.

Link to comment
Share on other sites

Lee,

 

Where the imaginary numbers are concerned, Is it important to this code and do I really need to take care of this or will this code function well enough without it.

 

That particular area is beyond me and at this stage I really do not feel I can get thru it on my own let alone get started.

 

Hi Buzzard,

 

I myself am not an electrical engineer, so I do not know the relevance of imaginary numbers in that field.

 

But, as a mathematician, I like completeness, and so I'd be all for accounting for imaginary numbers.

 

All you really need to know about imaginary numbers is that the square root of -1 is i (or j if you are an engineer).

 

And so, you only need to apply this to the parts of your code that use a sqrt. Take a look at my posted code example to get more of an example.

 

You would only need to test if an entry is negative (but not both negative if division and multiplication are involved), and then use the abs to carry out the calculation, but string either an i or j onto the final answer.

 

This method only works for purely imaginary numbers with no real part - but I think this would be sufficient for your formulae.

 

I can help you through this if you need me to :)

 

Lee

Link to comment
Share on other sites

Hi Buzzard,

 

I myself am not an electrical engineer, so I do not know the relevance of imaginary numbers in that field.

 

But, as a mathematician, I like completeness, and so I'd be all for accounting for imaginary numbers.

 

All you really need to know about imaginary numbers is that the square root of -1 is i (or j if you are an engineer).

 

And so, you only need to apply this to the parts of your code that use a sqrt. Take a look at my posted code example to get more of an example.

 

You would only need to test if an entry is negative (but not both negative if division and multiplication are involved), and then use the abs to carry out the calculation, but string either an i or j onto the final answer.

 

This method only works for purely imaginary numbers with no real part - but I think this would be sufficient for your formulae.

 

I can help you through this if you need me to :)

 

Lee

 

Thanks Lee,

 

I will review what you have provided for this. I will give it my best shot to try and figure this out on my own. I will tap you if I get stuck. In the mean time I am trying to redo another function in the short method for the Output Function of the code. Just some house keeping that needs to get done I guess.

 

Thanks Again

Link to comment
Share on other sites

Ok Lee,

 

I tested this code:

(defun c$plex (num / i)

(or (and (minusp num) (setq i "i")) (setq i ""))

(strcat (rtos (sqrt (abs num))) i))

 

(defun c:test (/ num)

(if (setq num (getreal "\nSpecify a Number: "))

(alert (c$plex num)))

(princ))

 

I also performed a check in the OLC.lsp and found that negative numbers are not accepted when it comes to square root.

So I see now where this would be important to fix. I am a bit confused as to what is going on in the function, because my level of math cannot compare to yours. I partially can understand portions of it, But as how to mingle it into my code has me somewhat lost.

 

My guess is c$plex function need to remain as a separate function within my code. I would also think instead of an alert I would have to call the function from within my calculation statements for square roots.

 

Not sure how to do this exactly. If I am on the right track would you please let me know.

 

Thanks

Link to comment
Share on other sites

The calculations associated with Complex Numbers would be used to generate the Ohms value so would not necessarily be within the scope of your routine – where the value is either already known by, or presented to the user.

 

If the ohm value needed further analysis then there would be a need to decompose it to resistance and reactance. Complex Numbers and their operators (+,-,*,/) are particularly well suited for this decomposition, though standard Cartesian vector operation could also accomplish the task.

 

http://en.wikipedia.org/wiki/Electrical_impedance

 

http://www.ndt-ed.org/EducationResources/CommunityCollege/EddyCurrents/Physics/impedance.htm

 

http://www.kpsec.freeuk.com/imped.htm#reactance

http://www.kpsec.freeuk.com/imped.htm

Link to comment
Share on other sites

Thanks for the info Sean, very complex... :lol:

 

Even so, I would still be interested in seeing Complex Numbers accounted for, just for completeness :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...