# ispravljac, primer, Vout + R

from pylab import *

deg = linspace(0, 360, 360 * 2 + 1)
wt = radians(deg)

minp = sin(wt)
amin = abs(minp)
smin = sign(minp)

Mout = linspace(0, 1, 201)
n = len(Mout)
Jout = empty(n)

close('all')
rc('text', usetex = True)
rc('font', family = 'serif')
rc('font', size = 12)
rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}']

figure(0, figsize = (4, 8))
degtick = (0, 60, 120, 180, 240, 300, 360)

for i in range(n):

    mout = Mout[i]
    moutline = mout * ones(len(deg))
    mx = amax(array([amin, moutline]), axis = 0)
    mr = mx - mout
    jout = mr
    Jout[i] = mean(jout)
    jin = jout * smin
    
    clf()

    subplot(4, 1, 1)
    plot(deg, minp)
    ylabel(r'$m_{IN}$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.2, 1.2)

    subplot(4, 1, 2)
    plot(deg, amin, 'b')
    plot(deg, moutline, 'c')
    plot(deg, mx, 'r')
    ylabel(r'$|m_{IN}| \, [\text{b}], \; M_{OUT} \, [\text{c}], \; m_{X} \, [\text{r}]$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.2, 1.2)

    subplot(4, 1, 3)
    plot(deg, jout, 'b')
    ylabel(r'$j_{OUT}$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.2, 1.2)
    
    subplot(4, 1, 4)
    plot(deg, jin)
    xlabel(r'$\omega t [^\circ]$')
    ylabel(r'$j_{IN}$')
    xlim(0, 360)
    xticks(degtick)
    ylim(- 1.2, 1.2)
    
    savefig('ispravljac-primer{:d}.png'.format(i), bbox_inches = 'tight')
   
figure(1)
plot(Mout, Jout)
xlabel(r'$M_{OUT}$')
ylabel(r'$J_{OUT}$')
savefig('Jout.png', bbox_inches = 'tight')

figure(2)
plot(Jout, Mout)
xlabel(r'$J_{OUT}$')
ylabel(r'$M_{OUT}$')
title('kriva regulacije')
savefig('Mout.png', bbox_inches = 'tight')

