Newer
Older
abgabensammlungSS15 / ea / Ub4 / a12.py
@Jan-Peter Hohloch Jan-Peter Hohloch on 18 May 2015 1 KB EA: python3
#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt
import copy as cp

def binary (b, u=31, l=-32, length=6):
    s=0
    for i in np.arange(1,length+1) :
        s+=b[length-i]*2**(i-1)
    return l+(u-l)/(2**length-1)*s

def f1 (x):
    return x**2

def matchScheme (scheme):
    ind=[]
    ind.append([])
    if(scheme[0]==0) or (scheme[0]==1):
        ind[0]=[scheme[i]]
        c=1
    else:
        ind[0]=[0]
        ind.append([1])
        c=2
    for i in np.arange(1,len(scheme)):
        if (scheme[i] == 0) or (scheme[i] == 1):
            for j in np.arange(0,c):
                ind[j].append(scheme[i])
        else :
            ind2=cp.deepcopy(ind)
            for j in np.arange(0,c):
                ind.append(ind2[j])
            for j in np.arange(0,c):
                ind[j].append(0)
            for j in np.arange(c,2*c):
                ind[j].append(1)
            c=2*c
    return ind

x=np.arange(-32,32)
plt.scatter(x,f1(x), label='x^2')
#plt.grid(True)

H3=['*',1,1,'*','*','*']
H3scheme=list(map(binary,matchScheme(H3)))
H4=['*','*','*',0,0,'*']
H4scheme=list(map(binary,matchScheme(H4)))

plt.vlines(H3scheme,list([0]*len(H3scheme)), list(map(f1,H3scheme)), linestyles='dashed', label='H3')
plt.vlines(H4scheme,list([0]*len(H4scheme)), list(map(f1,H4scheme)), linestyles='dotted', label='H4')
plt.legend(bbox_to_anchor=(0.6,0.8))

plt.show()