Output multiple titles and lines of data with one function onto a single figure.

1 次查看(过去 30 天)
Hey everyone! I am trying to create a function that is able to load two arrays, (x,y). These arrays are single line vectors for x and y data on a graph. After it loads the arrays I want the function to deturmine if the slope between two points is positive, if so, add that point to a plot, then keep integrating until the data's slope turns negative. For example x =[ 1 2 3 4 5 6 7 8] y =[8 7 6 5 5 9 10]. I would want the values of of x<4 integrated then plotted on the figure, the displayed, then the data x>5 integrated and displayed on the same figure but in a different format. Here is what i have so far, but my issue is that It prints the right figure as long as all my data is either positive or negative. If the data has one slope change, the function prints the negative slope graph only. My end goal is one figure with 3 rows of titles at the top providing a value for average time charging, steady, and discharging with the three sloped regions displayed in different colors.
function AverageVoltage=integrate_experimental(x,y)
AverageVoltage=trapz(x,y);
if ((y(2)-y(1))/(x(2)-x(1))) > 0 %deturmines if the point is in the charging, constant, or discharging phase
AverageVoltage=trapz(x,y);%takes the integral of the charging values
plot(x,y,'g-','LineWidth',2);%plots the chraging values
xlabel('time (Seconds)');ylabel('Voltage (Volts)');%formats the plot
title(['The Average Voltage During Charge = ',num2str(AverageVoltage)]);%formats the plot
return
elseif ((y(2)-y(1))/(x(2)-x(1))) == 0 %deturmines if the point is in the charging, constant, or discharging phase
plot(x,y,'k-','LineWidth',2);%plots the chraging values
xlabel('time (Seconds)');ylabel('Voltage (Volts)');%formats the plot
title(['Constant Voltage Region',num2str(AverageVoltage)]);%formats the plot
return
elseif ((y(2)-y(1))/(x(2)-x(1))) < 0 %deturmines if the point is in the charging, constant, or discharging phase
AverageVoltage=trapz(x,y);%takes the integral of the charging values
plot(x,y,'r-','LineWidth',2);%plots the dischraging values
xlabel('time (Seconds)');ylabel('Voltage (Volts)');%formats the plot
title(['The Average Voltage During Discharge = ',num2str(AverageVoltage)]);%formats the plot
return
else
return;
end
end

回答(1 个)

Poorna
Poorna 2023-9-22
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:
% Determine the region based on the slope
if slope > 0
charging_x = [charging_x, x(i), x(i+1)];
charging_y = [charging_y, y(i), y(i+1)];
elseif slope == 0
steady_x = [steady_x, x(i), x(i+1)];
steady_y = [steady_y, y(i), y(i+1)];
else
discharging_x = [discharging_x, x(i), x(i+1)];
discharging_y = [discharging_y, y(i), y(i+1)];
end
  • Finally, you can plot all the arrays and calculate the averages
plot(charging_x, charging_y, 'g-', 'LineWidth', 2);
hold on;
plot(steady_x, steady_y, 'k-', 'LineWidth', 2);
plot(discharging_x, discharging_y, 'r-', 'LineWidth', 2);
% Calculate the average voltages
charging_average = trapz(charging_x, charging_y);
steady_average = trapz(steady_x, steady_y);
discharging_average = trapz(discharging_x, discharging_y);
Hope this Helps!

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by