So, I am trying to solve the ODE that describes the response and stability of a power system. The precise equation and theory does not really matter. As the title suggests, I want the ode solver to change some parameter according to an IF condition. Here is the code:
clear;
E = 10;
X = 10;
R = 0;
T = 5; % Time Constant
tmax = 100;
G0 = 0; % Initial Value
Psc = E^2/X;
P0 = 1.25*Psc; % Power Losses
G = 0:0.001:2;
a = tand (0);
V = E ./ sqrt( (1+R*G+a*X*G).^2 + (X*G-a*R*G).^2 );
P = E^2*G ./ ( (1+R*G+a*X*G).^2 + (X*G-a*R*G).^2 );
% ODE Numerical Solution
[t23, Gsol23s] = ode23s(@(t, G) P0/T - G* (E ./ sqrt( (1+R*G+a*X*G).^2 ...
+ (X*G-a*R*G)).^2 )^2, [0 tmax], G0);
Vsol23s = E ./ sqrt( (1+R*Gsol23s+a*X*Gsol23s).^2 ...
+ (X*Gsol23s-a*R*Gsol23s).^2 );
Psol23s = E^2*Gsol23s ./ ( (1+R*Gsol23s+a*X*Gsol23s).^2 ...
+ (X*Gsol23s-a*R*Gsol23s).^2 );
figure;
plot(P/Psc, V/E, 'linewidth', 3);
hold on;
grid on;
plot(Psol23s/Psc, Vsol23s/E, 'linewidth', 2);
xlabel('P/Psc'); ylabel('V/E')
figure;
plot(t23, Vsol23s/E, t23, Psol23s/Psc, t23, Gsol23s*X)
legend('V/E', 'P/Psc', 'G*B');
I want, every time that, V is less than 0.95*E, to decrease a by 5. So something like:
if Vsol23s << 0.95*E
a = a - 5;
end
I know that right now, ode first solves for Gsol23s and then calculates the Vsol23s. Is there any way around?
Any ideas are much appreciated.
EDIT 1: A solution that gives one ode step at a time (and not the whole matrix for tspan), could do the trick, but I don't know if this is possible.