You are on page 1of 11

9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [6]: 

import matplotlib.pyplot as plt


import numpy as np
import random as rnd

x = np.array(range(1,11))

a = 2
b = 0.1
c = 0.1
d = 0.0015
e = 0.00001

y = a+b*x+c*(x**2)+d*(x**3)+e*(x**4)

rng = np.random.default_rng()
yn = y + 0.5*rng.normal (size = 10)
plt.plot(x,y,'b-')
plt.plot(x,yn,'ro')
plt.xlabel("x")
plt.ylabel("y")
plt.xlim(0, 11)
plt.ylim(0, 15)

plt.show
print(y)
print(yn)

[ 2.20151 2.61216 3.24131 4.09856 5.19375 6.53696 8.13851 10.00896


12.15911 14.6 ]
[ 2.08534512 3.01663535 2.79467886 4.34296307 5.00974082 6.65848232
8.39269201 9.70604377 11.37417554 14.71182439]

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 1/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 2/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [2]: 

import numpy as np
import matplotlib.pyplot as plt
import random as rnd

x = np.array(range(-10,10,1))
a = 1
b = 0.1
c = 0.1
d = 0.015
e = 0.00001

G = [np.ones(20), x, x**2, x**3, x**4]


Gt = np.transpose(G) #G sudah tertranspose

m = np.array([a,b,c,d,e])

y = np.matmul(Gt,m)

rng = np.random.default_rng()
yn = y + 0.5*rng.normal(size=20)

#cek misfit data sintetis


misfit_1 = sum((yn -y)**2)
erms_1 = (misfit_1/len(x))**0.5
print("Misfit 1:", misfit_1)
print("RMSE:", erms_1)

gtg = np.matmul(G,Gt)
gtginv = np.linalg.inv(gtg) #invert matrix
gtd = np.matmul(G,yn)

minv = np.matmul(gtginv,gtd)
print ("minv", minv)

#inversi model respon


ycal = np.matmul(Gt,minv)
print("ycal", ycal) #cek nilai - yn

#misfit
misfit = sum((yn -ycal)**2)
print("misfit", misfit)
erms = (misfit/len(x))**0.5
print(erms)

plt.plot(x, ycal, 'b-')


plt.plot(x, yn,'r+')
plt.title('simple problem')
plt.xlabel("x")
plt.ylabel("y")
plt.xlim(-10,10)
plt.ylim(-10,10)

print("x",x)
print("y",y)

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 3/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

print("yn", yn)
Misfit 1: 5.002068674053312
RMSE: 0.5001034230063474
minv [ 9.13109646e-01 1.39497604e-01 1.09524682e-01 1.44116265e-02
-9.61532180e-05]
ycal [-4.90255688 -2.60780654 -0.96588789 0.12928407 0.78148668 1.09218
96
1.16055482 1.08343664 0.9553817 0.86862894 0.91310965 1.17644741
1.74395814 2.6986501 4.12122385 6.09007227 8.68128058 11.96862631
16.02357931 20.91530178]
misfit 4.743536915971415
0.4870080551680544
x [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
8 9]
y [-4.9 -2.66939 -1.03904 0.07901 0.77296 1.13125 1.24256 1.19581
1.08016 0.98501 1. 1.21501 1.72016 2.60581 3.96256 5.88125
8.45296 11.76901 15.92096 21.00061]
yn [-5.03598222 -2.38501785 -1.40409834 0.45581155 1.12658912 1.3753675
7
0.71931134 1.54256314 -0.43511173 1.24282916 0.33304448 1.88473806
2.30312593 2.94306949 3.77478389 6.15331679 8.73273922 11.43078258
15.98675056 21.18234779]

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 4/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [2]: 

import numpy as np
import matplotlib.pyplot as plt
import random as rnd

x = np.linspace (1,10,10)
a = 2
b = 0.2
c = 0.1
#d = 0.015
#e = 0.00001

G = [np.ones(10), x,x**2]
Gt = np.transpose(G)
m = np.array([a,b,c])

y = np.matmul(Gt,m)

yn = y +0.5*np.random.randn()

plt.plot(x, y, '-')
plt.plot(x,yn,'x')
plt.title('simple problem')

print("x",x)
print("y",y)
print("yn", yn)

# Data yang akan disimpan


data = {'x': x, 'y': y, 'yn': yn}

# Nama file txt yang ingin Anda buat/simpan


nama_file = "Data Inversi.txt"

# Membuka file untuk menulis dan menyimpan data


with open(nama_file, "w") as file:
# Menulis data x, y, dan yn ke dalam file dengan spasi
file.write("x y yn\n") # Membuat header dengan spasi
for i in range(len(x)):
file.write(f"{x[i]} {y[i]} {yn[i]}\n") # Menambahkan spasi antara data

print(f"Data telah disimpan dalam file {nama_file}")

