Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: if statements
Date: Fri, 10 Oct 2008 09:45:54 -0400
Organization: The MathWorks, Inc.
Lines: 61
Message-ID: <gcnmai$4d$1@fred.mathworks.com>
References: <gcmi9m$lbj$1@fred.mathworks.com> <gcn6f7$p95$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1223646354 141 144.212.105.187 (10 Oct 2008 13:45:54 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 10 Oct 2008 13:45:54 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:494559



"Jos " <DELjos@jasenDEL.nl> wrote in message 
news:gcn6f7$p95$1@fred.mathworks.com...
> "Ongun Palaoglu" <ongun@mac.com> wrote in message 
> <gcmi9m$lbj$1@fred.mathworks.com>...
>> hello guys i am trying to use the if command,
>>  my code:
>>
>> %setting parameter
>> vtn = 0.75;
>> vds = [0.2 2.5 0];
>> vgs = [2 2 0];
>> knp=[200*10^-6 300*10^-6];
>> kp=knp*10;
>> i = 1; len = length (vgs);
>
> Since this loop executes a fixed number of times, you should change it 
> into a for-loop
> for i=1:len,
>   x(i) =
> end
>
> which you can vectorize using the dot notation (.*)
> x = vds .* (vgs - vtn - vds/2);
>
>> while i <= len
>>     x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);
>>     i = i + 1;
>> end
>
> I am pretty sure this "while" does not belong here:
>
>> while
>
> In general, it is a bad idea to compare floating point numbers exactly. 
> Try abs(id)< 0.00001
>
>> id=kp.'*x
>> if id == 0

In addition to Jos's comments, this line may not do what you think it will 
do.  From HELP IF:

    The statements are executed if the real part of the expression
    has all non-zero elements.

So unless ALL of the elements of id are equal to 0, the IF condition will 
never be satisfied.  If you want to satisfy the IF condition if ANY of the 
elements of id are equal to 0, you'd want:

if any(id == 0)

or, to account for floating-point as Jos said,

if any(abs(id) < 1e-5)

-- 
Steve Lord
slord@mathworks.com