Pythonで以下の二階常微分方程式をscimpyなどを使わずにEuler法を用いて解くという課題です。
x'' = -x
そこで様々なサイトなどを調べてどうにかこうにか以下のソースコードを書いたのですが、エラーが出てうまくいきません。
import numpy as np
import matplotlib.pyplot as plt
from math import*
f1 = 1
g1 = 0
dx = 0.01
for i in range(101):
x = i*dx f2 = f1 + dx*g1 g2 = g1 - dx*f1 f = cos(x) t_list.append(x) y1_list.append(f1) f1 = f2 g1 = g2 print('x = ', x) print('計算値 = ', f1) print('正確値 = ', f) print(' ')
plt.plot(x, y1_list, color='r')
plt.plot(x, np.cos(x), color='b')
plt.xlim(0.0, 1,.0)
plt.ylim(-1.0, 1.0)
plt.show()
これに対して現在出ているエラーは以下の通りです。
ValueError Traceback (most recent call last)
<ipython-input-9-d23a7437bae6> in <module>
31 print(' ')
32
---> 33 plt.plot(x, y1_list, color='r')
34 plt.plot(x, np.cos(x), color='b')
35
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
2793 return gca().plot(
2794 *args, scalex=scalex, scaley=scaley, **({"data": data} if data
-> 2795 is not None else {}), **kwargs)
2796
2797
~\Anaconda3\lib\site-packages\matplotlib\axes_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1664 """
1665 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
-> 1666 lines = [*self._get_lines(*args, data=data, **kwargs)]
1667 for line in lines:
1668 self.add_line(line)
~\Anaconda3\lib\site-packages\matplotlib\axes_base.py in call(self, *args, **kwargs)
223 this += args[0],
224 args = args[1:]
--> 225 yield from self._plot_args(this, kwargs)
226
227 def get_next_color(self):
~\Anaconda3\lib\site-packages\matplotlib\axes_base.py in _plot_args(self, tup, kwargs)
389 x, y = index_of(tup[-1])
390
--> 391 x, y = self._xy_from_xy(x, y)
392
393 if self.command == 'plot':
~\Anaconda3\lib\site-packages\matplotlib\axes_base.py in _xy_from_xy(self, x, y)
268 if x.shape[0] != y.shape[0]:
269 raise ValueError("x and y must have same first dimension, but "
--> 270 "have shapes {} and {}".format(x.shape, y.shape))
271 if x.ndim > 2 or y.ndim > 2:
272 raise ValueError("x and y can be no greater than 2-D, but have "
ValueError: x and y must have same first dimension, but have shapes (1,) and (808,)
何を言われているのか、何が間違っているのか、何をどうすればいいのか、いろいろ調べてもPythonの本を読んだりしてもよく分かりません。一週間格闘しましたがちっとも要領を得ません。
Pythonはおろか、プログラミング初心者です。
きっと根本的な、初歩的なミスがたくさんあるのだと思います。どなたか、ヒントを頂けませんか。
あなたの回答
tips
プレビュー