How to create this fucntion below

1 visualización (últimos 30 días)
NoYeah
NoYeah el 27 de Mzo. de 2020
Comentada: Walter Roberson el 27 de Mzo. de 2020
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 comentario
Walter Roberson
Walter Roberson el 27 de Mzo. de 2020
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.

Iniciar sesión para comentar.

Respuesta aceptada

Birdman
Birdman el 27 de Mzo. de 2020
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))

Más respuestas (1)

Tommy
Tommy el 27 de Mzo. de 2020
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)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by