Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Re: Ploting a simple graph

$
0
0
On 7/17/2013 3:36 AM, Tiger wrote:
> On 2013-07-16 16:05:13 +0000, Nasser M. Abbasi said:
> On 7/16/2013 10:39 AM, Tiger wrote:
...
> I would like to draw a y=x graph where y=0 if x<0.5. So the graph
> should be flat until x is greater than 0.5 and then a 45 degree line
> onwards. I used the code below but I cannot seem to achieve what I want
> to achieve. Where would be my mistake?
>
> x=0.1:0.01:1;
> if (x<0.5)
> y=0;
> else
> y=x;
> end
> figure(1), plot(x,y);
...

>
> f = @(x) x.*(x>=0.5);
> x = 0.1:0.01:1;
> plot(x,f(x))
...

> Thank you. I will study the code, in particular the " = @(x) x.*(x>=0.5)"
> part. But why my approach is not healthy? What is the logical mistake I
> am making? Could you please briefly comment?

The " = @(x) x.*(x>=0.5)" is an anonymous function. Look up them in the
doc search for details.

The logic flaw in your approach is that in Matlab, the IF condition is T
_iff_ all elements in the condition vector/array are T -- in this case
that is not so. So, the ELSE clause is all that's evaluated.

doc IF for details there...

One alternate way to write you condition would be

y=x; y(x<0.5)=0;

In the above, the logical array from the test is used an a logical
addressing expression (look that up, too) and then the locations that
are T by position are assigned the RHS.

I suggest going to the doc an reading the "Getting Started" section
linearly to begin to get a feel for how to use Matlab and the basic
ideas of how it's implemented.

--

Viewing all articles
Browse latest Browse all 19628

Trending Articles