質問編集履歴
2
実現したい事の説明の改善をしました.
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
python 初心者です.
|
4
4
|
関数z(x)を微分して法線を求め,その法線の傾き値(度表記)を求めようとしています.
|
5
|
-
また,傾き値は
|
5
|
+
また,傾き値はその座標値(a,z(a))の値で変化すると思いますが,aが0.1間隔で0から6まで変化した分だけの情報(すばわち60個分の傾き値)が欲しいです.
|
6
6
|
下図がイメージです.
|
7
7
|
|
8
8
|
|
9
|
-

|
10
10
|
|
11
11
|
### 発生している問題・エラーメッセージ
|
12
12
|
|
1
編集後 のところに訂正して頂いたプログラムを基に,作成したプログラムを載せました.しかし,エラーが出てしまいます...
title
CHANGED
File without changes
|
body
CHANGED
@@ -57,10 +57,90 @@
|
|
57
57
|
微分を行うところまでは,teratailユーザーのご協力もあり出来ましたが,ここから先,行き詰っています.
|
58
58
|
知識のある方,ご協力をお願い致します.
|
59
59
|
|
60
|
+
# 編集後
|
61
|
+
```Python
|
60
|
-
#
|
62
|
+
#法線の角度を求める
|
61
63
|
|
64
|
+
import matplotlib.pyplot as plt
|
65
|
+
import numpy as np
|
66
|
+
from sympy import *
|
67
|
+
from sympy import Derivative, Symbol, N
|
62
68
|
|
63
69
|
|
70
|
+
|
71
|
+
# xを変数xに指定
|
72
|
+
x = Symbol('x')
|
73
|
+
z = -(c*x**2/(1+(1-(1+k)*c**2*x**2)**(1/2))
|
74
|
+
|
75
|
+
prime_z = Derivative(z, x).doit()
|
76
|
+
|
77
|
+
a = Symbol('a')
|
78
|
+
z_a = z.subs(x, a) # f(a)
|
79
|
+
prime_z_a = prime_z.subs(x, a) # 点 (a, f(a)) の傾き
|
80
|
+
tangent = prime_z_a * (x - a) + z_a # 点 (a, f(a)) の接線
|
81
|
+
normal = (-1 / prime_z_a) * (x - a) + z_a # 点 (a, f(a)) の法線
|
82
|
+
theta = deg(atan(-1 / prime_z_a)) # 法線の角度
|
83
|
+
print('{} degree'.format(theta.evalf(subs={a: x, prime_z_a: x})))
|
84
|
+
|
85
|
+
# x, theta の点一覧を作成する。
|
86
|
+
xs = np.arange(0, 6.28, 0.1)
|
87
|
+
thetas = [theta.evalf(subs={x: v}) for v in xs]
|
88
|
+
|
89
|
+
# 描画する。
|
90
|
+
fig, ax = plt.subplots(figsize=(7, 7))
|
91
|
+
plt.grid(True)
|
92
|
+
plt.xlabel('$x$', fontsize=16)
|
93
|
+
plt.ylabel('$θ$', fontsize=16)
|
94
|
+
plt.title('upper die')
|
95
|
+
plt.plot(xs, thetas)
|
96
|
+
plt.show()
|
97
|
+
|
98
|
+
np.savetxt("points.csv", np.vstack([xs,thetas]).T, delimiter=",")
|
99
|
+
```
|
100
|
+
|
101
|
+
を入力すると,
|
102
|
+
|
103
|
+
```
|
104
|
+
>>> # 描画する。
|
105
|
+
... fig, ax = plt.subplots(figsize=(7, 7))
|
106
|
+
>>> plt.grid(True)
|
107
|
+
>>> plt.xlabel('$x$', fontsize=16)
|
108
|
+
Text(0.5,0,'$x$')
|
109
|
+
>>> plt.ylabel('$θ$', fontsize=16)
|
110
|
+
Text(0,0.5,'$\x83\xc6$')
|
111
|
+
>>> plt.title('upper die')
|
112
|
+
Text(0.5,1,'upper die')
|
113
|
+
>>> plt.plot(xs, thetas)
|
114
|
+
Traceback (most recent call last):
|
115
|
+
File "<stdin>", line 1, in <module>
|
116
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\pyplot.py", line 3363, in plot
|
117
|
+
ret = ax.plot(*args, **kwargs)
|
118
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\__init__.py", line 1867, in inner
|
119
|
+
return func(ax, *args, **kwargs)
|
120
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\axes\_axes.py", line 1529, in plot
|
121
|
+
self.add_line(line)
|
122
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\axes\_base.py", line 1960, in add_line
|
123
|
+
self._update_line_limits(line)
|
124
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\axes\_base.py", line 1982, in _update_line_limits
|
125
|
+
path = line.get_path()
|
126
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\lines.py", line 956, in get_path
|
127
|
+
self.recache()
|
128
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\lines.py", line 657, in recache
|
129
|
+
y = _to_unmasked_float_array(yconv).ravel()
|
130
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\matplotlib\cbook\__init__.py", line 2052, in _to_unmasked_float_array
|
131
|
+
return np.asarray(x, float)
|
132
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\numpy\core\numeric.py", line 501, in asarray
|
133
|
+
return array(a, dtype, copy=False, order=order)
|
134
|
+
File "C:\Users\owner\Anaconda3\envs\python3.6\lib\site-packages\sympy\core\expr.py", line 225, in __float__
|
135
|
+
raise TypeError("can't convert expression to float")
|
136
|
+
TypeError: can't convert expression to float
|
137
|
+
>>> plt.show()
|
138
|
+
|
139
|
+
```
|
140
|
+
この様なエラーが出てしまいます.
|
141
|
+
|
142
|
+
|
143
|
+
|
64
144
|
### 補足情報(FW/ツールのバージョンなど)
|
65
145
|
|
66
146
|
python3.6を使用しています.
|