How to create this fucntion below
조회 수: 1 (최근 30일)
이전 댓글 표시
I`m very new to matlab and don`t know how to create below function
first, I have made function
fx.m
function result = fx(t)
if -1<=t & t
result = 1;
elseif t>=0 & t
result = -1;
else
result = 0;
end
in console space
t=linspace(0,1);
x=fx(t)
and the compiler said x=0 and that`s it
how to deal with this?
댓글 수: 1
Walter Roberson
2020년 3월 27일
That diagram requires that at t = 0, f(t) is all of the values between -2 and +2 simultaneously. Even if you restrict yourself to numbers that are representable in IEEE 754 double precision, I figure that is 9223372036854775804 different numbers that would have to be returned at f(0) . This is not in any way practical.
채택된 답변
Birdman
2020년 3월 27일
You can try the Symbolic approach:
syms y(t)
y(t)=piecewise(t<-1,0,t>=-1 & t<0,2,t>=0 & t<=1,-2,t>1,0);
%plotting
t=-5:0.001:5;
plot(t,y(t))
댓글 수: 0
추가 답변 (1개)
Tommy
2020년 3월 27일
If you want fx to return an array the same size as t:
function result = fx(t)
result = zeros(size(t)); % f(t) is mostly 0
result(-1<=t & t<0) = 2; % except when t is between -1 and 0, in which case it's 2
result(t>=0 & t<1) = -2; % and when t is between 0 and 1, in which case it's -2
end
Then,
t = linspace(-1.5, 1.5);
x = fx(t);
plot(t, x)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!