Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
If else not working

Subject: If else not working

From: Md. Mushfiqur

Date: 25 Apr, 2013 23:24:09

Message: 1 of 3

I'm having some problems with a very simple if else statement.
When I am running the program, I see the Matlab is not executing the statements. I am trying to make iterations to get the value of all the variables when -0.05<Pnet<0.05. If the answer comes in range it will display all the variables, otherwise it will incerement the V and go for second iteration. When I run I see Matlab is incrementing 'V' but not recalculating the variables, instead printing the same values for the iteration.
V=3.0;


for i=0:1:300
N=151;
Tsr=(N*3.14*0.34)/(V*60);
Cp=(((-1)*0.4416*Tsr*Tsr)+(0.4342*Tsr));
Pf=(N*0.03*0.1046);
Pair=(1.212*V*V*V*0.153)/2;
Prot=N*0.03*0.1046;
Ke=0.0955;
E=Ke*N;
I=(E-13-1.4)/15;
Pe=E*I*1.04;
Pgen=Pe+Pf;
Pnet=Prot-Pgen;

if((-0.05<Pnet) && (Pnet<0.05))
display(V)
display(N)
display(Tsr)
display(Cp)
display(Pf)
display(Pair)
display(Prot)
display(Ke)
display(E)
display(I)
display(Pe)
display(Pgen)
display(Pnet)
V=V+0.01;
else
    V=V+0.01;
      
end
    end

Subject: If else not working

From: Nasser M. Abbasi

Date: 26 Apr, 2013 01:07:07

Message: 2 of 3

On 4/25/2013 6:24 PM, Md. Mushfiqur wrote:
> I'm having some problems with a very simple if else statement.
> When I am running the program, I see the Matlab is not executing the statements.
>I am trying to make iterations to get the value of all the
>variables when -0.05<Pnet<0.05. If the answer comes in range
>it will display all the variables, otherwise it
>will incerement the V and go for second iteration.
>When I run I see Matlab is incrementing 'V' but not
>recalculating the variables, instead printing the same values for the iteration.

> V=3.0;
>
>
> for i=0:1:300
> N=151;
> Tsr=(N*3.14*0.34)/(V*60);
> Cp=(((-1)*0.4416*Tsr*Tsr)+(0.4342*Tsr));
> Pf=(N*0.03*0.1046);
> Pair=(1.212*V*V*V*0.153)/2;
> Prot=N*0.03*0.1046;
> Ke=0.0955;
> E=Ke*N;
> I=(E-13-1.4)/15;
> Pe=E*I*1.04;
> Pgen=Pe+Pf;
> Pnet=Prot-Pgen;
>
> if((-0.05<Pnet) && (Pnet<0.05))
> display(V)
> display(N)
> display(Tsr)
> display(Cp)
> display(Pf)
> display(Pair)
> display(Prot)
> display(Ke)
> display(E)
> display(I)
> display(Pe)
> display(Pgen)
> display(Pnet)
> V=V+0.01;
> else
> V=V+0.01;
>
> end
> end
>

I do not understand your logic.

You are effectively writing this:

--------------------------
IF something_is_true THEN
     V = V+0.01
ELSE
     V = V+0.01
END IF
--------------------

Then why not just write

    V=V+0.01

i.e Why are you checking for something to be true or not, if you
are going to do the same thing regadless?

--Nasser

Subject: If else not working

From: Md. Mushfiqur

Date: 4 May, 2013 21:30:10

Message: 3 of 3

"Nasser M. Abbasi" wrote in message <klcjvv$187$1@speranza.aioe.org>...
> On 4/25/2013 6:24 PM, Md. Mushfiqur wrote:
> > I'm having some problems with a very simple if else statement.
> > When I am running the program, I see the Matlab is not executing the statements.
> >I am trying to make iterations to get the value of all the
> >variables when -0.05<Pnet<0.05. If the answer comes in range
> >it will display all the variables, otherwise it
> >will incerement the V and go for second iteration.
> >When I run I see Matlab is incrementing 'V' but not
> >recalculating the variables, instead printing the same values for the iteration.
>
> > V=3.0;
> >
> >
> > for i=0:1:300
> > N=151;
> > Tsr=(N*3.14*0.34)/(V*60);
> > Cp=(((-1)*0.4416*Tsr*Tsr)+(0.4342*Tsr));
> > Pf=(N*0.03*0.1046);
> > Pair=(1.212*V*V*V*0.153)/2;
> > Prot=N*0.03*0.1046;
> > Ke=0.0955;
> > E=Ke*N;
> > I=(E-13-1.4)/15;
> > Pe=E*I*1.04;
> > Pgen=Pe+Pf;
> > Pnet=Prot-Pgen;
> >
> > if((-0.05<Pnet) && (Pnet<0.05))
> > display(V)
> > display(N)
> > display(Tsr)
> > display(Cp)
> > display(Pf)
> > display(Pair)
> > display(Prot)
> > display(Ke)
> > display(E)
> > display(I)
> > display(Pe)
> > display(Pgen)
> > display(Pnet)
> > V=V+0.01;
> > else
> > V=V+0.01;
> >
> > end
> > end
> >
>
> I do not understand your logic.
>
> You are effectively writing this:
>
> --------------------------
> IF something_is_true THEN
> V = V+0.01
> ELSE
> V = V+0.01
> END IF
> --------------------
>
> Then why not just write
>
> V=V+0.01
>
> i.e Why are you checking for something to be true or not, if you
> are going to do the same thing regadless?
>
> --Nasser
>
Thanks Nasser Bhai
There was an error as well in the equations. I have corrected it but now am finding another error with matrix. I trying to tell the program that calculate Pnet increasing V by 0.01 withing the range -0.05<Pnet && Pnet<0.05, using the 14 values of N in the matrix. After the 14th value of N, the program should terminate and print the values

V=3.0;
j=1; % j will pick the required values from N matrix
N=[150 151.2 154.2 156.9 161.4 167.3 176.7 182 187.5 193.5 200 206.6 213.5 224.6];
for i=0:1:300
V=V+0.01;
Tsr=(N(j)*3.14*0.34)/(V*60); %first Tsr will work with the 1st value N(1) which is 150
Cp=(((-1)*0.4416*Tsr*Tsr)+(0.4342*Tsr));
Pf=(N(j)*0.03*0.1046);
Pair=(1.212*V*V*V*0.153)/2;
Prot=N(j)*0.03*0.1046;
Ke=0.0955;
E=Ke*N(j);
I=(E-13-1.4)/15;
Pe=E*I*1.04;
Pgen=Pe+Pf;
Pnet=Prot-Pgen;
if((-0.05<Pnet) && (Pnet<0.05))
display(V)
display(N)
display(Tsr)
display(Cp)
display(Pf)
display(Pair)
display(Prot)
display(Ke)
display(E)
display(I)
display(Pe)
display(Pgen)
display(Pnet)
j=j+1; %after working 1st element j will switch to the consecutive element of N matrix
if j==15
    break % j will increase the value until it works with the last element of N matrix
   
end
end
   
end

Tags for this Thread

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.

Contact us