Search This Blog

Showing posts with label Mathematics. Show all posts
Showing posts with label Mathematics. Show all posts

Wednesday, April 14, 2010

Simulation: Complex Parser Completion

So I have successfully finished the Complex Parser.  I was able to successfully separate the operators in a particular order.  So now if you have a string with complex math you can turn it into a complex class number.  There isn't much to say about this part of the project now that it's complete.  I still need to do further testing to confirm all the numbers, but as of now it works like a charm.

I will say that the code seems a little inefficient and longer than necessary.  Hopefully in the future I can be more efficient in the way it works.

Hopefully, I can get back to more stimulating posts later, teaching you guys somethings.  Feel free to leave comments and check out the old posts for more information to learn...

--------------
Justin Coulston
justin.coulston@gmail.com

Tuesday, April 13, 2010

Simulation: Update on the Complex Parser

This is a quick update on the software.  I was able to separate the string into separate tokens.  There are 3 types of tokens:

0 = Real Token
1 = Imaginary Token
2 = Inner Token (within parenthesis).

What will happen on a later date, this "Evaluate" subroutine will turn the real token into a complex number and the imaginary token into a complex number.  The inner token will go back through the "Evaluate" subroutine (without the parenthesis) until a complex number is returned.  This method is using recursion to evaluate a full complex string.

The next step will be to perform the math in operator precedence.  This may get a little tricky since they aren't in order when I produce the tokens.  Based on my current structure, this isn't easily done.  But now that I have a structure I can possibly work around this. Wish me luck

---------
Justin Coulston
Justin.Coulston@gmail.com

Monday, April 12, 2010

Simulation: Complex Math Parser

Introduction
In my project to build a SPICE program, I ran across a problem; I need to be able to perform complex math from strings.  This has already been done a thousand times before but finding a free, good parser isn't easy.  So I decided to do this myself.  This article will explain briefly the process I'm using to parse a complex math string.

Basic Process
  1. First check the string for incorrect characters.  This can be done while performing step 2
  2. Define every character in the string as a type (ie. 0=operator, 1=real number, 2=imaginary number, 3=left parenthesis, 4=right parenthesis).  Define this into a separate string.  The reason you do this is to take in different number types (real, imaginary, scientific notation, etc.)
  3. Next separate the string into tokens to be evaluated separately.  The most open parenthesis should be it's own token. (ie. str = "2*2j+(20+2j)^12" will turn into tokens "@1*@2+@3^@4") This is only an example.  I doubt i'll have a string like this.
  4. Evaluate each token by recursion through the same function until it outputs a complex number. (a custom class)
  5. With a complex number for the tokens you can then evaluate in precedent order of functions.
  6. Output complex number 
That's the quick and easy of it.  I have only completed steps 1 and 2 so far.  The rest will hopefully be completed in the week.  Hope this is helpful...

Sunday, April 4, 2010

Mathematics: Ohm's Law in Matrix Form


Introduction
So I was reading on Physics Forums about Ohm's Law in vector form and also SPICE analysis using matrices.  So I decided to do some more research and write on the topic.  From my understanding, many SPICE programs use a mixture of this technique to make node voltage analysis simpler in software.  It's actually a very effective technique.

Notation
The following notation is used for complex numbers.  It makes use of matrix algebra and can be used to multiple any complex numbers together.  I'll do my best to describe as much as possible how this applies to circuit analysis.

[R] Real Value (Resistance, Real part of Voltage, etc)
[X] Reactance Value (Inductance, Phase component, etc)

This is the basic form to representing a component.  So if we have a DC voltage source at 10V, it would be represented as so:

So to form Ohm's Law simply construct the equation:

which is equivalent to

So you see, use the dot product to find the product of the complex numbers.

Conclusion
This is a simple example but this can be expanded to be used in very complex circuits including frequency dependent components and some linear models.  Anyways hope this was helpful.  Let me know if you wish for more examples.

-------------------
Justin Coulston