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:
Can´t run my for-loop

Subject: Can´t run my for-loop

From: rastrab Hellmann

Date: 21 Sep, 2010 19:27:04

Message: 1 of 9

Hello!

This is my code (including error message):

p0=31.5;
d=0.051;
mPunkt=1.14;
c=1.2;
l=1;
A=0.25*pi*d^2;
V=A*l;
M=1;
hein=[2900 2915 2930 2945];
Tein=[290 293 296 299];
pein=[31 31.05 31.1 31.15];
Q=[1200 -320 -320 -322
1200 1200 -322 -322
1200 1200 1200 -322
1200 1200 1200 1200];
H=zeros(4, 4);
TD=zeros(4, 4);
TW=zeros(4, 4);
delta_h_sun(i,j)=zeros(4, 4);
delta_h_cap(i,j)=zeros(4, 4);

for i=1:1:4,
for j=1:1:4,

if j<=i,
H(i, j)=hein(j);
TD(i, j)=Tein(j);
TW(i, j)=Tein(j);

else
p=pein(j);
h=H(i,j-1);
Tw=TW(i,j-1);
T=TD(i,j-1);
roh=0.00015216+0.0372*h;
TR=(T+273.15)/647.096;
zeta=0.176*(647.096)^(1/6);
Fp=1+30.55*(0.292-0.2295);
vis=Fp/zeta*(0.807*TR^0.618);
cp=0.0711*p-0.0137*T+4.0873;
Lambda=0.05;
Pr=vis*cp/Lambda;
Re=mPunkt*d/(A*vis);
BeiwertNu=(1.8*log10(Re)-1.5);
Nu=BeiwertNu/8*Re*Pr);
alpha=Lambda/d*Nu;
delta_h_sun(i,j)=Q(i,j)/(roh*V);
delta_h_cap(i,j)=alpha*A*(Tw-T);
H(i,j)=H(i,j-1)+delta_h_sun(i,j)+delta_h_cap(i,j);
deltaTW=(Q(i,j)-delta_h_cap(i,j));
deltaTD=delta_h_cap(i,j)/c;
TW(i,j)=Tw+deltaTW;
TD(i,j)=T+deltaTD;
end
end
end
??? Subscript indices must either be real positive integers or logicals.

%seems as if one of the indices would turn 0 or negative during loop.
%But I can´t see that!!!
%Anyone another idea???
%
% Thanks !!!!

Subject: Can´t run my for-loop

From: Jan Simon

Date: 21 Sep, 2010 19:36:04

Message: 2 of 9

Dear rastrab,

> ??? Subscript indices must either be real positive integers or logicals.
>
> %seems as if one of the indices would turn 0 or negative during loop.
> %But I can´t see that!!!

Nice. You do not have to see it. Matlab can do this for you:
1. Look in the line number, which appears together with the error message.
2. Let Matlab's debugger stop the program, when the error occurs:
  dbstop if error
Then you can inspect the values of the variables in the command window. You can copy the line, split it inot parts and evaluate it atom for atom.

Using the debugger is even more promissing than asking this excellent newsgroup, because the debugger has access to your data.

Good luck, Jan

Subject: Can´t run my for-loop

From: Sean

Date: 21 Sep, 2010 19:37:05

Message: 3 of 9

"rastrab Hellmann" <wohnung.berlin.abMaerz@gmx.de> wrote in message <i7b0u8$ff3$1@fred.mathworks.com>...
> Hello!
>
> This is my code (including error message):
>
> p0=31.5;
> d=0.051;
> mPunkt=1.14;
> c=1.2;
> l=1;
> A=0.25*pi*d^2;
> V=A*l;
> M=1;
> hein=[2900 2915 2930 2945];
> Tein=[290 293 296 299];
> pein=[31 31.05 31.1 31.15];
> Q=[1200 -320 -320 -322
> 1200 1200 -322 -322
> 1200 1200 1200 -322
> 1200 1200 1200 1200];
> H=zeros(4, 4);
> TD=zeros(4, 4);
> TW=zeros(4, 4);
> delta_h_sun(i,j)=zeros(4, 4);
> delta_h_cap(i,j)=zeros(4, 4);
>
> for i=1:1:4,
> for j=1:1:4,
>
> if j<=i,
> H(i, j)=hein(j);
> TD(i, j)=Tein(j);
> TW(i, j)=Tein(j);
>
> else
> p=pein(j);
> h=H(i,j-1);
> Tw=TW(i,j-1);
> T=TD(i,j-1);
> roh=0.00015216+0.0372*h;
> TR=(T+273.15)/647.096;
> zeta=0.176*(647.096)^(1/6);
> Fp=1+30.55*(0.292-0.2295);
> vis=Fp/zeta*(0.807*TR^0.618);
> cp=0.0711*p-0.0137*T+4.0873;
> Lambda=0.05;
> Pr=vis*cp/Lambda;
> Re=mPunkt*d/(A*vis);
> BeiwertNu=(1.8*log10(Re)-1.5);
> Nu=BeiwertNu/8*Re*Pr);
> alpha=Lambda/d*Nu;
> delta_h_sun(i,j)=Q(i,j)/(roh*V);
> delta_h_cap(i,j)=alpha*A*(Tw-T);
> H(i,j)=H(i,j-1)+delta_h_sun(i,j)+delta_h_cap(i,j);
> deltaTW=(Q(i,j)-delta_h_cap(i,j));
> deltaTD=delta_h_cap(i,j)/c;
> TW(i,j)=Tw+deltaTW;
> TD(i,j)=T+deltaTD;
> end
> end
> end
> ??? Subscript indices must either be real positive integers or logicals.
>
> %seems as if one of the indices would turn 0 or negative during loop.
> %But I can´t see that!!!
> %Anyone another idea???
> %
> % Thanks !!!!

