summation of 2 Sine waves

조회 수: 80 (최근 30일)
Jay Perks
Jay Perks 2020년 5월 23일
댓글: Star Strider 2020년 5월 23일
Hi all,
I was hoping someone can help me wrap my head around why matlab does not add sine waves together.
Im trying to plot 2 added sine waves that are 180 degrees out of phase with the same amplitude. The added plot should show a stright line at 0 but im getting a strange array of signals.
If I plot the sine waves and sum wave on the some plot they seem to work which is confusing me even more.
here is my code.
A = 1 % Amplitude is 1 V
w = 2*pi*2; % w = 2Hz (frequency)
b = 2*pi/.5 % calculating wave length gives 0.5m
x = 0 :.005:1; % x axis from 0 to 1 with sampling every .005
L = 1; % length of transmission line
t = 0.0; % time = 0 (starting time)
%--------------------------------------------------------------------------
% signal generation
y = A*sin(b*x + (w*t)); % Wave equation for a transmission line
subplot(3,1,1);
plot (x, y);
axis ([0 1 -1.25 1.25]); % graph limits
grid on;
grid minor;
title ('Waveform Along A Transmission Line')
xlabel ('Metres');
ylabel ('Amplitude (V)');
set(gca, 'fontsize', 12); % change font size
%--------------------------------------------------------------------------
% Reflected wave
r = A*sin(b*(L-x)+ (w*t)); % reflected waveform, 180 out of phase
subplot(3,1,2);
plot (x, r);
axis ([0 1 -1.25 1.25]); % graph limits
grid on;
grid minor;
title ('Reflected Waveform Along A Transmission Line');
xlabel ('Metres');
ylabel ('Amplitude (V)');
set(gca, 'fontsize', 12); % change font size
%-------------------------------------------------------------------------
% Waveform sum
t = r + y;
subplot(3,1,3);
%plot (x,y);
%hold on;
%plot (x,r);
%hold on;
plot(x,t);
hold on;
I hope someone can clear up my mistake because it's driving me mad haha.
Jay

채택된 답변

Star Strider
Star Strider 2020년 5월 23일
It does show a straight line at zero. Look at the magnitude of the y-axis (±2E-15), and you will see that it is essentially plotting floating-point approximation error.
Add:
ylim([-1 1])
to subplot(3,1,3) (so it’s the same as the others) and you get the result you expect.
The full code for it now being:
t = r + y;
subplot(3,1,3);
%plot (x,y);
%hold on;
%plot (x,r);
%hold on;
plot(x,t);
hold on;
ylim([-1 1])
.
  댓글 수: 2
Jay Perks
Jay Perks 2020년 5월 23일
You my friend, have no idea how much time I have spent on this :P
Thankyou so much for your answer!
Star Strider
Star Strider 2020년 5월 23일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by