Hi Jack,
I understand that you are looking to plot the charging, discharging, and steady phases based on the slope of adjacent measurements using the given x and y values.
The code you provided calculates the slope for the first two observations and plots the graph based on it. However, each set of observations could have multiple phases. To plot all these phases, you need to iterate through each adjacent pair of observations, calculate their slopes, and plot accordingly.
To achieve this, you can follow these steps:
- Maintain three sets of variables: “charging_x”, “charging_y”, “discharging_x”, “discharging_y”, and “steady_x”, “steady_y”.
- Run a for loop from index 1 to “length(x)-1”.
- Inside the loop, calculate the slope between the ith coordinate and (i+1)th coordinate as below:
slope = (y(i+1) - y(i)) / (x(i+1) - x(i));
- Based on the sign of the slope, append the ith and (i+1)th observations to one of the three variable sets:
charging_x = [charging_x, x(i), x(i+1)];
charging_y = [charging_y, y(i), y(i+1)];
steady_x = [steady_x, x(i), x(i+1)];
steady_y = [steady_y, y(i), y(i+1)];
discharging_x = [discharging_x, x(i), x(i+1)];
discharging_y = [discharging_y, y(i), y(i+1)];
- Finally, you can plot all the arrays and calculate the averages
plot(charging_x, charging_y, 'g-', 'LineWidth', 2);
plot(steady_x, steady_y, 'k-', 'LineWidth', 2);
plot(discharging_x, discharging_y, 'r-', 'LineWidth', 2);
charging_average = trapz(charging_x, charging_y);
steady_average = trapz(steady_x, steady_y);
discharging_average = trapz(discharging_x, discharging_y);
Hope this Helps!