0% found this document useful (0 votes)
29 views

curve fitting.ipynb - Colab

The document contains Python code for performing linear and polynomial regression using NumPy and Matplotlib. It includes two datasets, one for linear regression with an equation T = 0.0117L + 0.8454, and another for quadratic regression with the equation y = 0.9588x² + 0.2533x - 0.2610. The code visualizes the data points and the best-fit lines for both regression types.

Uploaded by

mitrajeet3577
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)
29 views

curve fitting.ipynb - Colab

The document contains Python code for performing linear and polynomial regression using NumPy and Matplotlib. It includes two datasets, one for linear regression with an equation T = 0.0117L + 0.8454, and another for quadratic regression with the equation y = 0.9588x² + 0.2533x - 0.2610. The code visualizes the data points and the best-fit lines for both regression types.

Uploaded by

mitrajeet3577
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
You are on page 1/ 2

import numpy as np

import matplotlib.pyplot as plt

# Given data
L = np.array([40, 50, 60, 70, 80, 90, 100, 110, 120]) # Independent variable (X)
T = np.array([1.313, 1.442, 1.557, 1.668, 1.787, 1.848, 2.016, 2.139, 2.273]) # Dependent variable (Y)

# Perform Linear Regression


m, b = np.polyfit(L, T, 1) # Linear fit (degree = 1)

# Predicted values
T_pred = m * L + b

# Print Equation of Line


print(f"Linear Regression Equation: T = {m:.4f}L + {b:.4f}")

# Plot the data points and the best-fit line


plt.scatter(L, T, color='blue', label='Data Points')
plt.plot(L, T_pred, color='red', label=f'Best Fit Line: T = {m:.4f}L + {b:.4f}')
plt.xlabel('L')
plt.ylabel('T')
plt.legend()
plt.title('Linear Regression: L vs T')
plt.show()

Linear Regression Equation: T = 0.0117L + 0.8454

Start coding or generate with AI.

import numpy as np
import matplotlib.pyplot as plt

# Given data
x = np.array([1, 2, 3, 4, 5, 6]) # Independent variable
y = np.array([1.01, 4.03, 8.98, 16.25, 25.01, 35.72]) # Dependent variable

# Fit a 2nd-degree polynomial (quadratic)


coefficients = np.polyfit(x, y, 2) # Finds a, b, c for y = ax^2 + bx + c
a, b, c = coefficients

# Generate fitted values


x_fit = np.linspace(min(x), max(x), 100) # More points for smooth curve
y_fit = a * x_fit**2 + b * x_fit + c

# Print the polynomial equation


print(f"Fitted Polynomial Equation: y = {a:.4f}x² + {b:.4f}x + {c:.4f}")

# Plot the data points


plt.scatter(x, y, color='blue', label='Data Points')

# Plot the polynomial regression curve


plt.plot(x_fit, y_fit, color='red', label='Best-Fit Quadratic Curve')

# Labels and title


plt.xlabel('x')
plt.ylabel('y')
plt.title('Polynomial Regression (Quadratic Fit)')
plt.legend()
plt.show()

Fitted Polynomial Equation: y = 0.9588x² + 0.2533x + -0.2610

 

You might also like