質問編集履歴

3

式の修正

2021/05/25 07:50

投稿

keigo.lab
keigo.lab

スコア1

test CHANGED
File without changes
test CHANGED
@@ -19,6 +19,8 @@
19
19
  ```
20
20
 
21
21
  この部分で、変数i,kをどのようにコードに組み込んだらいいのか分かりません。
22
+
23
+ 式の前半部分は書けているのですが、Σ以降がまだ書けていません。
22
24
 
23
25
  Python初心者なので分かりやすく教えていただきたいです。
24
26
 

2

式、コードの変更・修正

2021/05/25 07:50

投稿

keigo.lab
keigo.lab

スコア1

test CHANGED
File without changes
test CHANGED
@@ -2,81 +2,31 @@
2
2
 
3
3
  Pythonで微分方程式を解いてグラフに表示させたいです。
4
4
 
5
- dy/dt=-0.2y{1-exp(1-1.337x**4)}
5
+ dy(i)/dt=-a*y(i){b-exp(b-c*x(i)**4)}+Σ[d*y(k)*exp{(x(i)-x(k)/2)**2/(e*x(k)**2)}]
6
6
 
7
+ Σの変数はkで0<k<3です。
8
+
7
- 縦軸に変数y、横軸に変数x(0<x<5)
9
+ 縦軸に変数y、横軸に変数x(0<x<3)
8
10
 
9
11
  時間は120sec,dt=6.0sec
10
12
 
11
13
 
12
14
 
15
+ ```
16
+
17
+ F_dy=lambda value: -self._a*value[1]*(self._b-math.exp(self._b-self._c*value[0]**4))
18
+
19
+ ```
20
+
21
+ この部分で、変数i,kをどのようにコードに組み込んだらいいのか分かりません。
22
+
13
23
  Python初心者なので分かりやすく教えていただきたいです。
24
+
25
+ 宜しくお願いします。
14
26
 
15
27
 
16
28
 
17
- ### 発生している問題・エラーメッセージ
18
-
19
- ---------------------------------------------------------------------------
20
-
21
- ValueError Traceback (most recent call last)
22
-
23
- <ipython-input-27-854e133ed99b> in <module>
24
-
25
- 25 dt=6.0
26
-
27
- 26 t=numpy.arange(0,120,dt)
28
-
29
- ---> 27 Phelps_Model(x,y,t).calc()
30
-
31
-
32
-
33
- <ipython-input-27-854e133ed99b> in calc(self)
34
-
35
- 15 def calc(self):
36
-
37
- 16 initval=[self._x,self._y]
38
-
39
- ---> 17 result=scipy.integrate.odeint(self.func, initval, self._t)
40
-
41
- 18 self.show(result[:].T[0],result[:].T[1])
42
-
43
- 19
44
-
45
-
46
-
47
- ~\anaconda3\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
48
-
49
- 231 mu = -1 # changed to zero inside function call
50
-
51
- 232
52
-
53
- --> 233 dt = np.diff(t)
54
-
55
- 234 if not((dt >= 0).all() or (dt <= 0).all()):
56
-
57
- 235 raise ValueError("The values in t must be monotonically increasing "
58
-
59
-
60
-
61
- <__array_function__ internals> in diff(*args, **kwargs)
62
-
63
-
64
-
65
- ~\anaconda3\lib\site-packages\numpy\lib\function_base.py in diff(a, n, axis, prepend, append)
66
-
67
- 1244 nd = a.ndim
68
-
69
- 1245 if nd == 0:
70
-
71
- -> 1246 raise ValueError("diff requires input that is at least one dimensional")
72
-
73
- 1247 axis = normalize_axis_index(axis, nd)
74
-
75
- 1248
76
-
77
-
78
-
79
- ValueError: diff requires input that is at least one dimensional
29
+ 現在のコードでのエラーはありません。
80
30
 
81
31
 
82
32
 
@@ -92,19 +42,25 @@
92
42
 
93
43
  import matplotlib.pyplot as plt
94
44
 
95
- import numpy
45
+ import numpy as np
46
+
47
+ import math
96
48
 
97
49
 
98
50
 
99
51
  class Phelps_Model(object):
100
52
 
101
- def __init__(self, a, b, c, t, dt, x, y):
53
+ def __init__(self, a, b, c, d, e, t, dt, x, y):
102
54
 
103
55
  self._a=a
104
56
 
105
57
  self._b=b
106
58
 
107
59
  self._c=c
60
+
61
+ self._d=d
62
+
63
+ self._e=e
108
64
 
109
65
  self._t=t
110
66
 
@@ -122,13 +78,11 @@
122
78
 
123
79
  resulty=[]
124
80
 
125
- F_dy=lambda value: -a*value[1]*(b-math.exp(b-c*value[0]**4))
81
+ F_dy=lambda value: -self._a*value[1]*(self._b-math.exp(self._b-self._c*value[0]**4))
126
82
 
127
83
  for i in range(0,len(self._t)):
128
84
 
129
85
  yy=F_dy([self._x,self._y])*self._dt
130
-
131
- self._x+=xx
132
86
 
133
87
  self._y+=yy
134
88
 
@@ -138,9 +92,13 @@
138
92
 
139
93
  self.show(resultx,resulty)
140
94
 
95
+
96
+
141
97
  def show(self,x,y):
142
98
 
143
99
  fig=plt.figure()
100
+
101
+ ax=fig.add_subplot()
144
102
 
145
103
  ax.plot(x,y,'o')
146
104
 
@@ -148,11 +106,11 @@
148
106
 
149
107
  plt.ylim(0, 100)
150
108
 
109
+ ax.set_xlabel('fiber length')
110
+
111
+ ax.set_ylabel('Number of fibers')
112
+
151
113
  plt.show()
152
-
153
-
154
-
155
- def Phelps_Model.calc()
156
114
 
157
115
 
158
116
 
@@ -162,10 +120,20 @@
162
120
 
163
121
  c=1.337
164
122
 
123
+ d=0.002
124
+
125
+ e=0.02
126
+
127
+ x=0.0
128
+
129
+ y=0.0
130
+
165
131
  dt=6.0
166
132
 
167
- t=numpy.arange(0,120,dt)
133
+ t=np.arange(0,120,dt)
168
134
 
135
+
136
+
169
- Phelps_Model(a,b,c,t,dt,x,y).calc()
137
+ Phelps_Model(a,b,c,d,e,t,dt,x,y).calc()
170
138
 
171
139
  ```

