Thread Subject: if statements

Subject: if statements

From: Ongun Palaoglu

Date: 10 Oct, 2008 03:31:02

Message: 1 of 5

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);
while i <= len
    x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);
    i = i + 1;
end
while
id=kp.'*x
if id == 0
    disp('cutoff')
else
    disp('saturation')
end

this is just an exercise i created. what is teh mistake in here. i want to display the cutoff value and saturation value. for example; 2 saturation.

and I also want to improve this code, i want to use saturation values on a different function.

thanks a ot.

Subject: if statements

From: Jos

Date: 10 Oct, 2008 09:15:19

Message: 2 of 5

"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
> disp('cutoff')
> else
> disp('saturation')
> end
>
> this is just an exercise i created. what is teh mistake in here. i want to display the cutoff value and saturation value. for example; 2 saturation.

To show the number, use for instance
disp(id)
>
> and I also want to improve this code, i want to use saturation values on a different function.
>
> thanks a ot.

hth
Jos

Subject: if statements

From: Steven Lord

Date: 10 Oct, 2008 13:45:54

Message: 3 of 5


"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

Subject: if statements

From: Ongun Palaoglu

Date: 10 Oct, 2008 18:54:02

Message: 4 of 5

"Steven Lord" <slord@mathworks.com> wrote in message <gcnmai$4d$1@fred.mathworks.com>...
>
> "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
>
hey,

> >> while i <= len
> >> x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);
> >> i = i + 1;

this part works perfecly fine, when and he result is 3 by 2 matrix, and at the first row's first number is positive and last 2 is 0.
so i want to compare the values,i want to pull zeros out of the ans, and want to call them saturation, and i want to use them for another function to calculate another thing. and it is same for the first number.

thats why I said
if id=0
disp('curoff')
else
disp('sat')
end
after this point i want to call the saturated values, for another function

Subject: if statements

From: swgillan

Date: 11 Oct, 2008 06:36:06

Message: 5 of 5

First of all comments help identify your intention you are trying to
do with you code.
> =A0my code:
>
> %setting parameter
> vtn =3D 0.75;
> vds =3D [0.2 2.5 0];
> vgs =3D [2 2 0];
> knp=3D[200*10^-6 300*10^-6];
> kp=3Dknp*10;
> i =3D 1; len =3D length (vgs);
> while i <=3D len
> =A0 =A0 x(i)=3Dvds(i)*(vgs(i)-vtn-vds(i)/2);
> =A0 =A0 i =3D i + 1;
> end

This part of your code will not work as someone mentioned above
because you are treating the vector id like a scalar. Also it looks
like you are creating an infinite loop here as your while has not exit
condition.

> while
> id=3Dkp.'*x

> if id =3D=3D 0
This like above will only be true of all the elements in id are zero.
> =A0 =A0 disp('cutoff')
> else
> =A0 =A0 disp('saturation')
> end

This, to me, makes a lot more sense.

id=3Dkp.*x;
%Check to see if the value of id is saturated
for i =3D1:len(id)
    if id(i) =3D 0
        disp('cutoff');
   else
        disp('saturated');
   end
end

Hope this helps.

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
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com