From: nolispe on
Hello I am trying to plot a function defined in 2 parts.
Here is my code :

function y=H(f)

if abs(f)<=4000
y=1/2*(1+cos(pi*f*1/4000));
else
y=0;
end
end

But when I try to plot it with f=0:6000; (for instance) it gives me a 0 line... I don't understand.
The only way to get the cosine part is to take f=0:a with a<4000.
I'm sure it's a very stupid error but I can't find it.
Thank you in advance
From: dpb on
nolispe wrote:
> Hello I am trying to plot a function defined in 2 parts.
> Here is my code :
>
> function y=H(f)
>
> if abs(f)<=4000
> y=1/2*(1+cos(pi*f*1/4000));
> else
> y=0;
> end
> end
>
> But when I try to plot it with f=0:6000; (for instance) it gives me a
> 0 line... I don't understand.
> The only way to get the cosine part is to take f=0:a with a<4000.
> I'm sure it's a very stupid error but I can't find it.

Ayup...that's what would happen given the two inputs...

Hint: the problem is the plotting it's in the definition of the
function. Specifically, look at

doc if

to see the root of the problem. From there the fix ought to be pretty
simple... :)

--
From: nolispe on
I'm sorry, I looked on the internet and I thought about what you told me but I really don't see the problem... Is it a syntax problem (did I forget brackets or semicolons?...) And I really don't know what "doc if" means
From: Steven Lord on

"nolispe" <nolispe280(a)hotmail.com> wrote in message
news:1059424393.21020.1277393445133.JavaMail.root(a)gallium.mathforum.org...
> I'm sorry, I looked on the internet and I thought about what you told me
> but I really don't see the problem... Is it a syntax problem (did I forget
> brackets or semicolons?...) And I really don't know what "doc if" means

"doc if" means to look at the documentation for the IF function by typing
the following at the MATLAB prompt:

doc if

You can also look at the reference page online:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html

Note the section of the Remarks that discussses nonscalar expressions. If
you do:

if (1:10 > 5)
disp('true')
else
disp('false')
end

then since (1:10 > 5) does not have ALL true entries, the IF condition is
NOT satisfied and so you should see the ELSE block execute.

For piecewise functions I recommend using logical indexing. Steve Eddins
has a brief introduction to this type of indexing on his blog:

http://blogs.mathworks.com/steve/2008/01/28/logical-indexing/

and it's described in the documentation here:

http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html#bq7egb6-1

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com