Calmcode - sympy: generalize maths

Generalize Maths

1 2 3 4 5 6

The goal is to have the variable c represent the circumference of the fence. By having this as a variable instead of a number we might be able to do more interesting things with it. We could for example ask the question, what if the circumference was bigger, what would the maximum area then be? Maths allows us to answer these sort of questions and sympy helps us automate it with python.

The code is listed below.

import sympy as sp
l, w = sp.symbols("l, w")
area = l * w
circumference = 2 * l + 2 * w
# define variable for circumference
c = sp.symbol("c")
l_expr = sp.solve(sp.Eq(circumference, c), l)[0]

opt_w = sp.solve(sp.diff(area.subs(l, l_expr), w), w)[0]
opt_l = l_expr.subs(w, opt_w)
opt_area = opt_w * opt_l
sp.plot(opt_area);