Ide dasarnya cukup sederhana. Anda mengatur matriks ( V ) yang mewakili "simpul" atau simpul dalam sistem Anda. Setiap node ini memiliki "voltase" bernilai skalar yang terkait dengannya yang dapat diubah atau diperbarui saat algoritma berjalan. Juga akan ada dua node yang tegangannya tidak dapat diubah. Kita akan menerapkan semacam "baterai" di sini, sehingga kedua simpul tersebut mewakili kedua ujung baterai ini.
Secara terpisah, dua matriks ( dan R hRvRh ) mewakili tepi dalam sistem, horisontal dan vertikal. Ini adalah nilai resistansi Anda, saya kira. Saya tidak yakin bagaimana Anda bermaksud mengisi ini. Tapi itu masalahmu. Teknik ini mengasumsikan Anda dapat mengisi matriks ini juga.
Bergantung pada bahasa komputer yang Anda gunakan, Anda mungkin atau mungkin tidak dapat menggunakan indeks negatif. Tidak masalah. Ini hanya masalah mengingat apa yang Anda hadapi.
Mari kita asumsikan panjang dibagi menjadi bagian N L dan bahwa "panjang" A dibagi menjadi bagian N A. Maka Anda perlu membuat matriks dengan ( N L + 1 ) ⋅ ( N A + 1 ) simpul untuk nilai tegangan skalar. (atau lebih besar.) Anda juga akan membutuhkan dua matriks lainnya dengan N A ⋅ ( N L + 1 ) tepi vertikal dan N L ⋅ ( N A +LNLANA(NL+1)⋅(NA+1)NA⋅(NL+1) tepi horizontal antara simpul tersebut.NL⋅(NA+1)
Sekarang. Inisialisasi semua simpul dengan . Pilih salah satu simpul di sebelah kiri (di tengah, lebih disukai) dan catat sebagai 00VNilai V yang TIDAK diizinkan untuk pernah berubah. Gunakan metode apa pun yang Anda inginkan untuk ini. Pilih salah satu simpul di sebelah kanan (di tengah, lebih disukai) dan ubah nilainya menjadi 10V , sambil sekali lagi perhatikan bahwa nilainya TIDAK diizinkan untuk pernah berubah. Teknik yang bekerja di sini adalah membiarkannya berubah secara normal tetapi kemudian mengganti nilainya setiap langkah. Tetapi tidak masalah bagaimana Anda mencapai ini, selama Anda mencapainya.1V
(Ada teknik lain untuk alasan efisiensi. Tapi mungkin tidak ada gunanya untuk mengganggu mereka di sini.)
Sekarang untuk algoritma, yang kadang-kadang disebut checkerboard atau algoritma merah-hitam . Bergerak melalui matriks tegangan simpul Anda, proses setiap node di mana jumlah dari dua indeks, genap, melakukan tugas sederhana berikut:i+j
Vi,j=Rhi,j−1⋅Rhi,j⋅(Vi−1,j⋅Rvi,j+Vi+1,j⋅Rvi−1,j)Rhi,j−1⋅Rhi,j⋅(Rvi,j+Rvi−1,j)+Rvi−1,j⋅Rvi,j(Rhi,j+Rhi,j−1)+Rvi−1,j⋅Rvi,j⋅(Vi,j−1⋅Rhi,j+Vi,j+1⋅Rhi,j−1)Rhi,j−1⋅Rhi,j⋅(Rvi,j+Rvi−1,j)+Rvi−1,j⋅Rvi,j(Rhi,j+Rhi,j−1)
Persamaan di atas tidak lebih dari menghitung tegangan dari sebuah simpul pusat yang memiliki empat resistor yang menghubungkannya, di mana tegangan di ujung lain dari empat resistor diketahui. Tegangan simpul pusat kemudian dihitung dari persamaan di atas. Karena pembagi adalah sama untuk setiap istilah, Anda bisa menghitung jumlah pembilang dan kemudian membaginya sekali dengan penyebut.
Itu akan memperbarui semua node di mana jumlah genap. Sekarang Anda melakukan prosedur yang sama untuk semua node di mana jumlah i + j aneh. Setelah kedua langkah ini dilakukan, Anda telah menyelesaikan satu siklus.i+ji+j
Jika perlu, setel ulang dua simpul khusus (untuk dan untuk 10V1V
Anda siap untuk siklus berikutnya. Lakukan siklus ini sebanyak yang Anda rasa perlu agar keadaan keseluruhan menjadi tenang (dan itu akan terjadi).
1V
I'm staring at some code I wrote that totals, with lots of comments, just 67 lines. So it's NOT hard to write.
The "short summary" of this idea is that you apply a 1V battery and then watch as the voltages spread throughout the system. Once the voltages stabilize (your criteria for that), all you have to do is look at the current coming into, or out of, one battery terminal or the other one. They should both be the same current value (within some numerical bounds) for obvious reasons.
Why is it that you must separate the system into i+j = even and i+j =
odd?
Suppose you compute V5,5=f(V4,5,V6,5,V5,4,V5,6). This references the nodes that surround V5,5. That's fine. Suppose you next compute V5,6=f(V4,6,V6,6,V5,5,V5,7). Note that in the list of parameters is the value you just computed for V5,5? This would "smudge" things a lot. It's not sound. Instead, each cycle of odd/even should "appear as if" it occurred at the same moment. So your next computation should be V5,7=f(V4,7,V6,7,V5,6,V5,8) because none of the inputs to the function are nodes that were changed during this step. Then you swing around and compute the alternates, avoiding the smudging but now updating the alternates. You really do have to do it this way.
Also, is the formula identical for both even and odd steps through?
Yes, it's the same.
Can it all be solved in one step using some sort of linear system Ax=b
where A is a linear operator and b provides the boundary conditions?
Looking at it, it seems somewhat analogous to finite difference
methods for solving partial differential equations..
There is a connection. I think it's called a 'matrix-free' implementation.
Here's an example. The following set of resistor values were placed into LTSpice for simulation:
I kept it short and simple. As you can see, the approximate computed current from the 1V power supply is given as 30.225mA. (The actual value computed by Spice was 30.224552mA.)
I ran the following VB.NET program:
Module GEOGRID
Const NL As Integer = 2
Const NA As Integer = 2
Const INF As Double = 1.0E+32
Sub Main()
Static Rh As Double(,) = New Double(NL + 2, NA + 1) {
{INF, INF, INF, INF},
{INF, 5, 21, INF},
{INF, 76, 10, INF},
{INF, 32, 22, INF},
{INF, INF, INF, INF}}
Static Rv As Double(,) = New Double(NA + 1, NL + 2) {
{INF, INF, INF, INF, INF},
{INF, 61, 50, 16, INF},
{INF, 56, 45, 18, INF},
{INF, INF, INF, INF, INF}}
Dim V As Double(,) = New Double(NL + 2, NA + 2) {
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}}
Dim PDE As Func(Of Integer, Integer, Double) = Function(ByVal i As Integer, ByVal j As Integer) (
Rh(i, j - 1) * Rh(i, j) * (V(i - 1, j) * Rv(i, j) + V(i + 1, j) * Rv(i - 1, j)) +
Rv(i - 1, j) * Rv(i, j) * (V(i, j - 1) * Rh(i, j) + V(i, j + 1) * Rh(i, j - 1))
) / (
Rh(i, j - 1) * Rh(i, j) * (Rv(i, j) + Rv(i - 1, j)) +
Rv(i - 1, j) * Rv(i, j) * (Rh(i, j) + Rh(i, j - 1))
)
Dim IV As Func(Of Integer, Integer, Double) = Function(ByVal i As Integer, ByVal j As Integer) 0 +
(V(i, j) - V(i - 1, j)) / Rv(i - 1, j) + (V(i, j) - V(i + 1, j)) / Rv(i, j) +
(V(i, j) - V(i, j - 1)) / Rh(i, j - 1) + (V(i, j) - V(i, j + 1)) / Rh(i, j)
Dim idx As Integer = NA \ 2 + 1
Dim jdx1 As Integer = NL + 1
Dim jdx2 As Integer = 1
For x As Integer = 1 To 1000
For k As Integer = 0 To (NA + 1) * (NL + 1) - 1 Step 2
Dim i As Integer = k \ (NL + 1)
Dim j As Integer = k - i * (NL + 1) + 1
i += 1
If Not (i = idx AndAlso (j = jdx1 OrElse j = jdx2)) Then V(i, j) = PDE(i, j)
Next
For k As Integer = 1 To (NA + 1) * (NL + 1) - 1 Step 2
Dim i As Integer = k \ (NL + 1)
Dim j As Integer = k - i * (NL + 1) + 1
i += 1
If Not (i = idx AndAlso (j = jdx1 OrElse j = jdx2)) Then V(i, j) = PDE(i, j)
Next
Next
Console.WriteLine("R = " & (1.0 / IV(idx, jdx1)).ToString)
Console.WriteLine("R = " & (-1.0 / IV(idx, jdx2)).ToString)
End Sub
End Module
With the following result printed out: R=33.0856844038614Ω. Which is the correct answer.
The above program shows a way of setting up the resistors, vertical and horizontal, as well as the voltage matrix, so that it simplifies some of the tests for non-existent nodes and/or resistor values. The code is a little cleaner, this way, though it does require some more array elements. (I've simply made the extra resistor values infinite in value.) Just compare how I've set up the arrays with the way the schematic was laid out, as well, and I think you will be able to work out all the exact details here.
I've also hacked in the resistors and node values, of course, without making this in any way a general purpose program for reading up a table of values. But that generality is pretty easy to add. And this code should make everything I wrote absolutely unambiguous.
Note that I also just ran the x loop 1000 types, alternating red and black within the x loop. I just picked a number. To make this more general purpose, you may prefer a different way of determining how many iterations to perform.
And a final note. Just to prove that you can use either fixed voltage node's current to compute the resistor, I've used two lines in order to print out both values: one computed from the 0V side and one computed from the 1V side. Either way, you should see the same number.
(Okay. One more final note. This would be much better targeted at F# or any decent compiler targeting a massively parallel computing system. Each calculation in either "red" or "black" can be performed in parallel; completely independently of each other. F# makes this trivial. So coded in F#, you could run this on all of your available cores without anything special to do. It just works. Just a note in case you are collecting a LOT of data in some fashion and might want to take full advantage of a multi-core system.)
END NOTE:
The derivation is pretty simple from KCL. Place four resistors into the following arrangement:
simulate this circuit – Schematic created using CircuitLab
Apply KCL:
VR1+VR2+VR3+VR4V=V1R1+V2R2+V3R3+V4R4∴=(V1R1+V2R2+V3R3+V4R4)(R1∣∣R2∣∣R3∣∣R4)
Some playing around with algebra gets the result I used in the code.