0% found this document useful (0 votes)
3 views2 pages

Program Ode

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Program Ode

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program Code :

import numpy as np
import [Link] as plt
from [Link] import curve_fit

# Linear Regression

x_linear = [Link]([50, 70, 100, 120], dtype=float)


y_linear = [Link]([12, 15, 21, 25], dtype=float)

def leastsqmethod(x, y):


n = len(x)
sumx = [Link](x)
sumy = [Link](y)
sumx_y = [Link](x * y)
sumxsqr = [Link](x * x)

denominator = (n * sumxsqr - sumx ** 2)


a = (sumy * sumxsqr - sumx * sumx_y) / denominator
b = (n * sumx_y - sumx * sumy) / denominator
return a, b

def linfunc(x, a, b):


return a + b * x

a, b = leastsqmethod(x_linear, y_linear)
xline = [Link]([[Link](x_linear), [Link](x_linear)])
yline = linfunc(xline, a, b)

[Link](figsize=(10, 7))
[Link](311)
[Link](x_linear, y_linear, color="red", label="Data Points")
[Link](xline, yline, color="blue", label="Best Fit Line")
[Link]("x")
[Link]("y")
[Link]("Linear Regression")
[Link]()
[Link](True)

# Exponential Regression
x_exp = [Link]([1, 2, 3, 4, 5])
y_exp = [Link]([2, 4, 10, 30, 80])

def exponential(x, p, q):


return p * [Link](q * x)

params, covariance = curve_fit(exponential, x_exp, y_exp)


p, q = params

x_fine = [Link](min(x_exp), max(x_exp), 100)


y_fitted_exp = exponential(x_fine, p, q)

[Link](312)
[Link](x_exp, y_exp, color='blue', label='Data')
[Link](x_fine, y_fitted_exp, color='red', label=f'y={p:.6f}*[Link]({q:.6f}*x)')
[Link]('x')
[Link]('y')
[Link]('Exponential Regression')
[Link]()
[Link](True)
# Polynomial Regression
x_poly = [Link]([10, 12, 14, 16, 18], dtype=float)
y_poly = [Link]([14, 20, 24, 26, 35], dtype=float)

n = 3 # Degree 3 polynomial for variety


A = [Link]((n+1, n+1))
b_poly = [Link](n+1)

for i in range(n+1):
for j in range(n+1):
A[i, j] = [Link](x_poly**(i + j))

b_poly[i] = [Link](y_poly * x_poly**i)

p_poly = [Link](A, b_poly)


xp_poly = [Link](min(x_poly) - 1, max(x_poly) + 1, 200)
yp_poly = [Link]([Link](p_poly), xp_poly)

[Link](313)
[Link](x_poly, y_poly, color='red', label='Data points')
[Link](xp_poly, yp_poly, color='blue', label='Fitted polynomial')
[Link]('x')
[Link]('y')
[Link]('Polynomial Regression (Degree 3)')
[Link]()
[Link](True)

plt.tight_layout()
[Link]()

You might also like