Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting error function with some parameters

I'm trying to plot the error function in the form:

#!/usr/bin/env python
import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.special import erf
from scipy.integrate import quad
import scipy.integrate as integrate

def integrand(t, alpha, r ):
        return np.exp(-alpha*(t-r)**2)

def damp(alpha, rho, r):
        return quad(integrand, 0, rho, args=(alpha, r))[0]

def norm_constant(alpha, r):
        return 2.0*math.sqrt(2)/math.sqrt(math.pi) * (1./(1.- erf(-math.sqrt(np.abs(alpha))*r)))

A = 1.5
r = 0.3

g = [ norm_constant(A,r) *damp(A,x,r) for x in np.arange(-2,2,0.2)]
x = np.arange(-2,2,0.2)

A2 = 1.8
r2 = 0.3
g2 = [ norm_constant(A2,r2) *damp(A2,x,r2) for x in np.arange(-2,2,0.2)]
plt.plot(x,g,'r')
plt.plot(x,g2,'b')
plt.show()

I want to visualize how the error function changes when we play with Alpha and r. However, I am getting the error message in return:

  plt.plot(x,g,'r')
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3154, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1424, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 386, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 364, in _plot_args
    x, y = self._xy_from_xy(x, y)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 223, in _xy_from_xy
    raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

How can I accomplished my task?

like image 501
Monica Avatar asked Jan 22 '26 09:01

Monica


2 Answers

After a more careful look at your code I saw that in your first for loop you were using as your dummy variable x. Then, your next line of code is x = np.arange(-2,2,0.2), where you globally define x as a numpy.ndarray. Then you are using that same x that is globally defined in your second for loop as the argument of damp(A2,x,r2) and the dummy variable, which gave you that error, when trying to plot them. Apparently, Python3 has no problem with that and can run the program just fine.

To resolve this replace the x in your second for loop with another dummy variable:

g2 = [ norm_constant(A2,r2) *damp(A2,z,r2) for z in np.arange(-2,2,0.2)]

When done so, you should be getting the following figure:

fig1

like image 70
gnikit Avatar answered Jan 24 '26 22:01

gnikit


w1 = np.linspace(-6,9,11)

Plotting error function

l = 0
it = len(x)
for i in range(it):
    l+=(w1*x[i]- y[i])**2

plt.plot(w1,l)
plt.xlabel('W')
plt.ylabel('Error')
plt.title('W vs Error')
plt.scatter(w1,l,c='red')
plt.show()
like image 27
EVILIST JACK Avatar answered Jan 25 '26 00:01

EVILIST JACK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!