{"id":4349,"date":"2026-07-28T07:06:11","date_gmt":"2026-07-28T11:06:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/matlab\/?p=4349"},"modified":"2026-07-28T07:09:38","modified_gmt":"2026-07-28T11:09:38","slug":"using-claude-code-and-matlab-to-find-a-new-way-to-estimate-pi","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/matlab\/2026\/07\/28\/using-claude-code-and-matlab-to-find-a-new-way-to-estimate-pi\/","title":{"rendered":"Using Claude Code and MATLAB to Find a New Way to Estimate Pi"},"content":{"rendered":"<div class = rtcContent><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Bio<\/span><span class=\"_richTextNode\">: Today's guest blogger is <\/span><a href = \"https:\/\/www.linkedin.com\/in\/charlie-saunders-82b091179\/\"><span class=\"_richTextNode\" style=' font-style: italic;'>Charlie Saunders<\/span><\/a><span class=\"_richTextNode\" style=' font-style: italic;'>. Charlie is a Senior Research Scientist in the MathWorks Advanced Research and Technology Office.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Recently, I came across a <\/span><a href = \"https:\/\/arxiv.org\/abs\/2602.14487\"><span class=\"_richTextNode\">cool result<\/span><\/a><span class=\"_richTextNode\"> about calculating <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\"> via coin flips. You may have seen other physical processes used to calculate <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">, too, such as throwing darts at a square and seeing what ratio land within a circle inside it. This one is a fun classroom demo, but the convergence is terrible... and you might not want to hand darts to a classroom of young kids! But this got me wondering whether there other simple random processes that produce <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">and are suitable to try out in a classroom. And, could I use an AI agent to help me search for one?<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">I spent an afternoon with Claude Code connected to a live MATLAB session using the <\/span><a href = \"https:\/\/www.mathworks.com\/products\/matlab-agentic-toolkit.html\"><span class=\"_richTextNode\">MATLAB Agentic Toolkit <\/span><\/a><span class=\"_richTextNode\">and MCP server. My goal was to use Claude as a fast experimental partner by having it help brainstorm candidate processes, write and run MATLAB simulations, and then help check the algebra when something looked promising.<\/span><\/div><h2  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 25px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">The Workflow: Claude Code + MATLAB MCP<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Claude Code is a command-line AI agent that can read and write files, run commands, and interact with tools from the terminal. For this project, the important part was that it could write MATLAB code, run it through the MCP connection, inspect the output, and then revise the next attempt without me having to manually intervene too much.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The loop went like this:<\/span><\/div><ul  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\">I described the main goal and a few starting approaches to consider: stopping rules, Poisson arrivals, and variants of coin-flip or birthday-problem processes. Claude then suggested some additional families of random processes to try.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\">For each candidate, Claude wrote a MATLAB simulation, ran it through the MCP server, and analyzed the results.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\">We dropped ideas that did not converge to <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">, converged too slowly, or appeared to have systematic bias.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\">For the methods that survived numerically, I asked Claude to use  <\/span><a href = \"https:\/\/www.mathworks.com\/products\/symbolic.html\"><span class=\"_richTextNode\">Symbolic Math Toolbox<\/span><\/a><span class=\"_richTextNode\">\u2122 to check whether there was a simple closed-form proof.<\/span><\/li><\/ul><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">This workflow was great, as I could test a half-formed \"what if...\" idea almost immediately, instead of spending an hour doing algebra or hand-writing experimental code to check an approach only to find it wasn't working.<\/span><\/div><h2  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 25px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">The Discovery: A Marble Bag Encodes Pi<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">We started by testing a whole bunch of ideas. I found a birthday-problem approach (how many people until two share a birthday?) that gives <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\"> only approximately, with bias that depends on the number of possible birthdays. Counting ties in coin flips was far too noisy to be practical. And an early version of the marble bag where you add one blue marble per round instead of two produced an answer close to <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">, yet upon further investigation resulted in <\/span><span texencoding=\"(8 log(2) + 4)\/3\" style=\"vertical-align:-5px\">$ (8 log(2) + 4)\/3 $<\/span><span class=\"_richTextNode\"> instead.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The idea we ended with came from exploring hazard-rate processes: random stopping games where the probability of stopping changes each round. After testing several stopping rules in MATLAB, we came across one where the probability of stopping on round k, assuming the game has reached that round, is 1\/(2k). At first, this may not sound especially connected to <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">. But this particular stopping rule has a useful hidden structure. The probability of surviving the first n rounds becomes<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span texencoding=\"S(n)=\\frac{\\binom{2n}{n}}{4^n}\" style=\"vertical-align:-17px\">$ S(n)=\\frac{\\binom{2n}{n}}{4^n} $<\/span><span class=\"_richTextNode\">,<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">which involves the central binomial coefficients. Those same coefficients appear in the Taylor series for <\/span><span texencoding=\"\\arcsin(x)\" style=\"vertical-align:-5px\">$ \\arcsin(x) $<\/span><span class=\"_richTextNode\">, and when that series is evaluated at <\/span><span texencoding=\"x=1\" style=\"vertical-align:-5px\">$ x=1 $<\/span><span class=\"_richTextNode\">, <\/span><span texencoding=\"\\arcsin(1)=\\pi\/2\" style=\"vertical-align:-5px\">$ \\arcsin(1)=\\pi\/2 $<\/span><span class=\"_richTextNode\">. So this was the first real clue that the stopping rule might be more than a numerical accident.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The remaining question was whether we could turn this abstract stopping rule into something simple enough to do physically.<\/span><\/div><h2  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 25px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">The Marble Bag<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Start with 1 red marble and 1 blue marble in the bag. Each round, draw a marble at random. If it is red, the game ends. If it is blue, put it back, add two more blue marbles, and draw again.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"http:\/\/blogs.mathworks.com\/matlab\/files\/2026\/07\/marblePi_1.png\" width = \"344\" height = \"518\" alt = \"\" style = \"vertical-align: baseline; width: 344px; height: 518px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Here is what happens:<\/span><\/div><ul  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Round 1:<\/span><span class=\"_richTextNode\"> Bag has 1 red + 1 blue (2 marbles). Draw. P(red) = 1\/2.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Round 2:<\/span><span class=\"_richTextNode\"> Bag now has 1 red + 3 blue (4 marbles). Draw. P(red) = 1\/4.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Round 3:<\/span><span class=\"_richTextNode\"> Bag now has 1 red + 5 blue (6 marbles). Draw. P(red) = 1\/6.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Round k:<\/span><span class=\"_richTextNode\"> Bag has 1 red + (2k-1) blue = 2k marbles. P(red) = 1\/(2k).<\/span><\/li><\/ul><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Record the stopping round <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c4<\/span><span class=\"_richTextNode\">. Compute the score: <\/span><span texencoding=\"\\tau \/ (2\\tau - 1)\" style=\"vertical-align:-5px\">$ \\tau \/ (2\\tau - 1) $<\/span><span class=\"_richTextNode\">. Repeat many times, and... the average score converges to <\/span><span texencoding=\"\\pi\/4\" style=\"vertical-align:-5px\">$ \\pi\/4 $<\/span><span class=\"_richTextNode\">! (a slice of Pi, if you will). <\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">This sounds reasonable, but does it actually work? I asked Claude to write a quick simulation and run it using the MATLAB MCP server to test this out. Here's the MATLAB code it ran:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >N = 10000;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >tau_vals = zeros(N, 1);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: rgb(14, 0, 255);\">for <\/span><span >t = 1:N<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >    k = 1;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >    <\/span><span style=\"color: rgb(14, 0, 255);\">while <\/span><span >rand() &gt;= 1\/(2*k)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >        k = k + 1;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >    <\/span><span style=\"color: rgb(14, 0, 255);\">end<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >    tau_vals(t) = k;<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: rgb(14, 0, 255);\">end<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >scores = tau_vals .\/ (2*tau_vals - 1);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >running_est = 4 * cumsum(scores) .\/ (1:N)';<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >fprintf(<\/span><span style=\"color: rgb(167, 9, 245);\">'After 100 games:  pi ~ %.4f\\n'<\/span><span >, running_est(100));<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '>&nbsp;<\/div><\/div><\/div><div  style = 'margin: 10px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"http:\/\/blogs.mathworks.com\/matlab\/files\/2026\/07\/marblePi_2.png\" width = \"560\" height = \"280\" alt = \"\" style = \"vertical-align: baseline; width: 560px; height: 280px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The estimate is pretty good! With 100 games, the mean absolute error was only about 0.06, or roughly 2% of <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">. We also see that the estimator is unbiased in expectation (and doesn't depend on anyone having good aim with a dart!).<\/span><\/div><h2  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 25px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">Proving It: The Symbolic Math Toolbox<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Without leaving the conversation, I then asked Claude to switch from performing numerical analysis with MATLAB to using the Symbolic Math Toolbox to prove the result in closed form. I had not tried this before so I was unsure how well it would work. But to my delight, it wrote the symbolic code, executed it through MCP, and returned an exact answer on the first try.<\/span><\/div><h3  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 20.4px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">The One-Line Proof<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The stopping probability 1\/(2k) at each round produces a PMF:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span texencoding=\"P(\\tau = k) = \\frac{\\binom{2k-2}{k-1}}{k \\cdot 2^{2k-1}}\" style=\"vertical-align:-17px\">$ P(\\tau = k) = \\frac{\\binom{2k-2}{k-1}}{k \\cdot 2^{2k-1}} $<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Claude defined this symbolically and asked MATLAB for the expected score:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >syms <\/span><span style=\"color: rgb(167, 9, 245);\">k<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >pmf = nchoosek(sym(2*k-2), k-1) \/ (k * 2^(2*k-1));<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >score = k \/ (2*k - 1);<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: rgb(0, 128, 19);\">% The one-line proof:<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 0px none rgb(33, 33, 33); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >E_score = symsum(score * pmf, k, 1, inf)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: rgb(0, 128, 19);\">% Returns: E_score = pi\/4<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">So with one call to <\/span><span class=\"_richTextNode\" style=' font-style: italic;'>symsum<\/span><span class=\"_richTextNode\">, the Symbolic Math Toolbox returns <\/span><span texencoding=\"\\pi\/4\" style=\"vertical-align:-5px\">$ \\pi\/4 $<\/span><span class=\"_richTextNode\">.<\/span><\/div><h3  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 20.4px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">Where Does Pi Come From?<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The identity <\/span><span texencoding=\"\\tau\/(2\\tau-1) = 1\/2 + 1\/(2(2\\tau-1))\" style=\"vertical-align:-5px\">$ \\tau\/(2\\tau-1) = 1\/2 + 1\/(2(2\\tau-1)) $<\/span><span class=\"_richTextNode\"> means:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span texencoding=\"E[\\tau\/(2\\tau-1)] = \\frac{1}{2} + \\frac{1}{2} \\cdot E[1\/(2\\tau-1)]\" style=\"vertical-align:-15px\">$ E[\\tau\/(2\\tau-1)] = \\frac{1}{2} + \\frac{1}{2} \\cdot E[1\/(2\\tau-1)] $<\/span><span class=\"_richTextNode\">.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The Symbolic Toolbox evaluates that inner expectation:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >E_inv = symsum(pmf \/ (2*k - 1), k, 1, inf)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span style=\"color: rgb(0, 128, 19);\">% ans = pi\/2 - 1<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">This traces back to the arcsin series, one of the classical representations of <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">. Combining:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span texencoding=\"E[\\tau\/(2\\tau-1)] = 1\/2 + (1\/2)(\\pi\/2 - 1) = \\pi\/4\" style=\"vertical-align:-5px\">$ E[\\tau\/(2\\tau-1)] = 1\/2 + (1\/2)(\\pi\/2 - 1) = \\pi\/4 $<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">If you haven't seen these before: the first series is the Taylor expansion of arcsin(x) at x = 1 (see <\/span><a href = \"https:\/\/en.wikipedia.org\/wiki\/Inverse_trigonometric_functions#Infinite_series\"><span class=\"_richTextNode\">here<\/span><\/a><span class=\"_richTextNode\"> for more detail). The second sums the Catalan numbers with geometric weights (see <\/span><a href = \"https:\/\/oeis.org\/A000108\"><span class=\"_richTextNode\">here<\/span><\/a><span class=\"_richTextNode\">). The partial fraction step shows how our score function decomposes into one piece from each series, and that's where <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\"> sneaks in!<\/span><\/div><h3  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 20.4px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 17px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">How long will a game last?<\/span><\/h3><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">We saw from our Monte Carlo simulations that the stopping round <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c4<\/span><span class=\"_richTextNode\"> is heavily right-skewed. You draw red on round 1 about half the time, but occasionally the game runs for 10+ rounds.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><img class = \"imageNode\" src = \"http:\/\/blogs.mathworks.com\/matlab\/files\/2026\/07\/marblePi_3.png\" width = \"508\" height = \"254\" alt = \"\" style = \"vertical-align: baseline; width: 508px; height: 254px;\"><\/img><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">We asked Claude to verify that the PMF sums to 1 and explore the moments:<\/span><\/div><div style=\"background-color: #F5F5F5; margin: 10px 0 10px 0;\"><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 1px solid rgb(217, 217, 217); border-bottom: 0px none rgb(33, 33, 33); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >symsum(pmf, k, 1, inf)     <\/span><span style=\"color: rgb(0, 128, 19);\">% ans = 1    (valid PMF)<\/span><\/span><\/div><\/div><div class=\"inlineWrapper\"><div  style = 'border-left: 1px solid rgb(217, 217, 217); border-right: 1px solid rgb(217, 217, 217); border-top: 0px none rgb(33, 33, 33); border-bottom: 1px solid rgb(217, 217, 217); border-radius: 0px 0px 4px 4px; padding: 0px 45px 4px 13px; line-height: 18.004px; min-height: 0px; white-space: nowrap; color: rgb(33, 33, 33); font-family: Menlo, Monaco, Consolas, \"Courier New\", monospace, Menlo, Monaco, Consolas, \"Courier New\", monospace; font-size: 14px; '><span style=\"white-space: pre\"><span >symsum(k * pmf, k, 1, inf) <\/span><span style=\"color: rgb(0, 128, 19);\">% ans = Inf  (infinite expected game length!)<\/span><\/span><\/div><\/div><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">That second result is a lovely surprise. The game has infinite expected length, even though the score <\/span><span texencoding=\"\\tau\/(2\\tau-1)\" style=\"vertical-align:-5px\">$ \\tau\/(2\\tau-1) $<\/span><span class=\"_richTextNode\"> has a perfectly finite mean.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">This does have an implication for the physical version of the game. Most games finish quickly: 50% stop on round 1, and about 75% stop within 5 rounds. But occasionally, a game can keep going for a very long time. That is fine for MATLAB, but tedious if you are drawing marbles by hand. In fact, whilst discussing this post, Mike Croucher had a game that ran for 3,997,135,355 draws: to do that with marbles, you'd need 40,000 metric tonnes of marbles and ~125 years!<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">I posed this issue to Claude, asking whether there's an unbiased way to cap the game at a set number of rounds. It turns out, there is an elegant solution. If red has not appeared by round M, stop and record a fixed replacement score equal to the average score you would expect from the unfinished tail:<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span texencoding=\"c_M = E\\left[\\frac{\\tau}{2\\tau-1} \\mid \\tau &gt; M\\right]\" style=\"vertical-align:-15px\">$ c_M = E\\left[\\frac{\\tau}{2\\tau-1} \\mid \\tau > M\\right] $<\/span><span class=\"_richTextNode\">.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">I felt that 5 rounds seems reasonable. For M=5, this replacement score is approximately <\/span><span texencoding=\"c_5 = 0.5162\" style=\"vertical-align:-6px\">$ c_5 = 0.5162 $<\/span><span class=\"_richTextNode\">.<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">So the practical classroom version works like this: draw at most 5 times. If you hit red, record <\/span><span texencoding=\"\\tau\/(2\\tau-1)\" style=\"vertical-align:-5px\">$ \\tau\/(2\\tau-1) $<\/span><span class=\"_richTextNode\">. If you do not hit red in 5 draws, record 0.5162. About 25% of games are truncated, but the estimator remains unbiased because those unfinished games are replaced by their exact conditional average.<\/span><\/div><h2  style = 'margin: 3px 10px 5px 4px; padding: 0px; line-height: 25px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left; '><span class=\"_richTextNode\">What Made This Work<\/span><\/h2><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">Looking back, three things came together to make this feasible in an afternoon:<\/span><\/div><ul  style = 'margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-size: 14px; '><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>Claude Code for reasoning and brainstorming.<\/span><span class=\"_richTextNode\"> Claude drew on its knowledge and reasoning to propose a non-obvious stopping rule. I probably wouldn't have thought to explore hazard-rate processes as I was not particularly familiar with them before embarking on this project.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>MATLAB via MCP for instant feedback.<\/span><span class=\"_richTextNode\"> Every idea was tested in seconds, so bad ideas could be discarded quickly, and good ones refined and verified. Claude could write a simulation, run it, look at the plot, and decide whether to continue autonomously.<\/span><\/li><li  style = 'margin-left: 56px; line-height: 21px; min-height: 0px; text-align: left; white-space: pre-wrap; '><span class=\"_richTextNode\" style=' font-weight: bold;'>The Symbolic Math Toolbox for proof.<\/span><span class=\"_richTextNode\"> Moving from simulation to proof without switching tools turned the empirical hunch into a theorem. The <\/span><span class=\"_richTextNode\" style=' font-style: italic;'>symsum<\/span><span class=\"_richTextNode\"> one-liner that returned <\/span><span texencoding=\"\\pi\/4\" style=\"vertical-align:-5px\">$ \\pi\/4 $<\/span><span class=\"_richTextNode\"> was more convincing than any Monte Carlo run.<\/span><\/li><\/ul><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">The project went from my question \"could we find a classroom-friendly random process that estimates <\/span><span style=\"font-family: STIXGeneral-webfont, serif; font-style: italic; font-weight: 400; color: rgb(33, 33, 33);\">\u03c0<\/span><span class=\"_richTextNode\">?\" to a working marble-bag game with simulations, figures, and a closed-form proof in a single afternoon. And I think that's pretty cool!<\/span><\/div><div  style = 'margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre; color: rgb(33, 33, 33); font-family: Helvetica, Arial, sans-serif, Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left; '><span class=\"_richTextNode\">If you want to try this kind of AI-assisted research yourself, you'll need a coding agent and the MATLAB MCP server. You can find the companion MATLAB scripts for this post <\/span><a href = \"https:\/\/github.com\/mathworks\/Research-Office-Projects\/tree\/main\/Examples\/finding-pi-with-marbles\"><span class=\"_richTextNode\">here<\/span><\/a><span class=\"_richTextNode\">.<\/span><\/div>\r\n<\/div><script type=\"text\/javascript\">var css = '._richTextNode {white-space: break-spaces;}'; var head = document.head || document.getElementsByTagName('head')[0], style = document.createElement('style'); head.appendChild(style); style.type = 'text\/css'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); }<\/script>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/matlab\/files\/2026\/07\/marblePi_1.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Bio: Today's guest blogger is Charlie Saunders. Charlie is a Senior Research Scientist in the MathWorks Advanced Research and Technology Office.Recently, I came across a cool result about calculating... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/matlab\/2026\/07\/28\/using-claude-code-and-matlab-to-find-a-new-way-to-estimate-pi\/\">read more >><\/a><\/p>","protected":false},"author":176,"featured_media":4345,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[144,63,30,135,145],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/4349"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/users\/176"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/comments?post=4349"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/4349\/revisions"}],"predecessor-version":[{"id":4350,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/posts\/4349\/revisions\/4350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/media\/4345"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/media?parent=4349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/categories?post=4349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/matlab\/wp-json\/wp\/v2\/tags?post=4349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}