You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
add text to the axis
12 views (last 30 days)
Show older comments
i want to add 'noon, sunrise and sunset' at these positons respectivly 13,6,18 below the x-axis with rotation
Accepted Answer
Star Strider
on 16 Jul 2023
Perhaps this —
t = 0:24;
y = rand(3,25);
figure
plot(t, y)
grid
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-30)
.
19 Comments
Star Strider
on 16 Jul 2023
As always, my pleasure!
I saw your earlier post as well with respect to the different areas, however I could not understand which areas you wanted to calculate. It might be easier to use an image editor to colour the different areas to make that more obvious.
The easiest solution to that would be to define all the curves over the full 24-hour span (defining some as zero where appropriate), then use linspace to create an independent variable (time) vector with an appropriate number of elements (perhaps 1000 or more) so that all the points were accurately accounted for, then use interp1 on each of them to interpolate to the common independent variable values. (Those results would then be vectors of the same lengths, defined at the same independent variable locations, making the calculations using them possible.) After that, use trapz (or more appropriately cumtrapz) to do the integrations, then subtract those results to get the desired areas. Again, I am not certain how how you would define the areas, so this would likely require subtracting the areas over different regions to get the desired result.
.
Tasneem Abed
on 16 Jul 2023
the region with the yellow
i want to make sure this code calculate the area within it.
% Finding the area under the intersection
intersection = min(P(1:L), pmax(1:L)); % Select the minimum values between P and pmax
area_under_intersection = trapz(t(1:L), intersection); % Calculate the area using the trapz function
disp(['Area under intersection: ', num2str(area_under_intersection)]);
Star Strider
on 16 Jul 2023
That diagram helps.
I need the code that produces that plot, including any required data files (although I do not remember that any wer required).
Tasneem Abed
on 16 Jul 2023
pmax =[0 21.428*42 57.1428*42 109*42 116*42 123*42 127*42 129*42 129.5*42 130*42 130*42 130*42 129*42 123*42 116*42 109*42 57.1428*42 21.428*42 0 ];
t =[5 6 7 7.30 8 9 9.30 10 11 12 13 14 15 15.30 16 16.30 17 18 19 ];
hold on
plot(t,pmax,'r')
hold off
data=[0 6 900;
6 7 2400;
7 15 3000;
15 18 1800;
18 22 3000;
22 24 1800];
power=data(:, 3)
Dt=data(:,2) - data(:,1)
Power_Generated=power.*Dt
Total_power1=sum(Power_Generated)
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2) ;
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
hold on
plot(t,P,'b')
hold off
xlim([1 24])
xlabel(['Timer, Hr'])
ylabel(['Power, W'])
title('Combined curves, W versus time, hour')
grid on
grid minor
% Finding the area under the intersection
intersection = min(P(1:L), pmax(1:L)); % Select the minimum values between P and pmax
area_under_intersection = trapz(t(1:L), intersection); % Calculate the area using the trapz function
disp(['Area under intersection: ', num2str(area_under_intersection)]);
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-90)
Tasneem Abed
on 16 Jul 2023
i want to seperate each region by 'sections with red color in one plot ','region with yellow color in another plot' and 'region with blue color as well in a different plot'and calculating the area of each region
Star Strider
on 16 Jul 2023
My results are somewhat different from your results, since I used cumtrapz to do the integrations, and then summed those results as necessary to get the total results for the various regions. I began by creating the ‘tv’ vector and then interpolating all the curves with respect to it. I then integrated those results using cumtrapz, and subtracted them as necesary to get the various areas, then added those vectors to get the area totals. This required some additional interpolation to get the regions where the ‘pmaxi’ and ‘Pi’ curves intersected, to make the results as accurate as I could. The results are integrated with respect to time, so essentially:
Make appropriate changes to get the result you want —
pmax =[0 21.428*42 57.1428*42 109*42 116*42 123*42 127*42 129*42 129.5*42 130*42 130*42 130*42 129*42 123*42 116*42 109*42 57.1428*42 21.428*42 0];
t =[5 6 7 7.30 8 9 9.30 10 11 12 13 14 15 15.30 16 16.30 17 18 19];
pmaxq = [0 pmax 0];
tq = [0 t 24];
tv = linspace(0, 24, 1E+3); % Interpolation Time Vector
pmaxi = interp1(tq, pmaxq, tv); % Interpolated 'pmax' Vector
figure
hold on
plot(t,pmax,'r')
hold off
data=[0 6 900;
6 7 2400;
7 15 3000;
15 18 1800;
18 22 3000;
22 24 1800];
power=data(:, 3);
Dt=data(:,2) - data(:,1);
Power_Generated=power.*Dt;
Total_power1=sum(Power_Generated)
Total_power1 = 52800
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2);
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
hold on
plot(t,P,'b')
hold off
xlim([1 24])
xlabel(['Timer, Hr'])
ylabel(['Power, W'])
title('Combined curves, W versus time, hour')
grid on
grid minor
% Finding the area under the intersection
intersection = min(P(1:L), pmax(1:L)); % Select the minimum values between P and pmax
area_under_intersection = trapz(t(1:L), intersection); % Calculate the area using the trapz function
disp(['Area under intersection: ', num2str(area_under_intersection)]);
Area under intersection: 29099.9268
uv = cumsum(ones(size(t)))*1E-12; % Offset Vector
Pi = interp1(t+uv, P+uv, tv); % Interpolated 'P' Vector
Pi = fillmissing(Pi,'nearest');
Ipmaxi = cumtrapz(tv, pmaxi); % Integrated Interpolated 'pmax'
IPi = cumtrapz(tv, Pi); % Integrated Interpolated 'P'
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-90)
isx1(1) = find(pmaxi>0,1);
isx1(2) = find(pmaxi>0,1,'last'); % Start & End Indices For 'pmaxi'
blu = Ipmaxi(isx1(1):isx1(2)) - IPi(isx1(1):isx1(2));
blu = sum(blu(blu>0)); % Area 'blue'
tv07 = tv <= 7; % Index Range For 'tv' <= 7
red(1) = sum(IPi(tv07) - Ipmaxi(tv07)); % First 'red' Area
tq = (tv>=15 & tv <= 20);
isx2(1) = find(tv>=7,1); % Index For 'tv' >= 7
isx2(2) = find(diff(sign(pmaxi(tq)-Pi(tq)))) + find(tv>=15,1); % Index Range For Second Intersection Of 'pmaxi' & 'Pi'
yel = sum(Ipmaxi(isx2(1):isx2(2)) - IPi(isx2(1):isx2(2))); % Area 'yellow'
red(2) = sum(Ipmaxi(isx2(2):end) - IPi(isx2(2):end)); % Second 'red' Area
redt = red(1)+red(2); % Combined 'red' Area
figure
plot(tv,pmaxi, 'r', 'DisplayName','pmax_i')
hold on
plot(tv,Pi, 'b', 'DisplayName','P_i')
grid on
grid minor
legend('Location','best')
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-90)
text(5, 700,sprintf('RED_1 = %.1f',red(1)), 'Rotation',75)
text(18, 1100,sprintf('RED_2 = %.1f',red(2)))
text(8,1200,sprintf('BLUE = %.1f',blu))
text(8,4000,sprintf('YELLOW = %.1f',yel))
text(3,5800,sprintf('RED = %.1f',redt))
% figure
% plot(tv,Ipmaxi, 'DisplayName','pmax')
% hold on
% plot(tv,IPi, 'DisplayName','P')
% hold off
% grid
% legend('Location','best')
.
Tasneem Abed
on 17 Jul 2023
the area under the whole blue curve which is the Total_power1 = 52800
from what you calculated its much more than that about 7609295.2
why the difference??
Star Strider
on 17 Jul 2023
The difference is that I used the cumtrapz function to integrate it with respect to time (‘tv’), because I believe that is the correct approach. I am not certain what the correct procedure is.
If you want to use a simple sum instead, just use the sum function, with the same arguments for ‘pmaxi’ and ‘Pi’ rather than integrating them with respect to ‘tv’, to calculate ‘red’, ‘yel’, and ‘blu’.
Tasneem Abed
on 17 Jul 2023
its not working
what i am sure about is the area under the whole blue curve =52800
i can't differentiate what is right and what is not.
it has been a whole week trying to figure out how to do it
Star Strider
on 17 Jul 2023
I have no idea how they are supposed to be calculated.
Using a ‘corrected’ cumsum result instead of using cumtrapz —
pmax =[0 21.428*42 57.1428*42 109*42 116*42 123*42 127*42 129*42 129.5*42 130*42 130*42 130*42 129*42 123*42 116*42 109*42 57.1428*42 21.428*42 0];
t =[5 6 7 7.30 8 9 9.30 10 11 12 13 14 15 15.30 16 16.30 17 18 19];
pmaxq = [0 pmax 0];
tq = [0 t 24];
tv = linspace(0, 24, 1E+3); % Interpolation Time Vector
pmaxi = interp1(tq, pmaxq, tv); % Interpolated 'pmax' Vector
figure
hold on
plot(t,pmax,'r')
hold off
data=[0 6 900;
6 7 2400;
7 15 3000;
15 18 1800;
18 22 3000;
22 24 1800];
power=data(:, 3);
Dt=data(:,2) - data(:,1);
Power_Generated=power.*Dt;
Total_power1=sum(Power_Generated)
Total_power1 = 52800
Total_power2=power.*Dt;
DATA=[data(:,1) data(:,2) power];
Average_load=Total_power2/sum(Dt);
peak_load=max(power);
Daily_LF=Average_load/peak_load*100;
Results=[Average_load peak_load*ones(size(Daily_LF)) Daily_LF];
L=length(data);
timeinterval=data(:,1:2);
t = sort(reshape(timeinterval,1,2*L));
for n = 1:L
P(2*n-1) = power(n);
P(2*n) = power(n);
end
hold on
plot(t,P,'b')
hold off
xlim([1 24])
xlabel(['Timer, Hr'])
ylabel(['Power, W'])
title('Combined curves, W versus time, hour')
grid on
grid minor
% Finding the area under the intersection
intersection = min(P(1:L), pmax(1:L)); % Select the minimum values between P and pmax
area_under_intersection = trapz(t(1:L), intersection); % Calculate the area using the trapz function
disp(['Area under intersection: ', num2str(area_under_intersection)]);
Area under intersection: 29099.9268
uv = cumsum(ones(size(t)))*1E-12; % Offset Vector
Pi = interp1(t+uv, P+uv, tv); % Interpolated 'P' Vector
Pi = fillmissing(Pi,'nearest');
% Ipmaxi = cumtrapz(tv, pmaxi); % Integrated Interpolated 'pmax'
% IPi = cumtrapz(tv, Pi); % Integrated Interpolated 'P'
Ipmaxi = cumsum(pmaxi)/numel(pmaxi); % Summed Interpolated 'pmax'
IPi = cumsum(Pi)/numel(Pi); % Summed Interpolated 'P'
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-90)
isx1(1) = find(pmaxi>0,1);
isx1(2) = find(pmaxi>0,1,'last'); % Start & End Indices For 'pmaxi'
blu = Ipmaxi(isx1(1):isx1(2)) - IPi(isx1(1):isx1(2));
blu = sum(blu(blu>0)); % Area 'blue'
tv07 = tv <= 7; % Index Range For 'tv' <= 7
red(1) = sum(IPi(tv07) - Ipmaxi(tv07)); % First 'red' Area
tq = (tv>=15 & tv <= 20);
isx2(1) = find(tv>=7,1); % Index For 'tv' >= 7
isx2(2) = find(diff(sign(pmaxi(tq)-Pi(tq)))) + find(tv>=15,1); % Index Range For Second Intersection Of 'pmaxi' & 'Pi'
yel = sum(Ipmaxi(isx2(1):isx2(2)) - IPi(isx2(1):isx2(2))); % Area 'yellow'
red(2) = sum(Ipmaxi(isx2(2):end) - IPi(isx2(2):end)); % Second 'red' Area
redt = red(1)+red(2); % Combined 'red' Area
figure
plot(tv,pmaxi, 'r', 'DisplayName','pmax_i')
hold on
plot(tv,Pi, 'b', 'DisplayName','P_i')
grid on
grid minor
legend('Location','best')
text([13 6 18], zeros(1,3), {'noon','sunrise','sunset'}, 'Horiz','left', 'Vert','top', 'Rotation',-90)
text(5, 700,sprintf('RED_1 = %.1f',red(1)), 'Rotation',75)
text(18, 1100,sprintf('RED_2 = %.1f',red(2)))
text(8,1200,sprintf('BLUE = %.1f',blu))
text(8,4000,sprintf('YELLOW = %.1f',yel))
text(3,5800,sprintf('RED = %.1f',redt))
% figure
% plot(tv,Ipmaxi, 'DisplayName','pmax')
% hold on
% plot(tv,IPi, 'DisplayName','P')
% hold off
% grid
% legend('Location','best')
Interpolating them is necesary because of the irregular nature of the regions being defined. That may lead to some inconsistencies.
.
Tasneem Abed
on 17 Jul 2023
in the code when u used 'cumtrapz 'there was an area with negative result..
why??
Star Strider
on 17 Jul 2023
I did not get the same result. All the areas I calculated were positive.
Tasneem Abed
on 17 Jul 2023
'Ipmaxi'
the 'I' here what is it for??
Star Strider
on 17 Jul 2023
That stands for: Integrated pmax interpolated, and pmaxi is simply pmax interpolated.
Also ‘IPi’ is: Integrated P interpolated, Pi is P interpolated.
I named them differently to avoid over-writing the orignal vectors. (I did my best to comment-documnet my code.) The ‘Pi’ and ‘IPi’ vectors were not straightforward because of the way ‘P’ was originally created. Both ‘pmaxi’ and ‘Pi’ required some creativity because of that.
Tasneem Abed
on 17 Jul 2023
if u deleted i from IPiand Ipmax u will get these results
Star Strider
on 17 Jul 2023
I had to interpolate the vectors in order to segment the areas as requested. They do not originally share the same independent variable as originally described, and interpolating them to that is necessary to do the segmentations and calculations.
It might be possible to use polyshape (or alphaShape) to define the ouitlines of the regions and then do the calculations using polyarea, however I chose the approach I posted because I have more experience with it, and describing the polygons using the two different variables (pmax and P) was not obvious to me since they are described differently. It would also be necessary to define the intersections between them, and that would require interpolation.
This is not a straightforward problem regardless of the approach used.
Star Strider
on 19 Jul 2023
As always, my pleasure!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)