What's a proportional controller?
Even though system control was main subject for me for a while in college, i did not grasp on that subject too well. The main outline is clear to me. We got closed loop and in place of the controller it's possible to put all kinds of controllers.

For example P, or PI, or PID. The goal for today is understanding P (proportional) controller a little bit more. To do that i figured its easiest trough code.
Why code? Because there is no calculus to be deciphered. It's just a for loop that sums up everything every time it loops trough time. The next code is written in GNU Octave, it's a free and open source software similar to Matlab, and it's especially made for calculating almost anything.
P controller code:
t = 0:0.01:10; % Time vector
setpoint = 1.0; % Desired value
Kp = 2.0; % Proportional gain
dt = 0.01;
y = zeros(size(t)); % Output
for i = 2:length(t)
e = setpoint - y(i-1);
u = Kp * e;
dy = (-y(i-1) + u) * dt;
y(i) = y(i-1) + dy;
end
plot(t, y, 'b', t, ones(size(t))*setpoint, 'r--');
xlabel('Time'); ylabel('Output');
legend('Output y(t)', 'Setpoint');
title(sprintf('P Regulator (Kp = %.1f)', Kp));
grid on;
-First there is t=0:0.01:10; And that's making an array from 0 to 10 with increments of 0.01.
-"setpoint" is desired value the system has to follow, for example this can be a temperature on a thermostat.
-"Kp" is literally a gain by which is system defined, in other words, how strong does it react to changes.
-"dt" is an increment of time between steps, it's also how much is increment in the first variable called "t".
For loop in octave goes something like this:
for i = 1:5 (This means every loop i will be incremented by 1 until it gets to 5.
for i = 1:5
disp(i)
end
1
2
3
4
5
So in this case the for loop serves as a substitute for integral. It's less accurate, but accurate enough if the processor that runs the code is fast enough for my use case.
for i = 2:length(t)
e = setpoint - y(i-1);
u = Kp * e;
dy = (-y(i-1) + u) * dt;
y(i) = y(i-1) + dy;
end
If i try to go through one loop i get:
e = 1 - 0,
u = 2 * 1,
dy = (0 + 2) * 0.01,
y(1) = 0 + 0.02,
And then code does that for as many times as there is increments and plots the whole array y against time.
Output:

P controller code with disturbance at 5 seconds:
t = 0:0.01:10;
setpoint = 1.0;
Kp = 2.0;
dt = 0.01;
% Disturbance: step at t=5
d = 0.5 * (t >= 5);
y = zeros(size(t));
for i = 2:length(t)
e = setpoint - y(i-1);
u = Kp * e;
dy = (-y(i-1) + u + d(i)) * dt; % <-- disturbance added here
y(i) = y(i-1) + dy;
end
plot(t, y, 'b', t, ones(size(t))*setpoint, 'r--', t, d, 'g:');
xlabel('Time'); ylabel('Output');
legend('Output y(t)', 'Setpoint', 'Disturbance');
title(sprintf('P Regulator with Step Disturbance (Kp = %.1f)', Kp));
grid on;

Right here disturbance is added at the fifth second.
d = 0.5 * (t>=5), is a step function that switches only after t is bigger than 5.
So in: dy = (-y(i-1) + u + d(i)) * dt; Until t is 5, the equation is the same as without disturbance, but after t is 5, then dy is bigger by 5.
That's it, I'm sure i have complicated it more than needed. Thanks for reading.