Plotting multiple piecewise functions onto the same graph

Hi!
I'm new to the program and i've been trying to plot two piecewise functions,
y(x) = piecewise(0<x<1.85, sin(523.26*pi*x), 1.85<x<3.7, 0, 3.7<x<5.55, sin(523.26*pi*x), 5.55<x<7.4, 0, 7.4<x<9.25, sin(523.26*pi*x));
and
y2(x) = piecewise(0<x<1.63, sin(463.08*pi*x), 1.63<x<3.26, 0, 3.26<x<4.89, sin(463.08*pi*x), 4.89<x<6.52, 0, 6.52<x<8.15, sin(463.08*pi*x));
onto the same graph, but i can't get the coding right. Would anyone be able to help?
Thank you so much in advance

2 件のコメント

darova
darova 2019 年 8 月 21 日
Maybe you have some coding so we can correct it?
Margherita Carlon
Margherita Carlon 2019 年 8 月 21 日
Oh absolutely,
I couldn't find any support for this specific case but what I tried was this (which is probably way off:
syms y1(x)
y1(x) = piecewise(0<x<1.85, sin(523.26*pi*x), 1.85<x<3.7, 0, 3.7<x<5.55, sin(523.26*pi*x), 5.55<x<7.4, 0, 7.4<x<9.25, sin(523.26*pi*x));
fplot(y1)
hold on
syms y2(x)
y2(x) = piecewise(0<x<1.63, sin(463.08*pi*x), 1.63<x<3.26, 0, 3.26<x<4.89, sin(463.08*pi*x), 4.89<x<6.52, 0, 6.52<x<8.15, sin(463.08*pi*x));
fplot(y2)

サインインしてコメントする。

 採用された回答

Star Strider
Star Strider 2019 年 8 月 21 日

0 投票

I coded and ran ‘y1’ as:
y1 = @(x) ((0<x) & (x<1.85)).*sin(523.26*pi*x) + ((1.85<x) & (x<3.7)).*0 + ((3.7<x) & (x<5.55)).*sin(523.26*pi*x) + ((5.55<x) & (x<7.4)).*0 + ((7.4<x) & (x<9.25)).*sin(523.26*pi*x);
t = linspace(-5, 20, 500);
figure
plot(t, y1(t))
grid
Do the same for ‘y2’. Choose as many points as you want for ‘t’.

4 件のコメント

Margherita Carlon
Margherita Carlon 2019 年 8 月 21 日
Unfortunately this wouldn't solve my problem of having both functions on the same graph. Thanks though! :)
Star Strider
Star Strider 2019 年 8 月 21 日
Yes, it would!
Code ‘y2’ using the same approach I used when I coded ‘y1’, then:
figure
plot(t, y1(t), t, y2(t))
grid
or use the hold function with two plot calls.
Margherita Carlon
Margherita Carlon 2019 年 8 月 22 日
Oh I see!! Now it works perfectly :)
do you happen to have any tips on how to change the number of points for t? Given it's really high-frequency, the mapped function dosen't look too sinusoidal anymore...
Star Strider
Star Strider 2019 年 8 月 22 日
Change the third argument in the linspace function to change the number of points it creates. (The default number is 100.) See the documentation section on Vector with Specified Number of Values.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by