In the command window type:
>>dbstop if error
Then when it stops look at all of the values and see which one is either negative 0 or non integer.

I'll give you a hint:
H(i,j)=***H(i,j-1)***

Subject: Can´t run my for-loop

From: someone

Date: 21 Sep, 2010 19:40:22

Message: 4 of 9

"rastrab Hellmann" <wohnung.berlin.abMaerz@gmx.de> wrote in message <i7b0u8$ff3$1@fred.mathworks.com>...
> Hello!
>
> This is my code (including error message):
>
> p0=31.5;
> d=0.051;
> mPunkt=1.14;
> c=1.2;
> l=1;
> A=0.25*pi*d^2;
> V=A*l;
> M=1;
> hein=[2900 2915 2930 2945];
> Tein=[290 293 296 299];
> pein=[31 31.05 31.1 31.15];
> Q=[1200 -320 -320 -322
> 1200 1200 -322 -322
> 1200 1200 1200 -322
> 1200 1200 1200 1200];
> H=zeros(4, 4);
> TD=zeros(4, 4);
> TW=zeros(4, 4);
> delta_h_sun(i,j)=zeros(4, 4);
> delta_h_cap(i,j)=zeros(4, 4);

The problem is probably the two lines above.
You haven't defined i & j yet, so MATLAB is
using the default values of sqrt(-1) for them.

A good habit to get into is to avoid using i & j
for indexes for that reason. Consider using ii & jj for example.

>
> for i=1:1:4,
> for j=1:1:4,
>
> if j<=i,
> H(i, j)=hein(j);
> TD(i, j)=Tein(j);
> TW(i, j)=Tein(j);
>
> else
> p=pein(j);
> h=H(i,j-1);
> Tw=TW(i,j-1);
> T=TD(i,j-1);
> roh=0.00015216+0.0372*h;
> TR=(T+273.15)/647.096;
> zeta=0.176*(647.096)^(1/6);
> Fp=1+30.55*(0.292-0.2295);
> vis=Fp/zeta*(0.807*TR^0.618);
> cp=0.0711*p-0.0137*T+4.0873;
> Lambda=0.05;
> Pr=vis*cp/Lambda;
> Re=mPunkt*d/(A*vis);
> BeiwertNu=(1.8*log10(Re)-1.5);
> Nu=BeiwertNu/8*Re*Pr);
> alpha=Lambda/d*Nu;
> delta_h_sun(i,j)=Q(i,j)/(roh*V);
> delta_h_cap(i,j)=alpha*A*(Tw-T);
> H(i,j)=H(i,j-1)+delta_h_sun(i,j)+delta_h_cap(i,j);
> deltaTW=(Q(i,j)-delta_h_cap(i,j));
> deltaTD=delta_h_cap(i,j)/c;
> TW(i,j)=Tw+deltaTW;
> TD(i,j)=T+deltaTD;
> end
> end
> end
> ??? Subscript indices must either be real positive integers or logicals.
>
> %seems as if one of the indices would turn 0 or negative during loop.
> %But I can´t see that!!!
> %Anyone another idea???
> %
> % Thanks !!!!

Subject: Can´t run my for-loop

From: rastrab Hellmann

Date: 21 Sep, 2010 19:43:21

Message: 5 of 9

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i7b1f4$k8s$1@fred.mathworks.com>...
> Dear rastrab,
>
> > ??? Subscript indices must either be real positive integers or logicals.
> >
> > %seems as if one of the indices would turn 0 or negative during loop.
> > %But I can´t see that!!!
>
> Nice. You do not have to see it. Matlab can do this for you:
> 1. Look in the line number, which appears together with the error message.
> 2. Let Matlab's debugger stop the program, when the error occurs:
> dbstop if error
> Then you can inspect the values of the variables in the command window. You can copy the line, split it inot parts and evaluate it atom for atom.
>
> Using the debugger is even more promissing than asking this excellent newsgroup, because the debugger has access to your data.
>
> Good luck, Jan
Thanks Jan!!!

Subject: Can´t run my for-loop

From: Walter Roberson

Date: 21 Sep, 2010 19:41:42

Message: 6 of 9