x [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
y [ 2.3 2.8 3.5 4.4 5.5 6.8 8.3 10. 11.9 14. ]
yn [ 2.10674372 2.60674372 3.30674372 4.20674372 5.30674372 6.6067437
2
8.10674372 9.80674372 11.70674372 13.80674372]
Data telah disimpan dalam file Data Inversi.txt

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 5/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [ ]: 

import numpy as np
import matplotlib.pyplot as plt
import random as rnd

x = np.array(range(-10,10,1))
a = 1
b= 0.1
c = 0.1
d = 0.015
e = 0.00001

Gt = [np.ones(20), x, x**2, x**3, x**4]


G = np.transpose(Gt)

m = np.array([a, b, c, d, e])

y = np.matmul(G,m)

rng = np.random.default_rng()
yn = y + 0.5*rng.normal(size =20)

misfit = sum((yn-y))**2
RMSE = (misfit/len(x))**0.5

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 6/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [3]: 

import numpy as np
import matplotlib.pyplot as plt

# Data Anda
data_x = np.array([1.002284498, 1.585363635, 3.4110445, 4.016840429, 5.206796717])
data_y = np.array([1.195105552, 1.985314114, 2.392273696, 3.495687709, 3.495687709])

# Melakukan regresi linear


a, b = np.polyfit(data_x, data_y, 1) # 1 menunjukkan regresi linier

# Plot data dan garis regresi


plt.scatter(data_x, data_y, label='Data')
plt.plot(data_x, a * data_x + b, color='red', label=f'Regression Line: Y = {a:.2f}X + {b
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.grid(True)
plt.show()

# Output nilai a dan b


print(f'Nilai a (slope): {a:.2f}')
print(f'Nilai b (intercept): {b:.2f}')

Nilai a (slope): 0.54


Nilai b (intercept): 0.87

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 7/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [4]: 

import numpy as np
import matplotlib.pyplot as plt
import random as rnd

x = np.array([1.002284498, 1.585363635, 3.4110445, 4.016840429, 5.206796717])


y_i = np.array([1.195105552, 1.985314114, 2.392273696, 3.495687709, 3.495687709])

a = 0.5
b = 0.8

Gt = [np.ones(5), x, x**2]
G = np.transpose(Gt) #G sudah tertranspose

m = np.array([a,b])

y = np.matmul(G,m)

#cek misfit data sintetis


misfit_1 = sum((y-y_i)**2)
erms_1 = (misfit_1/len(x))**0.5
print("Misfit 1:", misfit_1)
print("RMSE:", erms_1)

gtg = np.matmul(Gt,G)
gtginv = np.linalg.inv(gtg) #invert matrix
gtd = np.matmul(G,y)

minv = np.matmul(gtginv,gtd)
print ("minv", minv)

#inversi model respon


ycal = np.matmul(G,minv)
print("ycal", ycal) #cek nilai - yn

#misfit
misfit = sum((y-ycal)**2)
print("misfit", misfit)
erms = (misfit/len(x))**0.5
print(erms)

plt.plot(x, ycal, 'b-')


plt.plot(x, y,'r+')
plt.title('simple problem')
plt.xlabel("x")
plt.ylabel("y")
plt.xlim(-10,10)
plt.ylim(-10,10)

print("x",x)
print("y",y_i)
print("y", y)

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 8/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

--------------------------------------------------------------------------
-
ValueError Traceback (most recent call las
t)
~\AppData\Local\Temp/ipykernel_20076/1531267816.py in <module>
16 m = np.array([a,b])
17
---> 18 y = np.matmul(G,m)
19
20 #cek misfit data sintetis

ValueError: matmul: Input operand 1 has a mismatch in its core dimension


0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from
3)

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 9/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [6]: 

import numpy as np
import matplotlib.pyplot as plt
import random

x = np.array(range(-10, 10, 1))


a = 1
b = 0.1
c = 0.1
d = 0.015
e = 0.00001

G = [np.ones(20), x, x**2, x**3, x**4]


Gt = np.transpose(G)
m = np.array([a, b, c, d, e])

y = np.matmul(Gt, m)

err = np.random.rand(1, 21)

randmat = np.random.randint(-3, 3, size = (20))


qq = random.choice((-2, 2, 1, 0.4))
yn = y + randmat*0.4

plt.plot(x, y, 'r-')
plt.plot(x, yn, 'bo')
plt.plot()

#Inversi
gtg = np.matmul(G, Gt)
gtg_inv = np.linalg.inv(gtg)
gtd = np.matmul(G, yn)
m_inv = np.matmul(gtg_inv, gtd)

ycal = np.matmul(Gt, m_inv)

plt.plot(x, y, 'r-')
plt.plot(x, yn, 'bo')
plt.plot(x, ycal, 'y')
e = 1
plt.errorbar(x, ycal, yerr = e, fmt = '.', linewidth = 1, capsize = 5)
plt.plot()

Out[6]:

[]

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 10/11


9/26/23, 1:20 PM Inversi Linear - Jupyter Notebook

In [7]: 

plt.fill_between(x, ycal + e, ycal - e)


plt.plot(x, ycal, 'y')
plt.plot()

Out[7]:

[]

In [ ]: 

localhost:8888/notebooks/Downloads/MT/Inversi Linear.ipynb 11/11

You might also like