実現したいこと
Pythonのfor文を用いてdydx=3*xのyを1から9の範囲で求めたい。
前提
演算のプログラムを組んでいます。
発生している問題・エラーメッセージ
ValueError Traceback (most recent call last) <ipython-input-26-24ae3b4bdc29> in <cell line: 19>() 17 k_4=h*f(x+k_3/2) 18 y +=y+(k_1+k_2/2+k_3/2+k_4)/6 ---> 19 plt.plot(x,ypoints) 3 frames /usr/local/lib/python3.9/dist-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs, return_kwargs, ambiguous_fmt_datakey) 502 503 if x.shape[0] != y.shape[0]: --> 504 raise ValueError(f"x and y must have same first dimension, but " 505 f"have shapes {x.shape} and {y.shape}") 506 if x.ndim > 2 or y.ndim > 2: ValueError: x and y must have same first dimension, but have shapes (1,) and (9,)``` ### 該当のソースコード ```Python import numpy as np import matplotlib.pyplot as put #dydx=3x h=1 def f(x): return 3*x x=1 y=1.0 ypoints=[] for x in np.arange(1,10,1): ypoints.append(y) k_1=h*f(x) k_2=h*f(x+k_1/2) k_3=h*f(x+k_2/2) k_4=h*f(x+k_3/2) y +=y+(k_1+k_2/2+k_3/2+k_4)/6 plt.plot(x,ypoints)
試したこと
あらかじめ変数を定義しようとx=np.arange(1,10,1)という変数の定義を行ったが以下のエラーが発生した。
ValueError Traceback (most recent call last)
<ipython-input-27-b5a0c7c0d279> in <cell line: 19>()
17 k_4=h*f(x+k_3/2)
18 y +=y+(k_1+k_2/2+k_3/2+k_4)/6
---> 19 plt.plot(x,ypoints)
3 frames
/usr/local/lib/python3.9/dist-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs, return_kwargs, ambiguous_fmt_datakey)
502
503 if x.shape[0] != y.shape[0]:
--> 504 raise ValueError(f"x and y must have same first dimension, but "
505 f"have shapes {x.shape} and {y.shape}")
506 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (1,) and (9,)
0 コメント