On 10-09-21 02:27 PM, rastrab Hellmann wrote:
> Hello!
>
> This is my code (including error message):
>
> p0=31.5;
> d=0.051;
> mPunkt=1.14;
> c=1.2;
> l=1;
> A=0.25*pi*d^2;
> V=A*l;
> M=1;
> hein=[2900 2915 2930 2945];
> Tein=[290 293 296 299];
> pein=[31 31.05 31.1 31.15];
> Q=[1200 -320 -320 -322
> 1200 1200 -322 -322
> 1200 1200 1200 -322
> 1200 1200 1200 1200];
> H=zeros(4, 4);
> TD=zeros(4, 4);
> TW=zeros(4, 4);
> delta_h_sun(i,j)=zeros(4, 4);
> delta_h_cap(i,j)=zeros(4, 4);

> ??? Subscript indices must either be real positive integers or logicals.
>
> %seems as if one of the indices would turn 0 or negative during loop.
> %But I can´t see that!!!
> %Anyone another idea???

At the point I truncated the code at, i and j have not been defined by your
code. They would therefore still have their original values, and by default
both i and j mean the square root of negative 1 in Matlab. Your problem is not
in your loop but rather before your loop.

Subject: Can´t run my for-loop

From: rastrab Hellmann

Date: 21 Sep, 2010 19:45:22

Message: 7 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i7b1h1$o0g$1@fred.mathworks.com>...
> "rastrab Hellmann" <wohnung.berlin.abMaerz@gmx.de> wrote in message <i7b0u8$ff3$1@fred.mathworks.com>...
> > Hello!
> >
> > This is my code (including error message):
> >
> > p0=31.5;
> > d=0.051;
> > mPunkt=1.14;
> > c=1.2;
> > l=1;
> > A=0.25*pi*d^2;
> > V=A*l;
> > M=1;
> > hein=[2900 2915 2930 2945];
> > Tein=[290 293 296 299];
> > pein=[31 31.05 31.1 31.15];
> > Q=[1200 -320 -320 -322
> > 1200 1200 -322 -322
> > 1200 1200 1200 -322
> > 1200 1200 1200 1200];
> > H=zeros(4, 4);
> > TD=zeros(4, 4);
> > TW=zeros(4, 4);
> > delta_h_sun(i,j)=zeros(4, 4);
> > delta_h_cap(i,j)=zeros(4, 4);
> >
> > for i=1:1:4,
> > for j=1:1:4,
> >
> > if j<=i,
> > H(i, j)=hein(j);
> > TD(i, j)=Tein(j);
> > TW(i, j)=Tein(j);
> >
> > else
> > p=pein(j);
> > h=H(i,j-1);
> > Tw=TW(i,j-1);
> > T=TD(i,j-1);
> > roh=0.00015216+0.0372*h;
> > TR=(T+273.15)/647.096;
> > zeta=0.176*(647.096)^(1/6);
> > Fp=1+30.55*(0.292-0.2295);
> > vis=Fp/zeta*(0.807*TR^0.618);
> > cp=0.0711*p-0.0137*T+4.0873;
> > Lambda=0.05;
> > Pr=vis*cp/Lambda;
> > Re=mPunkt*d/(A*vis);
> > BeiwertNu=(1.8*log10(Re)-1.5);
> > Nu=BeiwertNu/8*Re*Pr);
> > alpha=Lambda/d*Nu;
> > delta_h_sun(i,j)=Q(i,j)/(roh*V);
> > delta_h_cap(i,j)=alpha*A*(Tw-T);
> > H(i,j)=H(i,j-1)+delta_h_sun(i,j)+delta_h_cap(i,j);
> > deltaTW=(Q(i,j)-delta_h_cap(i,j));
> > deltaTD=delta_h_cap(i,j)/c;
> > TW(i,j)=Tw+deltaTW;
> > TD(i,j)=T+deltaTD;
> > end
> > end
> > end
> > ??? Subscript indices must either be real positive integers or logicals.
> >
> > %seems as if one of the indices would turn 0 or negative during loop.
> > %But I can´t see that!!!
> > %Anyone another idea???
> > %
> > % Thanks !!!!
>
> In the command window type:
> >>dbstop if error
> Then when it stops look at all of the values and see which one is either negative 0 or non integer.
>
> I'll give you a hint:
> H(i,j)=***H(i,j-1)***
Hey Sean,

thanks for your advice!!!
Even though your hint is not the problem;)

Subject: Can´t run my for-loop

From: Sean

Date: 21 Sep, 2010 20:03:08

Message: 8 of 9


> > H(i,j)=***H(i,j-1)***
> Hey Sean,
>
> thanks for your advice!!!
> Even though your hint is not the problem;)

You're right; I missed the if statement constraining j to be at least 2.

 Use Walter's advice - replace all i's with ii's and j's with jj's or ix/jx something that's not sqrt(-1)

Subject: Can´t run my for-loop

From: Walter Roberson

Date: 21 Sep, 2010 20:05:06

Message: 9 of 9

On 10-09-21 03:03 PM, Sean wrote:

> Use Walter's advice - replace all i's with ii's and j's with jj's or
> ix/jx something that's not sqrt(-1)

That was "someone"'s advice; I was one of the people that pointed out the
initialization with subscript i and j before those subscripts were defined.
"someone" beat me to that, though, by a minute or two.

Tags for this Thread

No tags are associated with 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