Add 2 trigonometric functions, with conditions

조회 수: 2 (최근 30일)
Panagiotis Arvanitis
Panagiotis Arvanitis 2022년 9월 22일
답변: Panagiotis Arvanitis 2022년 9월 26일
Greeting to all,
I am trying to add graphically 2 trigonometric functions:
  1. f(x)=a*sin(k*x), graphicaly displayed only for positive value (≥0)
  2. g(x)=b*cos(m*x+d), graphicaly displayed only for negative value (≤0)
Thanks

채택된 답변

Sam Chak
Sam Chak 2022년 9월 22일
편집: Sam Chak 2022년 9월 26일
Are you trying to plot the addition of two sinusoidal functions, even when there are discontinuities?
x = linspace(-1, 1, 2001);
a = 1;
b = 1;
k = pi;
m = pi;
d = 0;
f = sin(k*x);
g = cos(m*x + d);
fp = f.*(f > 0);
gn = g.*(g < 0);
% Plot
subplot(221)
plot(x, f), grid on, ylim([-1.5 1.5]), title({'$f(x)$'}, 'interpreter', 'latex')
subplot(223)
plot(x, g), grid on, ylim([-1.5 1.5]), title({'$g(x)$'}, 'interpreter', 'latex')
xlabel({'$x$'}, 'interpreter', 'latex')
subplot(222)
plot(x, fp, x, gn), grid on, ylim([-1.5 1.5]), title({'$f^{+}(x)$ and $g^{-}(x)$'}, 'interpreter', 'latex')
legend({'$f^{+}(x)$', '$g^{-}(x)$'}, 'interpreter', 'latex', 'location', 'northwest')
subplot(224)
plot(x, fp + gn), grid on, ylim([-1.5 1.5]), title({'$f^{+}(x) + g^{-}(x)$'}, 'interpreter', 'latex')
xlabel({'$x$'}, 'interpreter', 'latex')

추가 답변 (2개)

Panagiotis Arvanitis
Panagiotis Arvanitis 2022년 9월 26일
Thank you Sam Chak,
Yes, I am trying to add two functions with discontinuities.
I didn't get the same output graph, both positive and negative values appeared.
x = linspace(-0.5, 4, 2001);
a = 1;
b = 1;
k = pi;
m = pi;
d = 0.25;
f = sin(k*x);
g = cos(m*x + d);
% Plot
plot(x, f, x, g, x, f+g), grid on, ylim([-2 2])
xlabel({'$x$'}, 'Interpreter', 'LaTeX'),
ylabel({'$y$'}, 'Interpreter', 'LaTeX'),
legend({'$x$'}, 'Interpreter', 'LaTeX', 'FontSize', 14, 'Location', 'Northwest')
  댓글 수: 1
Sam Chak
Sam Chak 2022년 9월 26일
I have fixed the code in my Answer.
Please check if that that is the signal desired.

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


Panagiotis Arvanitis
Panagiotis Arvanitis 2022년 9월 26일
I appriciate your hepl!!. It works
Thank you

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by