From: Steven Lord on

"Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message
news:hohvqm$j26$1(a)fred.mathworks.com...
> "Wayne King" <wmkingty(a)gmail.com> wrote in message
> <hohsr2$su9$1(a)fred.mathworks.com>...
>> "Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message
>> <hohnhf$a4s$1(a)fred.mathworks.com>...
>> > I am having great difficulty with trying to plot a piecewise function.
>> > I am not allowed to use piecewise_eval etc but rather with a control
>> > loop etc. I have already spend one week playing around with for loop,
>> > while loop, if elseif etc but I am still getting error.Can someone
>> > please help me? The problem is this. Plot x(t) = t-1 for -1=<t<=1, x(t)
>> > =0 otherwise. Plot for -2=<t<=2. Use a resolution of 0.1 time units. I
>> > am completely lost and can't work out the right way to approach the
>> > problem. Thanks.
>>
>> Hi Mario, Are you reading the MATLAB documentation? If you are spending a
>> week on this type of problem, then it seems like you would benefit
>> greatly from reading the documentation. It shows you how to do these
>> types of things with examples. Also, the problem you had with leaving off
>> semicolons is clearly explained in the documentation. I think your
>> homework assignments will go much better if you work through the Getting
>> Started Guide. I'm not going to give you the answer to your homework
>> outright, but there are a number of ways to do this:
>>
>> 1.) Create a time vector from [-2,2] in increments of 0.1. You will find
>> out how to do this easily by reading the documentation.
>> 2.) Create an x vector of zeros equal in size to your time vector.
>> 3.) Identify those elements of the time vector that correspond to [-1,1].
>> 4.) Use the indices you obtained in step 3 and fill those elements of the
>> x vector with x(t)=t-1
>
>
> I have done this
> t=-2:0.1:2
> x=zeros(1,41)

Rather than hard-coding in the size of x, which would require you to change
your code if you changed the value of t, I recommend you use an expression
here to create x to be an appropriate size based on the size of t.

x = zeros(size(t));

Now whatever size t your code is given, x will be an array of all zeros that
is the same size.

> t1=t.*(t<=1)
> t2=t.*(-1<=t)
> Can you tell me how to implement step 3 and 4?

Read through this blog posting from Loren's blog, particularly the sections
where she works with logical arrays:

http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/

and this one from Steve E's blog which serves as more of a tutorial on
logical indexing:

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

Using logical indexing, you can implement steps 3 and 4 with 2 lines (or 1
line if you compute something twice rather than computing it once and
reusing it.)

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


From: Mario Fatafehi on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hoifnj$8gb$1(a)fred.mathworks.com>...
>
> "Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message
> news:hohvqm$j26$1(a)fred.mathworks.com...
> > "Wayne King" <wmkingty(a)gmail.com> wrote in message
> > <hohsr2$su9$1(a)fred.mathworks.com>...
> >> "Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message
> >> <hohnhf$a4s$1(a)fred.mathworks.com>...
> >> > I am having great difficulty with trying to plot a piecewise function.
> >> > I am not allowed to use piecewise_eval etc but rather with a control
> >> > loop etc. I have already spend one week playing around with for loop,
> >> > while loop, if elseif etc but I am still getting error.Can someone
> >> > please help me? The problem is this. Plot x(t) = t-1 for -1=<t<=1, x(t)
> >> > =0 otherwise. Plot for -2=<t<=2. Use a resolution of 0.1 time units. I
> >> > am completely lost and can't work out the right way to approach the
> >> > problem. Thanks.
> >>
> >> Hi Mario, Are you reading the MATLAB documentation? If you are spending a
> >> week on this type of problem, then it seems like you would benefit
> >> greatly from reading the documentation. It shows you how to do these
> >> types of things with examples. Also, the problem you had with leaving off
> >> semicolons is clearly explained in the documentation. I think your
> >> homework assignments will go much better if you work through the Getting
> >> Started Guide. I'm not going to give you the answer to your homework
> >> outright, but there are a number of ways to do this:
> >>
> >> 1.) Create a time vector from [-2,2] in increments of 0.1. You will find
> >> out how to do this easily by reading the documentation.
> >> 2.) Create an x vector of zeros equal in size to your time vector.
> >> 3.) Identify those elements of the time vector that correspond to [-1,1].
> >> 4.) Use the indices you obtained in step 3 and fill those elements of the
> >> x vector with x(t)=t-1
> >
> >
> > I have done this
> > t=-2:0.1:2
> > x=zeros(1,41)
>
> Rather than hard-coding in the size of x, which would require you to change
> your code if you changed the value of t, I recommend you use an expression
> here to create x to be an appropriate size based on the size of t.
>
> x = zeros(size(t));
>
> Now whatever size t your code is given, x will be an array of all zeros that
> is the same size.
>
> > t1=t.*(t<=1)
> > t2=t.*(-1<=t)
> > Can you tell me how to implement step 3 and 4?
>
> Read through this blog posting from Loren's blog, particularly the sections
> where she works with logical arrays:
>
> http://blogs.mathworks.com/loren/2008/05/14/acting-on-specific-elements/
>
> and this one from Steve E's blog which serves as more of a tutorial on
> logical indexing:
>
> http://blogs.mathworks.com/steve/2008/01/28/logical-indexing/
>
> Using logical indexing, you can implement steps 3 and 4 with 2 lines (or 1
> line if you compute something twice rather than computing it once and
> reusing it.)
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

I actually try logical index but when I plot it there were errors because of different vector size. Finally I do this but I am not sure whether it is wrong or not. t=-2:0.1:2; y=t-1; x=zeros(size(t)); plot(x,y). But I am only suppose to plot from -1<=t <=1 for y=t-1 and y=0 for other t values?
From: Steven Lord on

"Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message
news:hokjal$as4$1(a)fred.mathworks.com...

*snip*

> I actually try logical index but when I plot it there were errors because
> of different vector size.

Can you post what you've tried that threw an error when you ran it? Perhaps
we can suggest where the problem is.

> Finally I do this but I am not sure whether it is wrong or not.
> t=-2:0.1:2; y=t-1; x=zeros(size(t)); plot(x,y). But I am only suppose to
> plot from -1<=t <=1 for y=t-1 and y=0 for other t values?

That should have given you a line from x = 0, y = -3 to x = 0, y = 1 --
which is indeed not what you wanted according to your problem statement.

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