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

1 view (last 30 days)

Accepted Answer

David Hill
David Hill on 30 Dec 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 Comments
Image Analyst
Image Analyst on 31 Dec 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).

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Dec 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 Comments
Colty Day
Colty Day on 31 Dec 2021
Edited: Walter Roberson on 31 Dec 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 on 31 Dec 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;

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!