Thread Subject: how to plot a rect function

Subject: how to plot a rect function

From: BC Tan

Date: 10 Oct, 2009 14:22:01

Message: 1 of 4

Hi All,

Can anyone advise me on how to plot a rectangular function for an ideal filter? tks

Subject: how to plot a rect function

From: Mehmet OZTURK

Date: 10 Oct, 2009 14:59:02

Message: 2 of 4

"BC Tan" <rainzz@live.com> wrote in message <haq5a9$r11$1@fred.mathworks.com>...
> Hi All,
>
> Can anyone advise me on how to plot a rectangular function for an ideal filter? tks

Hi, firstlu u can define u(t) "unit step function" such that

function u=ut(t)
u=t>=0;

and with this function you can plot any form of rectangles

>> t=0:.1:10;
>> plot(t, ut(t-1) - ut(t-2))

Subject: how to plot a rect function

From: BC Tan

Date: 12 Oct, 2009 23:45:06

Message: 3 of 4

Hi, the code provided by you as show below

function u=ut(t)
t=0:.1:10;
u=t>=0;
plot(t, ut(t-1) - ut(t-2))

results in the following error.

??? Undefined function or method 'ut' for input arguments of type 'double'.

Error in ==> rect at 4
plot(t, ut(t-1) - ut(t-2))

Pls assist. Tks

Subject: how to plot a rect function

From: Steven Lord

Date: 13 Oct, 2009 14:00:14

Message: 4 of 4


"BC Tan" <rainzz@live.com> wrote in message
news:hb0f22$fu$1@fred.mathworks.com...
> Hi, the code provided by you as show below
>
> function u=ut(t)
> t=0:.1:10;

Why allow ut to accept an input argument when you're just going to overwrite
it on the next line?

> u=t>=0;
> plot(t, ut(t-1) - ut(t-2))

Is this an attempt to recursively call ut or an attempt to plot u*t versus
t?

> results in the following error.
>
> ??? Undefined function or method 'ut' for input arguments of type
> 'double'.
>
> Error in ==> rect at 4
> plot(t, ut(t-1) - ut(t-2))

So the root cause for this specific error is that you've written a function
ut as the main (first) function inside the file rect.m -- when the names of
the file and the main function in that file differ, MATLAB will refer to the
function by the FILE name. So if you wanted this to be a recursive call,
you would need:

plot(t, rect(t-1) - rect(t-2))

but if you do that with the rest of the code written above, you will receive
a Recursion Limit error (as your code has no way to break out of the
recursion.)

Go back to the textbook where you learned the definition of a rect function.
Open an M-file and write the steps you need to follow to evaluate the rect
function for a given input as comments -- DO NOT write down any MATLAB code
at this stage. Once you have the steps written down, break any that you
don't know how to implement directly into substeps. Again, don't write any
code yet.

Once each comment corresponds to an instruction that you know how to
implement (and not before), implement them. Doing this, not only will you
get code that's more likely to work the first time, but you'll also
understand it better.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com