Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Solving DC circuits using numpy #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed

Solving DC circuits using numpy #37

wants to merge 13 commits into from

Conversation

drskoolie
Copy link

@drskoolie drskoolie commented Nov 1, 2020

First off I want to thank @melissawm and the numpy team for all the encouragement and motivation!

Reason for writing tutorial:

If you google how to solve circuits using matlab, a plethora of results show up. At a cursory glance I counted: 3 books written on the topic, numerous youtube videos and countless articles. On the other hand, googling how to solve circuits using numpy I found only one webpage on the matter. In it they presented a grand total of 3 lines of code and in it they used the soon-to-be deprecated matrix class and inverted the matrix to solve it:

NumpyTut

Numpy modules used in the tutorial:

np.array, np.linspace, np.argmax, np.linalg.inv, np.matmul, np.linalg.solve, np.linalg.det


Issues I faced:

  1. The file is formatted with MyST and compiled perfectly with sphinx, but I had difficulties converting it to .ipynb.
  2. Didn't know how to show the outputs of the code so I left them blank except for the plot
  3. Don't know how to reference images taken from the internet. For that reason, I drew the circuits by hand although I am looking into open source circuit drawing software so that anyone can add on to the tutorial in the future with the same consistency.

Pikachu

Added a couple of pictures of the lovable Pokemon but I don't know if it is suitable:

  1. The google docs recommended not using local pop cultural references so that the tutorial could be understood worldwide. To that regard, I used to watch the show in Arabic and the wiki has it dubbed to +40 languages. I think you would be hard pressed to find someone who doesn't know pikachu
  2. On the other hand, the google docs recommended not including 'frivolous` material which this probably falls under
  3. But on the other "other" hand, the numpy tutorial style guide advises to add an illustration below the title even if it is decorative.

It's a very simple fix to remove, so do let me know to that regard.

@melissawm
Copy link
Member

Thank you, @a-elhag ! I'll take a look as soon as possible, please let me know if you have any questions in the meantime.

@drskoolie
Copy link
Author

So I updated it with dc2.md. It has all sphinx directives and python outputs removed.

I kept the older one for reference (can easily remove it though).

Copy link
Member

@melissawm melissawm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @a-elhag! Thank you for writing this, I think this is a nice tutorial for someone looking to get started, especially if they have such a technical background. I have left a number of comments; please don't be discouraged - most of them are pretty small and simple :) In many cases, I'm using the conventions we have been using for the other tutorials, because this helps keep the content consistent across the repo and makes it easier for users to know what to expect from each document.

Some specific comments:

  • I LOVE Pikachu, but I'm not sure if we should include the pictures here. As far as I know, these are copyrighted and even if it could be argued that this is fair use, I think it's safer to drop it.
  • In general, we tend to avoid the admonition role. It may clutter the page, so we should leave that for really important points that the reader can't miss. Some of my suggestions were moving the content in the admonitions to the main text.
  • I added a bunch of reference links. Can you check that I added the appropriate ones?
  • I'm suggesting we move the proposed exercises to the "on your own" section - I think that's more appropriate and makes the tutorial flow better because the user doesn't feel like they need to stop and do the exercises in the middle of it.
  • I suggested this in a few comments but there is a last example where I didn't: whenever you choose specific values to run an example, make it clear to the user that they were chosen arbitrarily. This helps the user understand that they can play with those values and get different results without changing the main point of the analysis.

I think that's everything. Again, thank you for working on this and for considering these suggestions. If you have any questions or comments, let me know. Cheers!

EDIT: just realized that I reviewed the first file - I don't think that's super different from the other one so the comments should be just the same. Sorry about that!

@drskoolie
Copy link
Author

Hey @melissawm,

Thanks for taking the time to look through it! I'm here to learn and the more comments the better :)

I'm going through the suggestions now, will post an update when I'm done.

@drskoolie
Copy link
Author

@melissawm

I believe that I addressed each of the concerns, but do let me know if I missed anything. A summary of what was updated:

  1. Deleted the markdown file with sphinx directives and kept only the one that was linked to the .ipynb file
  2. Tried to rewrite points to make it clearer to a broader audience
  3. Checked the reference links and they are appropriate
  4. Emphasised that the numbers chosen were arbitrary.

PS:
Although it saddens my heart, I removed all pictures of the lovable Pokemon. The copyright issue is a concern and the tonality might be too informal.

@MarsBarLee
Copy link

Hey Al,

I have a suggestion- what do you think about updating your hand-drawn circuit diagrams? I have an example below of your first diagram.

circuit1 again

Not a big difference for the first circuit, but I can help with the legibility and layout of the last few diagrams too.

I used a combination of an online circuit diagram maker and Photoshop.

I could update the rest too. Let me know if you would like this. :)

@drskoolie
Copy link
Author

Yeah, thanks a lot @MarsBarLee! If you have any questions do let me know.

@MarsBarLee
Copy link

Hi Al,

These are my updates to the diagrams. Let me know what you think!
circuit1 edit
circuit2 edit
circuit3 edit
circuit4 edit
circuit5 edit
circuit6 edit

@drskoolie
Copy link
Author

@MarsBarLee these look amazing! If you want to you can merge them in to this PR (is that possible?) or I can just do it myself. Thanks for these, mine looked like chicken scratch 😄

@MarsBarLee
Copy link

I looked up how to modify other people's PR (looks like creator would have to check 'Allow edits from maintainers' and I would need to push an edit?), but I think it would be easier for both of us if you did.

And no problem, happy to help!

@drskoolie
Copy link
Author

