How to create this fucntion below

3 vues (au cours des 30 derniers jours)
NoYeah
NoYeah le 27 Mar 2020
Commenté : Walter Roberson le 27 Mar 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 commentaire
Walter Roberson
Walter Roberson le 27 Mar 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.

Connectez-vous pour commenter.

Réponse acceptée

Birdman
Birdman le 27 Mar 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))

Plus de réponses (1)

Tommy
Tommy le 27 Mar 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)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by