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
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
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))

更多回答(1 个)

Tommy
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)

类别

Help CenterFile Exchange 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by