2017-01-14 6 views
0

私は以下のように積分結果をプロットしたいと思いますが、それは空白のシートであるようですが、何故その理由がありますか?私を助けてください!python科学プログラミングなぜ正しい結果でプロットするときに空白が発生するのですか?

# -*- coding: utf-8 -*- 
import matplotlib.pylab as plt 
import numpy as np 
import scipy as sp 
from scipy.integrate import quad, dblquad, tplquad 

x = np.arange(0,1,0.1) 
print ("x = ", x) 

def f(x): 
    return x 
print ("f(x) = ", f(x)) 

x_lower = 0 
for x_upper in x : 

    val, abserr = quad(f, x_lower, x_upper) 
    print ("integral value =", val, ", x_upper = ", x_upper ,", absolute error =", abserr) 
    plt.plot(x_upper, val, ' b--') 

plt.show() 

出力は、プロットは空白です!

x = [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 
f(x) = [ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 
integral value = 0.0 , x_upper = 0.0 , absolute error = 0.0 
integral value = 0.005000000000000001 , x_upper = 0.1 , absolute error = 5.551115123125784e-17 
integral value = 0.020000000000000004 , x_upper = 0.2 , absolute error = 2.2204460492503136e-16 
integral value = 0.04500000000000001 , x_upper = 0.3 , absolute error = 4.996003610813205e-16 
integral value = 0.08000000000000002 , x_upper = 0.4 , absolute error = 8.881784197001254e-16 
integral value = 0.125 , x_upper = 0.5 , absolute error = 1.3877787807814457e-15 
integral value = 0.18000000000000005 , x_upper = 0.6 , absolute error = 1.998401444325282e-15 
integral value = 0.24500000000000005 , x_upper = 0.7 , absolute error = 2.720046410331634e-15 
integral value = 0.32000000000000006 , x_upper = 0.8 , absolute error = 3.552713678800502e-15 
integral value = 0.40499999999999997 , x_upper = 0.9 , absolute error = 4.496403249731884e-15 

答えて

2

プロットに何も表示されない理由は、1つの単一点の複数の線プロットをプロットしているためです。行には開始と終了が必要なので(少なくとも2つの点を意味する)、グラフは空白のままです。

あなたのポイントを示すの最も簡単な方法は、marker="o"によってplt.plot()にお電話で' b--'を置き換えるために、次のようになります。

plt.plot(x_upper, val, marker="o", color="b") 

別のオプションは、最初のリスト内のすべての積分結果を収集して、プロットすることです

+0

涼しいです!私のような初心者にあなたの助けをありがとう!非常に便利な答えです!多くを学んだ!イエスはあなたを祝福します! –

+0

私はベクトル化されたメソッド 'integrate.nquad()'を使って解決策を進めていましたが、もっと高速でした...-) – MaxU

0

おそらく、1つのドットだけをプロットするたびに、行はプロットしないために、答えが見つかるかもしれません。 'bs'を設定すると、それは実際には空白ではなく、いくつかの点が見つかります。

関連する問題