How do I fill colour on both sides of the sin wave in this graph?
Show older comments
Please help me modify this code so that the area under the sin wave is red and the area above the wave is blue. Half of the area of the graph should be blue and half red. There is a rough image of what im looking for the colour to be filled like. (obviously this isnt an exact sin wave lol). I dont know how i might do this so your help is much appreciated.

% Define the time axis (x-axis)
t = linspace(0, 12, 1000);
% Define the parameters of the sinusoidal wave
midpoint = 7.5;
amplitude = 7.5;
period = 3;
% Create the sinusoidal wave
y = midpoint + amplitude*sin(2*pi*t/period);
% Set the y-axis limits to 0-15
ylim([0, 15]);
% Plot the wave
plot(t, y);
xlabel('Time');
ylabel('Amplitude');
title('Sinusoidal Wave');
Accepted Answer
More Answers (1)
% Define the time axis (x-axis)
t = linspace(0, 12, 1000);
% Define the parameters of the sinusoidal wave
midpoint = 7.5;
amplitude = 7.5;
period = 3;
% Create the sinusoidal wave
y = midpoint + amplitude*sin(2*pi*t/period);
% Plot the wave
figure
plot(t, y);
hold on
patch([t flip(t)], [ones(size(y))*min(y) flip(y)], 'r') % Fill Below Durve
patch([t flip(t)], [ones(size(y))*max(y) flip(y)], 'b') % Fill Above Curve
hold off
xlabel('Time');
ylabel('Amplitude');
title('Sinusoidal Wave');
% Set the y-axis limits to 0-15
ylim([0, 15]);
.
Categories
Find more on Graphics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