1

ソースコードを修正しました。宜しくお願い致します。

2021/05/25 07:48

投稿

keigo.lab
keigo.lab

スコア1

test CHANGED
File without changes
test CHANGED
@@ -88,43 +88,55 @@
88
88
 
89
89
 
90
90
 
91
+ ```
92
+
91
- import scipy.integrate
93
+ import matplotlib.pyplot as plt
92
94
 
93
95
  import numpy
94
-
95
- import matplotlib.pyplot as plt
96
96
 
97
97
 
98
98
 
99
99
  class Phelps_Model(object):
100
100
 
101
- def __init__(self,t,x,y):
101
+ def __init__(self, a, b, c, t, dt, x, y):
102
+
103
+ self._a=a
104
+
105
+ self._b=b
106
+
107
+ self._c=c
102
108
 
103
109
  self._t=t
110
+
111
+ self._dt=dt
104
112
 
105
113
  self._x=x
106
114
 
107
115
  self._y=y
108
116
 
109
-
110
-
111
- def func(self, result, t0):
117
+
112
-
113
- F_dy=lambda result: -0.02*result[1](1-exp(1-1.337*result[0]**4))
114
-
115
- return [F_dy(result)]
116
-
117
-
118
118
 
119
119
  def calc(self):
120
120
 
121
- initval=[self._x,self._y]
121
+ resultx=[]
122
122
 
123
- result=scipy.integrate.odeint(self.func, initval, self._t)
123
+ resulty=[]
124
124
 
125
- self.show(result[:].T[0],result[:].T[1])
125
+ F_dy=lambda value: -a*value[1]*(b-math.exp(b-c*value[0]**4))
126
126
 
127
-
127
+ for i in range(0,len(self._t)):
128
+
129
+ yy=F_dy([self._x,self._y])*self._dt
130
+
131
+ self._x+=xx
132
+
133
+ self._y+=yy
134
+
135
+ resultx.append(self._x)
136
+
137
+ resulty.append(self._y)
138
+
139
+ self.show(resultx,resulty)
128
140
 
129
141
  def show(self,x,y):
130
142
 
@@ -132,10 +144,28 @@
132
144
 
133
145
  ax.plot(x,y,'o')
134
146
 
147
+ plt.xlim(0, 4.0)
148
+
149
+ plt.ylim(0, 100)
150
+
135
151
  plt.show()
136
152
 
153
+
137
154
 
155
+ def Phelps_Model.calc()
156
+
157
+
158
+
159
+ a=0.2
160
+
161
+ b=1.0
162
+
163
+ c=1.337
138
164
 
139
165
  dt=6.0
140
166
 
141
167
  t=numpy.arange(0,120,dt)
168
+
169
+ Phelps_Model(a,b,c,t,dt,x,y).calc()
170
+
171
+ ```