Path: news.mathworks.com!not-for-mail
From: "Yi Cao" <y.cao@cranfield.ac.uk>
Newsgroups: comp.soft-sys.matlab
Subject: Re: error in while function
Date: Wed, 27 Feb 2008 12:12:02 +0000 (UTC)
Organization: Cranfield University
Lines: 43
Message-ID: <fq3k2i$ruq$1@fred.mathworks.com>
References: <6820854.1204093199469.JavaMail.jakarta@nitrogen.mathforum.org>
Reply-To: "Yi Cao" <y.cao@cranfield.ac.uk>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1204114322 28634 172.30.248.35 (27 Feb 2008 12:12:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 27 Feb 2008 12:12:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 69713
Xref: news.mathworks.com comp.soft-sys.matlab:453979


sronggot <istardi@gmail.com> wrote in message 
<6820854.1204093199469.JavaMail.jakarta@nitrogen.mathforum.o
rg>...
> Dear,
> 
> I have problem with my matlab programming, i use while 
for my iteration and after i run my program.. there are 
message error like this 
> 
> '??? Attempted to access U1(:,1); index out of bounds 
because numel(U1)=1.'
> 
> anyone can help me to solve this problem..
> 
> this my simple program :
> 
>  i=1;
>         U1(1)=500;%First guess [W/m2C]
>         while abs((U1(i+1)-U1(i))/U1(i+1))>=0.01
> A_total=Duty*1000/(U1(i)*delta_T)
> i=1+i;
> U1(i)=1/A_total;
> end
> U1final=U1(i)

I guess the following code is what you want although it is 
not a good practice to dynamically expand U1:

Duty=0.1; % just take a value for test
delta_T=1;
e=100; % initial error
i=1;
U1(1)=500;
while e>=0.01
A_total=Duty*1000/(U1(i)*delta_T);
i=1+i;
U1(i)=1/A_total;
e=abs((U1(i)-U1(i-1))/U1(i));  % calculate error now but 
shif index by 1
end
U1final=U1(i)

Yi