Hello!
Could you, please, suggest an answer:
How to define function f (x), which for x from -1 to 1, f (x) = 1; and outside these limits f (x) = 0.
Thank in advance!

3 comentarios

Rik
Rik el 11 de Ag. de 2020
This is your homework. What have you tried so far? And do you want a normal (or anonymous) function, or a symbolic function?
Viktoriia Buliuk
Viktoriia Buliuk el 11 de Ag. de 2020
I try to define Window function. I have tried with if.
KSSV
KSSV el 11 de Ag. de 2020
Show us your code.

Iniciar sesión para comentar.

 Respuesta aceptada

hosein Javan
hosein Javan el 11 de Ag. de 2020

0 votos

function y = f(x)
y = zeros(size(x));
y(abs(x) < 1) = 1;
end

5 comentarios

hosein Javan
hosein Javan el 11 de Ag. de 2020
the advantage of such definition is that you can pass inputs to the function as array rather than scalars. it should really help in dealing with fourier transform which requires element-wise product.
hosein Javan
hosein Javan el 11 de Ag. de 2020
however this method could lead to unnecessary computer calculations becasue when you multiply the window function to your signal, the elements that are multiplied eaither by 1 or 0 which is unnecessary. multiplication is for a non-rectangular window function like gaussian,etc,.. . but in the case of rectangular. you only have to select the values of your function that meet the condition by array indexing. if you are planning to do heavy simulation with rectangular window, indexing is more efficient but if it's just a simple calculation function multiplication should be fine.
Rik
Rik el 11 de Ag. de 2020
Editada: Rik el 11 de Ag. de 2020
In this case you can simply convert the logical to double:
f=@(x) double(abs(x)<1);
In most other cases you will need multiplication:
%g={x^2 for -1<x<1
% {8 for x>2
% {0 otherwise
g=@(x) ...
x.^2.*(-1<x & x<1)+...
8.*(x>2)+...
0;
hosein Javan
hosein Javan el 11 de Ag. de 2020
ofcourse unless you want to use function file, you need this multiplication only to define your function in anonymous form. correct.
Viktoriia Buliuk
Viktoriia Buliuk el 4 de Sept. de 2020
Thank you very much!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 11 de Ag. de 2020

Comentada:

el 4 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by