# Tolerance and Monte Carlo: which part limits the flyback

Every part on a board differs from its nominal value. Here you take the flyback whose output is regulated by a TL431 and an optocoupler, let its divider resistors, its inductance and its output capacitor vary within their tolerances in a Monte Carlo analysis, read the output-voltage and peak-current distributions, and rank the parts by how much of the spread each one causes. Then you tighten the part that limits the output and check the yield again. About 20 minutes.

Pictures: a numbered red box is what you click or read, `[Run]` is a button, `Analysis › Monte Carlo` a menu path.

Every number on this page comes from a run of these steps, and a test runs them again and fails when a number here differs from the run (`crates/engine/tests/tutorial_flyback_tolerance.rs`).

## 1. Open the flyback with the TL431 loop

Choose `File › Open Example › Isolated › Flyback with TL431 and optocoupler`.

![The File menu open at Open Example, Isolated, Flyback with TL431 and optocoupler, marked 1 to 4](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/01-open-example.png)

1. `File`.
2. `Open Example`.
3. `Isolated`.
4. `Flyback with TL431 and optocoupler`.

The circuit runs from 12 V to 12 V at 6 W. The TL431 compares the output, divided by `Rup` and `Rlow`, with its reference, and the optocoupler carries the error to the primary-side PWM.

## 2. Prepare the parts that vary

Make three changes before the analysis:

- **Keep the full load.** The example halves its load at 4 ms. Select the block `load_on` and set its `after` to `1` in the Properties panel, so the load stays and the whole run is one operating point.
- **One inductance for both windings.** Choose `Circuit › Variables`, add `Lm` = `20u`, then select `T1` and set both `l1` and `l2` to `Lm`. A core's inductance tolerance moves both windings together, and the analysis can now vary them as one value.
- **Measure after the loop has settled.** Every measurement below reads 5 ms to 6 ms of the 6 ms run. With `C1` at the top of its tolerance the output is still recovering at 3 ms; by 5 ms it has settled.

![The flyback schematic with Rup, Rlow, T1 and C1, marked 1 to 4](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/02-parts.png)

1. `Rup` (9.53 kΩ): the upper divider resistor, ±1 %.
2. `Rlow` (2.49 kΩ): the lower divider resistor, ±1 %.
3. `T1`: the coupled inductor, both windings `Lm` = 20 µH, ±10 %.
4. `C1`: the 100 µF output capacitor, ±20 %.

The tolerances are the ones an engineer picks for this exercise: the grades IEC 60062 codes F (±1 %), K (±10 %) and M (±20 %). Each part is drawn uniformly within its tolerance.

## 3. Monte Carlo of the output voltage

Choose `Analysis › Monte Carlo` and fill in the form.

![The Monte Carlo form with the variables, runs, seed, measurement and lower limit, marked 1 to 5](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/03-form.png)

1. `Variables`: `[{"name":"Rup.r","tol":0.01},{"name":"Rlow.r","tol":0.01},{"name":"Lm","tol":0.1},{"name":"C1.c","tol":0.2}]`.
2. `Number of runs`: `50`.
3. `Seed`: `1`. The same seed draws the same 50 sets of parts every time, so the next steps vary exactly the same circuits.
4. `Measure`: signal `v(out)`, statistic `mean`, window start `5m`, window end `6m`.
5. Under `More options`, `Lower limit` `11.88` and `Upper limit` `12.12`: the output must stay within 12 V ± 1 %.

Press `[Run]`. The 50 simulations take a few seconds.

## 4. Read the histogram

![The Monte Carlo result: statistics, yield and the histogram of v(out) between the two limits, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/04-histogram.png)

1. The result: the statistics, the yield against the limits and the histogram, with the limits as red lines.

The mean output is <!-- run:loose.vout.mean -->12.065 V<!-- /run --> with a standard deviation of <!-- run:loose.vout.sd -->86.2 mV<!-- /run -->; the 50 runs lie between <!-- run:loose.vout.min -->11.916 V<!-- /run --> and <!-- run:loose.vout.max -->12.234 V<!-- /run -->. Only <!-- run:loose.passed -->39<!-- /run --> of the 50 runs stay within 12 V ± 1 %: a yield of <!-- run:loose.yield -->78 %<!-- /run -->. The window also gives the 95 % confidence interval of the yield, since 50 runs only estimate it.

## 5. The peak current

Open `Analysis › Monte Carlo` again. Keep the variables, runs and seed, set `Measure` to signal `i(S1)` with statistic `max` (same window), clear both limits and press `[Run]`.

![The Monte Carlo result for the peak switch current i(S1), marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/05-peak-current.png)

1. The peak current of the switch over the same 50 circuits.

The peak current averages <!-- run:loose.ipk.mean -->2.51 A<!-- /run --> with a standard deviation of <!-- run:loose.ipk.sd -->71 mA<!-- /run -->, from <!-- run:loose.ipk.min -->2.36 A<!-- /run --> to <!-- run:loose.ipk.max -->2.64 A<!-- /run -->. Size the switch, the current sense and the core for the top of that range, not for the mean.

