Filter löschen
Filter löschen

ive tried doing it but i dont know why the plot is not right t=-2:0.1:2; if t<=-1 y=-5.*t-5; elseif t<0 y=t.^2+1; elseif t<1 y=pi.^t; elseif t>=1 y=pi+sin(pi

2 Ansichten (letzte 30 Tage)

Akzeptierte Antwort

David Hill
David Hill am 30 Dez. 2021
t=-2:.1:2;
f=zeros(size(t));
f(t<=-1)=-5*(t(t<=-1))-5;
f(t>-1&t<0)=t(t>-1&t<0).^2+1;
f(t>=0&t<1)=pi.^(t(t>=0&t<1));
f(t>=1)=pi+sin(pi*t(t>=1));
plot(t,f);
  2 Kommentare
Colty Day
Colty Day am 30 Dez. 2021
uhm...sorry is there other way on doing it... what if i use if elseif
Image Analyst
Image Analyst am 31 Dez. 2021
David we normally don't give full solutions to what is obviously his homework. Colty, I know you accepted this but beware of turning it in. I know for a fact that some professors use "plagiarism detectors" and you don't want to get caught turning in someone else's solution as your own.
It looks like you actually wanted a version using if/else and I gave you hints for that and you almost got it except that t on the right hand side needed to be t(k).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 30 Dez. 2021
Hint to get you started
function f(t)
ft = zeros(1, length(t));
for k = 1 : length(t)
if t(k) <= -1
ft(k) =
elseif
end
end
plot(t, ft, 'b-')
  5 Kommentare
Colty Day
Colty Day am 31 Dez. 2021
Bearbeitet: Walter Roberson am 31 Dez. 2021
t=-2:0.1:2;
yt = zeros(1, length(t));
for k =1: length(t)
if t(k)<=-1
yt(k)=-5.*t-5;
elseif t(k)<0
yt(k)=t.^2+1;
elseif t(k)<1
yt(k)=pi.^t;
elseif t(k)>=1
yt(k)=pi+sin(pi.*t);
end
end
plot(t,yt,'r-');
Image Analyst
Image Analyst am 31 Dez. 2021
@Colty Day, you almost got it, but you need to use t(k), not the entire t vector, on the right hand side of the equation. If it works for you can you at least "Vote" for this answer. 🙂
t=-2:0.1:2;
yt = zeros(1, length(t));
for k = 1: length(t)
if t(k) <= -1
yt(k) = -5.*t(k)-5;
elseif t(k) < 0
yt(k) = t(k).^2+1;
elseif t(k) < 1
yt(k) = pi.^t(k);
elseif t(k) >= 1
yt(k) = pi + sin(pi.*t(k));
end
end
plot(t,yt,'r-');
grid on;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots 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