Homework 3: Over-Actutated Mathematics
Numerical integrators are like the over-caffeinated interns of the math world. They work tirelessly, step by tiny step, to estimate the area under a curve, often taking way too many steps just to be sure they got it right. While they may not have the elegance of an analytical solution, they certainly have the persistence. It’s as if they say, “Who needs elegance when you can brute-force your way to an answer with the precision of a squirrel gathering nuts for winter?”
Objective1
Create a new ROS workspace and populate it with the necessary files to create a numerical integrator node. This node will host a service server, where when prompted will do integration of a given function f(x)
between two values also defined in the service.
Use the following python code snippet to guide you with the required parsing implementation.
import sympy as sp
from scipy.integrate import quad
import numpy as np
def parse_function(func_str):
# Define the variable
x = sp.symbols('x')
# Parse the function
func = sp.sympify(func_str.replace('^', '**'))
return func, x
def integrate_function(func_str, a, b):
func, x = parse_function(func_str)
# Convert the sympy function to a numerical function
f_lambdified = sp.lambdify(x, func, 'numpy')
# Perform the numerical integration
result, error = quad(f_lambdified, a, b)
return result
Example usage of snippet:
func_str1 = "x^2 + 3"
func_str2 = "sin(x)"
a, b = 0, 1
print(f"Integral of {func_str1} from {a} to {b} is: {integrate_function(func_str1, a, b)}")
print(f"Integral of {func_str2} from {a} to {b} is: {integrate_function(func_str2, a, b)}")
Objective 2
Action Server
Write an action server that would perfrom a monte-carlo estimation of $\pi$. It will perform it using $N$ defined points in the action goal
. The feedback will be given $10$ times (feedback every $\lfloor N/10 \rfloor$).
Submission Guideline
Refer to the tutorial 2: submission guideline.