How to define and plot a function with input range?

조회 수: 70 (최근 30일)
Karl Bridggie
Karl Bridggie 2021년 10월 24일
댓글: Karl Bridggie 2021년 10월 24일
Hi,
I have a function:
I need to plot it: y(t) = 2f(t-1) + 4f(t-2) + 3(ft-3) + f(t-4)
I could imagine what y(t) looks like but couldn't make the MATLAB code working after a lot of tries. I have:
pt = @(t) (1 ./ (1 + t .^ 2));
But I don't know how to specify the range of t (0.5 >= t >= -0.5).
I also tried piecewise():
pt = @(t) piecewise(0.5 >= t >= -0.5, (1 + t .^ 2) .^ (-1), t > 0.5, 0, t < 0.5, 0);
x = 0:0.01:10;
plot(x, pt(x));
xlim([-1 10]);
But it reported error: Undefined function 'piecewise' for input arguments of type 'double'.
Could you please help me with this? Thanks a lot!

채택된 답변

Alan Stevens
Alan Stevens 2021년 10월 24일
Like so:
pt = @(t) (1 ./ (1 + t .^ 2)).*(t>=-0.5).*(t<=0.5);
x = 0:0.01:10;
y = 2*pt(x-1) + 4*pt(x-2) + 3*pt(x-3) + pt(x-4);
plot(x, y);
xlim([-1 10]);
  댓글 수: 1
Karl Bridggie
Karl Bridggie 2021년 10월 24일
pt = @(t) (1 ./ (1 + t .^ 2)).*(t>=-0.5).*(t<=0.5); does the trick!
Thanks very much!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by