-
Notifications
You must be signed in to change notification settings - Fork 0
/
electr_circuit.html
224 lines (182 loc) · 8.09 KB
/
electr_circuit.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<style type="text/css">
/* 13. Basic Styling with CSS */
/* Style the lines by removing the fill and applying a stroke */
.line {
fill: none;
stroke: #ffab00;
stroke-width: 3;
}
/* Style the dots by assigning a fill and stroke */
.dot {
fill: #ffab00;
stroke: #fff;
}
</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
R<sub>0</sub>=<input id="R0" value="0.01"><br>
U=<input id="U" value="12"><br>
k=<input id="k" value="0.01"><br>
<button onclick="start()">Start</button><br><br>
<div>
<svg width="580" height="400" xmlns="http://www.w3.org/2000/svg">
<!-- Created with Method Draw - http://github.com/duopixel/Method-Draw/ -->
<g>
<title>background</title>
<rect fill="#fff" id="canvas_background" height="402" width="582" y="-1" x="-1"/>
<g display="none" overflow="visible" y="0" x="0" height="100%" width="100%" id="canvasGrid">
<rect fill="url(#gridpattern)" stroke-width="0" y="0" x="0" height="100%" width="100%"/>
</g>
</g>
<g>
<title>Layer 1</title>
<line stroke="#fff" stroke-linecap="undefined" stroke-linejoin="undefined" id="svg_1" y2="324.999995" x2="485.999996" y1="95" x1="111" stroke-width="1.5" fill="none"/>
<rect stroke="#000000" id="svg_2" height="291.999987" width="426.000014" y="51.000013" x="83" stroke-width="2.5" fill="#ffffff"/>
<line stroke-linecap="null" stroke-linejoin="null" id="svg_4" y2="344.751596" x2="183" y1="51" x1="183" stroke-width="2.5" stroke="#000000" fill="none"/>
<line stroke-linecap="null" stroke-linejoin="null" id="svg_5" y2="343.55238" x2="297" y1="50" x1="297" stroke-width="2.5" stroke="#000000" fill="none"/>
<line stroke-linecap="null" stroke-linejoin="null" id="svg_8" y2="201" x2="525.128336" y1="201" x1="490" stroke-width="5.5" stroke="#000000" fill="none"/>
<line stroke-linecap="null" stroke-linejoin="null" id="svg_9" y2="213" x2="540.030762" y1="213" x1="475" stroke-width="4.5" stroke="#000000" fill="none"/>
<line stroke-linecap="null" stroke-linejoin="null" id="svg_10" y2="207" x2="476.968758" y1="207" x1="541" stroke-width="7.5" stroke="#ffffff" fill="none"/>
<rect id="svg_11" height="246.315517" width="70.526238" y="71.579098" x="46.842375" stroke-width="7.5" fill="#ff0000"/>
<rect id="svg_13" height="246.315517" width="70.526238" y="71.579098" x="145.789633" stroke-width="7.5" fill="#ff0000"/>
<rect id="svg_14" height="246.315517" width="70.526238" y="70.526467" x="260.52635" stroke-width="7.5" fill="#ff0000"/>
<text stroke="#000000" transform="matrix(1.823717713356018,0,0,1.73101007938385,-61.12242844887078,-123.89478642307222) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_15" y="189.754659" x="69.593251" stroke-width="0" fill="#000000">R</text>
<text stroke="#000000" transform="matrix(1.823717713356018,0,0,1.73101007938385,-61.12242844887078,-123.89478642307222) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_16" y="187.930353" x="125.580615" stroke-width="0" fill="#000000">R</text>
<text stroke="#000000" transform="matrix(1.823717713356018,0,0,1.73101007938385,-61.12242844887078,-123.89478642307222) " xml:space="preserve" text-anchor="start" font-family="Helvetica, Arial, sans-serif" font-size="24" id="svg_17" y="187.930353" x="187.91706" stroke-width="0" fill="#000000">R</text>
</g>
</svg>
</div>
<div2><p>R(t)</p></div2><br>
<div3><p>I(t)</p></div3><br>
<!-- <div4><p>R<sub>total</sub>(t)</p></div4> -->
<script>
function plot_self(){
var svg = d3.select("div")
.append("svg")
.style("border", "1px solid black")
.attr("width", 300)
.attr("height", 250);
dx=25;
dy=20;
var p1 = [dx, 30];
var p2 = [dx, p1[1]+dy];
var p3 = [200, 30];
var width = 30;
var height = 100;
var p4 = [200, p2[1] + height/2];
var p5 = [p4[0], p4[1]+7];
var p6 = [200, p2[1] + height+dy];
var p7 = [p1[0], p2[1]+height+dy];
var p8 = [p7[0], p7[1]-dy];
var ps1 = [p4[0] - 5, p4[1]];
var ps2 = [p4[0] + 5, p4[1]];
var pS1 = [p5[0] - 10, p5[1]];
var pS2 = [p5[0] + 10, p5[1]];
function putPoint(p){
svg.append("circle")
.attr("cx", p[0])
.attr("cy", p[1])
.attr("r", 2);
}
function putLine(p1, p2){
svg
.append("line")
.style("stroke", "black")
.attr("x1", p1[0])
.attr("y1", p1[1])
.attr("x2", p2[0])
.attr("y2", p2[1])
}
putLine(p1, p2);
putLine(p1, p3);
putLine(p3, p4);
putLine(p5, p6);
putLine(p7, p6);
putLine(p7, p8);
putLine(ps1, ps2);
putLine(pS1, pS2);
svg
.append("g")
.append("rect")
.attr("width", width)
.attr("height", height)
.attr("x", p1[0]-width/2)
.attr("y", p2[1])
.style("fill", "red")
svg.select("g")
.append("text")
.attr("x",0)
.attr("y",0)
.value("R")
}
function plotsmth(dataset, target, ymax, n){
// 2. Use the margin convention practice
var margin = {top: 50, right: 50, bottom: 50, left: 50}
//, width = window.innerWidth - margin.left - margin.right // Use the window's width
//, height = window.innerHeight - margin.top - margin.bottom; // Use the window's height
, width = 250 // Use the window's width
, height = 100 // Use the window's height
// The number of datapoints
// var n = 21;
// 5. X scale will use the index of our data
var xScale = d3.scaleLinear()
.domain([0, n-1]) // input
.range([0, width]); // output
// var ymax = Math.max(...Object.values(dataset));
// 6. Y scale will use the randomly generate number
var yScale = d3.scaleLinear()
.domain([0, ymax]) // input
.range([height, 0]); // output
// 7. d3's line generator
var line = d3.line()
.x(function(d, i) { return xScale(i); }) // set the x values for the line generator
.y(function(d) { return yScale(d.y); }) // set the y values for the line generator
.curve(d3.curveMonotoneX) // apply smoothing to the line
// 8. An array of objects of length N. Each object has key -> value pair, the key being "y" and the value is a random number
// var dataset = d3.range(n).map(function(d) { return {"y": d3.randomUniform(1)() } })
// 1. Add the SVG to the page and employ #2
d3.select(target).select("svg").remove();
var svg = d3.select(target).append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// 3. Call the x axis in a group tag
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale)); // Create an axis component with d3.axisBottom
// 4. Call the y axis in a group tag
svg.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(yScale)); // Create an axis component with d3.axisLeft
// 9. Append the path, bind the data, and call the line generator
svg.append("path")
.datum(dataset) // 10. Binds data to the line
.attr("class", "line") // Assign a class for styling
.attr("d", line); // 11. Calls the line generator
// 12. Appends a circle for each datapoint
svg.selectAll(".dot")
.data(dataset)
.enter().append("circle") // Uses the enter().append() method
.attr("class", "dot") // Assign a class for styling
.attr("cx", function(d, i) { return xScale(i) })
.attr("cy", function(d) { return yScale(d.y) })
.attr("r", 5);
}
function start(){
var U = Number(document.getElementById("U").value);
var R0 = Number(document.getElementById("R0").value);
var k = Number(document.getElementById("k").value);
// alert(U+R0+k);
var n=20;
var R = d3.range(n).map(function(d) {return {"y": R0*(1+k*d)}});
var I = d3.range(n).map(function(d) {return {"y": 3*U/(R0*(1+k*d))}});
var R_total = d3.range(n).map(function(d) {return {"y": 1/(3*1/(R0*(1+k*d)))}});
// R = R0*(1+k*t)
// I = U/R
// Q = I*R*R
plotsmth(R, "div2", R0*1.5, n);
plotsmth(I, "div3", 3*U/R0, n);
// plotsmth(I, "div4", R_total*1.5, n);
}
</script>