Hi Sahrish,
For achieving the described behavior, the approach needs to be adjusted. The way in which if statements and the "ode45" function are used seems to be conceptually incorrect. The "ode45" function is used for solving ordinary differential equations and it is not clear how you have modelled the injected current or its effect on the system from the provided code snippet.
Instead of trying to manipulate the "x" and "y" directly, we can simulate the entire system first and then adjust the output for plotting based on the condition of the injected current.
Since the specifics of how the injected current is modeled or its relation to the action potential initiation are mpy provided, here is a conceptual code snippet regarding how to adjust the plotting based on a condition:
% Simulate the system (you will use ode45 or another method)
sysPars.simTime = 500;
x = linspace(0, sysPars.simTime, 1000); % Time vector
y = % Placeholder for the simulated voltage (replace with your actual simulation results)
% Define start and end of the action potential phase based on current injection
% Assuming the action potential starts at 100 ms and ends by 400 ms for illustration
apStartIndex = find(x >= 100, 1, 'first');
apEndIndex = find(x <= 400, 1, 'last');
% Simulate the action potential
for i = apStartIndex:apEndIndex
if x(i) < (x(apStartIndex) + x(apEndIndex)) / 2
% Rising phase
y(i) = y(i) - (x(i) - x(apStartIndex)); % Decrease to simulate the rise
else
% Falling phase
y(i) = y(i) - (x(apEndIndex) - x(i)); % Increase back to simulate the fall
end
end
% Ensure action potential is within -69 to 40 mV
y(apStartIndex:apEndIndex) = -69 + (40 - (-69)) * (y(apStartIndex:apEndIndex) - min(y(apStartIndex:apEndIndex))) / (max(y(apStartIndex:apEndIndex)) - min(y(apStartIndex:apEndIndex)));
% Plot results
figure;
plot(x, y, 'LineWidth', 1);
xlabel('Time (ms)', 'FontSize', 15);
ylabel('Voltage (mV)', 'FontSize', 15);
Hope this helps!