## 6. Which part drives the spread

The histogram says how wide the spread is, not where it comes from. Press `⌘K` to open the command palette, type `sensitivity` and choose `Sensitivity`. Fill in `Variables` `Rup.r, Rlow.r, Lm, C1.c`, and `Metrics` `{"vout":{"signal":"v(out)","stat":"mean","t0":"5m","t1":"6m"},"ipk":{"signal":"i(S1)","stat":"max","t0":"5m","t1":"6m"}}`. Press `[Run]`.

![The Sensitivity result: each part's normalized and absolute sensitivity for vout and ipk, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/06-sensitivity.png)

1. The table, largest first. `Normalized` is the relative change of the metric per relative change of the part: 0.8 means a 1 % larger part moves the metric by 0.8 %.

At the nominal parts the output is <!-- run:nominal.vout -->12.067 V<!-- /run --> and the peak current <!-- run:nominal.ipk -->2.50 A<!-- /run -->. A sensitivity alone does not rank the parts: a sensitive part with a tight tolerance can matter less than a dull part with a loose one. Multiply each sensitivity by the part's own spread (a part drawn uniformly within ±tol has a standard deviation of tol/√3) and you get the spread it adds to the metric. The last column checks the ranking against the Monte Carlo itself: the correlation between each part's value and the metric over the 50 runs of steps 4 and 5 (`[Copy CSV]` in their windows gives the samples).

The output voltage:
<!-- run:table.vout -->

| Part | Tolerance | Sensitivity | Spread it adds (1σ) | Correlation in the 50 runs |
|---|---|---|---|---|
| `Rup` | ±1 % | +0.79 | 0.46 % | +0.70 |
| `Rlow` | ±1 % | −0.79 | 0.46 % | −0.74 |
| `C1` | ±20 % | 0.00 | 0.00 % | +0.09 |
| `Lm` | ±10 % | 0.00 | 0.00 % | −0.02 |

<!-- /run -->

The peak current:
<!-- run:table.ipk -->

| Part | Tolerance | Sensitivity | Spread it adds (1σ) | Correlation in the 50 runs |
|---|---|---|---|---|
| `Lm` | ±10 % | −0.50 | 2.89 % | −0.97 |
| `Rup` | ±1 % | +0.79 | 0.46 % | +0.27 |
| `Rlow` | ±1 % | −0.79 | 0.45 % | −0.11 |
| `C1` | ±20 % | 0.00 | 0.00 % | +0.34 |

<!-- /run -->

Both methods agree. The two divider resistors set the output's spread; the inductance and the output capacitor add nothing to it, because the loop corrects whatever they change. The peak current follows the inductance: in discontinuous conduction the core stores the same energy each period, so a smaller `Lm` needs a higher current (`Ipk = √(2P/(Lm·f))`, a sensitivity of −0.5). A correlation of a few tenths for a part whose sensitivity is zero is chance: 50 runs cannot tell it from nothing.

## 7. Tighten the part that limits the output

The divider limits the output, so buy better resistors. Open `Analysis › Monte Carlo` once more, set the measurement back to `v(out)` `mean` with the limits `11.88` and `12.12`, change the tolerance of `Rup.r` and `Rlow.r` to `0.001` (±0.1 %) and press `[Run]`.

![The Monte Carlo result of v(out) with 0.1 % divider resistors, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/flyback-tolerance/07-tighter.png)

1. The narrower histogram, well inside the limits.

The standard deviation falls from <!-- run:loose.vout.sd -->86.2 mV<!-- /run --> to <!-- run:tight.vout.sd -->8.6 mV<!-- /run -->; the runs lie between <!-- run:tight.vout.min -->12.051 V<!-- /run --> and <!-- run:tight.vout.max -->12.083 V<!-- /run -->, and <!-- run:tight.passed -->50<!-- /run --> of 50 pass: a yield of <!-- run:tight.yield -->100 %<!-- /run -->. What is left is the offset of the mean, <!-- run:tight.vout.mean -->12.066 V<!-- /run -->: the divider was chosen for 12.06 V, including the TL431's REF current through `Rup`. From now on the nominal values, not the tolerances, are the largest error of the output.

## What you learned

- A Monte Carlo analysis varies every part at once within its tolerance and gives the distribution, the yield against limits and the worst run.
- The same seed draws the same parts, so several measurements can be read from the same set of circuits.
- To find the part that limits a specification, weigh each part's sensitivity by its own tolerance, and check the ranking against the correlation in the Monte Carlo samples.
- In a regulated converter the feedback divider sets the output's accuracy; the power parts set the stresses, such as the peak current.

Next: [Python scripting](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/13-python/) runs the same analyses from a script, or go back to the [tutorial list](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/).
