# How to make a piecewise function without using syms?

57 Ansichten (letzte 30 Tage)
Brianna Miranda am 15 Dez. 2021
Kommentiert: Mathieu NOE am 6 Mär. 2024
I am trying to plot the following piecewise function without using syms but it plots as a horizontal line. This is the code I'm using. The issue isn't the <=, I have tried several different combinations of them (0<=n && n<=4, 0<=n & n<=4, 0<=n<=4) and the code still does not work.
dt = .1;
n = -31:dt:31;
for ii = 1:length(n)
if 0<=n<=4
x(ii) = n(ii)./4;
elseif 4<n<8
x(ii) = 2-(n(ii)./4);
else
x(ii) = 0;
end
end
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Mathieu NOE am 15 Dez. 2021
hello Brianna
try this (using logical arrays)
n = 0:20;
x = zeros(size(n)); % cond 3 by default
% cond 1
ind1 = (0<=n & n<=4);
x(ind1) = n(ind1)/4;
% cond 2
ind2 = (4<n & n<8);
x(ind2) = 2-n(ind2)/4;
plot(n,x);
##### 3 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Ekin am 6 Mär. 2024
it's been like 3 years since your comment apparently but i wanted to thank you this code helped me a lot :)
Mathieu NOE am 6 Mär. 2024

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Applications finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by