@melissawm I rebased the commits and updated the figures. Do let me know if there is anything more that I need to do.

And @MarsBarLee, yeah I didn't know if the process was simple or hard.

@melissawm
Copy link
Member

Hi @a-elhag ! Finally getting around to this - thanks for your patience!

Before doing a thorough review, I think we can get rid of the .ipynb file as we'll do everything via .md. Also, the dc.md and other files should be at the content/ folder, not under another folder. We're still not 100% on the right organization of files in the repo (see #45 ) but for now everything lives under content.

Copy link
Member

@melissawm melissawm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! You'll probably have to change the links to the images as well because of the folder structure and markdown format. To test if your document renders successfully, go to the site folder and run make html (you will have to have sphinx installed and maybe some other dependencies, you can check for those at the requirements.txt file.) You should also add this entry to the TOC at site/index.md for sphinx to find it.

Thanks again, @a-elhag !


### What you'll need
* A "can-do" attitude
* Basic electrical knowledge (Ohm's Law, KCL, KVL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Basic electrical knowledge (Ohm's Law, KCL, KVL)
* Basic electrical knowledge (Ohm's Law, [Kirchhoff's Circuit Laws](https://en.wikipedia.org/wiki/Kirchhoff%27s_circuit_laws))

Solving for $I$ in *Figure 1* is a pretty trivial matter, it can be done in a
few lines of code. Using `R` to denote the resistance, `V` to denote the
voltage and `I` to denote the current, we have the following:
```python
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the way we are converting from ipynb to md, this should be:

Suggested change
```python
```{code-cell} ipython3

for every cell to be executed in the final notebook. (I won't note the others but I'd do it for every one of them)

Comment on lines +81 to +83
The code above is **inefficient**. You generally want to avoid running *for loops* in Python
since they are statically typed and interpreted. In this simple example it won't make much of a difference
but with larger and more complex code it will noticably run worse.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think justifying that loops are since they are statically typed and interpreted. is not correct. I would rather point to an existing resource such as

Suggested change
The code above is **inefficient**. You generally want to avoid running *for loops* in Python
since they are statically typed and interpreted. In this simple example it won't make much of a difference
but with larger and more complex code it will noticably run worse.
The code above is **inefficient**. In scientific applications, you may want to use NumPy's [n-dimensional arrays](https://numpy.org/doc/stable/reference/arrays.ndarray.html), for which [many operations are performed in compiled code for performance](https://numpy.org/doc/stable/user/whatisnumpy.html). In this simple example it won't make much of a difference
but with larger and more complex code it will noticably run worse.

Comment on lines +140 to +141
states that maximum power transfer will occur when the resistance of the load (Rload)
equals the resistance of the source (Rth).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put Rload and Rth in backticks to make them stand out against the text, but this is just a suggestion.

Suggested change
states that maximum power transfer will occur when the resistance of the load (Rload)
equals the resistance of the source (Rth).
states that maximum power transfer will occur when the resistance of the load (`Rload`)
equals the resistance of the source (`Rth`).

Comment on lines +192 to +195
\begin{equation*}
I_1 (R_1 + R_2) + I_2(-R_2) = V_1 \\
I_1 (-R_2) + I_2(R_2 + R_3) = V_2
\end{equation*}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think MyST-NB doesn't understand this LaTeX syntax - I only got it to show successfully with

Suggested change
\begin{equation*}
I_1 (R_1 + R_2) + I_2(-R_2) = V_1 \\
I_1 (-R_2) + I_2(R_2 + R_3) = V_2
\end{equation*}
$$I_1 (R_1 + R_2) + I_2(-R_2) = V_1 \\
I_1 (-R_2) + I_2(R_2 + R_3) = V_2$$


\begin{equation*}
\begin{bmatrix} R1+R2 & -R2 \\ -R2 & R2+R3 \end{bmatrix}
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use * for matrix multiplication, use \cdot instead (please check the other equations)

Comment on lines +376 to +379
```python
# x = np.linalg.solve(A, b)
# print(x)
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```python
# x = np.linalg.solve(A, b)
# print(x)
```
Now,
```{code-cell} ipython3
---
tags: [raises-exception]
---
x = np.linalg.solve(A, b)
print(x)

This tag in the code-cell makes the exception show up in the final document, but doesn't cause errors in the build - which is exactly what we want. Now, you can remove the pasted exception below from the text.

This can be easily confirmed by taking the determinant of $A$:

```
Det = np.linalg.det(A)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Det = np.linalg.det(A)
np.linalg.det(A)

inside a code-cell so we can see that the result it 0.0

Comment on lines +437 to +438
* In Part 2a Try using voltage division to calculate `Pload` directly
(ie without needing to calculate `Iload`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* In Part 2a Try using voltage division to calculate `Pload` directly
(ie without needing to calculate `Iload`)
* In Part 2a, try using [voltage division](https://en.wikipedia.org/wiki/Voltage_divider) to calculate `Pload` directly
(i. e. without needing to calculate `Iload`)

@@ -0,0 +1,452 @@
---
jupyter:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why this line is here, it doesn't show up for the other tutorials. Please check that this header is the same as for the other documents, so we don't have problems with jupytext and mystnb/sphinx.

Base automatically changed from master to main March 6, 2021 11:39
@melissawm
Copy link
Member

Hi @a-elhag - do you have any plans to continue work on this PR?

@melissawm
Copy link
Member

@a-elhag hello! If you don't have plans to update this tutorial I might take this over just to get the finishing touches and merge. Please let me know what you prefer. Cheers!

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants