Filter löschen
Filter löschen

Plotting piecewise function over two periods

1 Ansicht (letzte 30 Tage)
Yamana Uno
Yamana Uno am 20 Okt. 2023
Bearbeitet: Voss am 20 Okt. 2023
I am trying to plot a piecewise function over two periods. I am able to plot it over two periods but it is not very efficient.
Below is the code I am using for now.
How do I make this more efficient/simpler? I am only using MATLAB, no additional softwares or extensions such as Symbolic Math Toolbox.
t0 = 0:0.001:(pi/10);
ih_t0 = 500*sin((10*t0)).^2;
subplot(2,1,1)
plot(t0, ih_t0,'b')
hold on
t1 = (pi/10):0.001:0.8;
ih_t1 = 0;
plot(t1,ih_t1)
hold on
t2 = 0.8:0.001:(0.8+(pi/10));
t3 = (0.8+(pi/10)):0.001:1.6;
plot(t2,ih_t0)
hold on
plot(t3,ih_t1)
hold off

Akzeptierte Antwort

Voss
Voss am 20 Okt. 2023
t = 0:0.001:0.8;
ih = zeros(size(t));
idx = t < pi/10;
ih(idx) = 500*sin(10*t(idx)).^2;
plot([t t+0.8],[ih ih])

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by