#!/usr/bin/perl
# -*- perl -*-

$vin = 5; # volts
$vout = 3.3; # volts
$iout = 0.25; # amps
$vsat = 0; # volts across switch
$vf = 0.8; # volts across diode
$vripple = 0.01; # volts at load

$freq = 150_000; # Hz

# Math starts here

$duty = ($vout + $vf) / ($vin - $vsat - $vout);

$tcycle = 1.0 / $freq;
$toff = $tcycle / ($duty + 1);
$ton = $tcycle - $toff;
printf("Duty Cycle %f\n", $ton / $tcycle);

$vpulse_p = 0.2 * ($ton/$tcycle);
$vpulse_n = -0.2 * ($toff/$tcycle);
printf("Vpulse = %.2f %.2f 0 %.2fu %.2fu 1f %.2fu\n",
       $vpulse_n, $vpulse_p,
       $tcycle/2.01*1_000_000, $tcycle/2.01*1_000_000,
       $tcycle*1_000_000);

$ipk = 2 * $iout;

$l = ($vin - $vsat - $vout) / $ipk * $ton;

$c = ($ipk * $tcycle) / (8 * $vripple);

printf("Inductor = %.3f uH\n", $l * 1_000_000);
printf("Capacitor = %.3f uF\n", $c * 1_000_000);

printf("R2 = %.2f\n", $vout / $iout);
