필터 지우기
필터 지우기

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 (최근 30일)

채택된 답변

David Hill
David Hill 2021년 12월 30일
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
Colty Day
Colty Day 2021년 12월 30일
uhm...sorry is there other way on doing it... what if i use if elseif
Image Analyst
Image Analyst 2021년 12월 31일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2021년 12월 30일
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
Colty Day
Colty Day 2021년 12월 31일
편집: Walter Roberson 2021년 12월 31일
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 2021년 12월 31일
@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;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by