HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Labs
  2. /Convexity on a grid
MathematicsMediumHigh School (11–12)Inequalities and the AM, GM and HM Means

Convexity on a grid

You will test convexity with numbers and measure the gap in Jensen's inequality.

The idea. For a small step hhh, the second difference f(x+h)−2f(x)+f(x−h)f(x + h) - 2f(x) + f(x - h)f(x+h)−2f(x)+f(x−h) is about h2f′′(x)h^2 f''(x)h2f′′(x). A function is convex on a grid when every second difference is at least 000 (we allow −1e−12-1e-12−1e−12 for rounding). The Jensen gap of a list is the mean of fff minus fff of the mean.

Your tasks.

  1. Finish second_diff(f, x, h).
  2. Finish convex_on(f, lo, hi, n). Use the points lo + (hi - lo) * i / n for i = 1 to n - 1, with h = (hi - lo) / n, and return True when no second difference is below -1e-12.
  3. Finish jensen_gap(f, xs).

Run it first, just as it is. The checker at the end prints FAIL for each task. Then do the tasks one at a time, and run the program again after each one. A Hint appears under every check that still fails.

When every line says PASS, try convex_on(math.sin, 0, math.pi, 100) and convex_on(lambda x: -math.sin(x), 0, math.pi, 100). Which one is True, and why?

StemPy LabContributed by StemPy Lab
python
Loading Python runtime…

A worked solution

Run your code once to unlock the worked solution.

Have you tried it?

Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.

Log in to save
← All Labs
← Best and worst pairingsThe point that minimises total distance →
More onInequalities and the AM, GM and HM Means

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Convex functions and Jensen's inequalityConvexity, Jensen and Inequality Techniques

More labs on this topic

  1. The point that minimises total distanceMedium
  2. Best and worst pairingsMedium
  3. Testing Cauchy-SchwarzMedium
  4. Least values by search and by AM-GMMedium
Three means and their orderMedium
All labs on this topic →