</>
Skip to content
SciPy lessons (3/25)

SciPy — Get Started

First SciPy program

from scipy import optimize
import numpy as np

# Define a function
def f(x):
    return x**2 + 5*x + 6

# Find the minimum
result = optimize.minimize_scalar(f)
print(f"Minimum at x = {result.x:.2f}")
print(f"Minimum value = {result.fun:.2f}")

Basic optimization

from scipy.optimize import minimize

def objective(x):
    return (x[0] - 1)**2 + (x[1] - 2)**2

x0 = [0, 0]  # Initial guess
result = minimize(objective, x0)
print(f"Optimal: x = {result.x}")

Linear algebra

from scipy import linalg
import numpy as np

A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])

# Solve Ax = b
x = linalg.solve(A, b)
print(f"Solution: {x}")

Integration

from scipy import integrate

# Integrate x^2 from 0 to 1
result, error = integrate.quad(lambda x: x**2, 0, 1)
print(f"Integral = {result:.4f}")

Interpolation

from scipy import interpolate
import numpy as np

x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 9, 16])

f = interpolate.interp1d(x, y)
print(f"Value at 2.5 = {f(2.5):.2f}")

Mini Practice

  1. Optimize a 2D function
  2. Solve a linear system
  3. Compute an integral
  4. Interpolate data points

Up Next

Continue with Installation - Setting up SciPy.

Related Topics

Frequently Asked Questions about Get Started

What is Get Started in SciPy?

Get Started is a fundamental concept in SciPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Get Started?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Get Started.

Why is Get Started important in SciPy?

Get Started is essential for SciPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.