Got Questions? Get Answers.
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:
error in while function

Subject: error in while function

From: sronggot

Date: 27 Feb, 2008 06:19:29

Message: 1 of 4

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)

Subject: error in while function

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 27 Feb, 2008 06:35:47

Message: 2 of 4

In article <6820854.1204093199469.JavaMail.jakarta@nitrogen.mathforum.org>,
sronggot <istardi@gmail.com> wrote:

>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

i starts at 1, and U1 starts at only one element [U1(1)=500],
and the very first access you make is to U1(i+1) which would be to
U1(2) which does not exist.

>A_total=Duty*1000/(U1(i)*delta_T)
>i=1+i;
>U1(i)=1/A_total;
>end
>U1final=U1(i)


You will have to completely rethink your code; there is no obvious
way to fix it that would not destroy the code structure you have
established.
--
  "Is there any thing whereof it may be said, See, this is new? It hath
  been already of old time, which was before us." -- Ecclesiastes

Subject: error in while function

From: Roger Stafford

Date: 27 Feb, 2008 07:39:02

Message: 3 of 4

sronggot <istardi@gmail.com> wrote in message
<6820854.1204093199469.JavaMail.jakarta@nitrogen.mathforum.org>...
> 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)
--------
  Perhaps you should describe in words what you are trying to accomplish
with your code. The interior of the while-loop seems to be using elements of
the U1 vector that haven't yet been defined. But whether or not that is so,
you appear to be multiplying each element in U1 by a fixed constant, delta_T/
(Duty*1000), to obtain the value which is to replace the next element without
regard to whatever value that element might have previously possessed. This
gives you a geometric series which continues until you encounter two
successive values in U1 that differ by less than one percent. As it stands, it
does not seem to make a lot of sense, and we are left guessing what it is you
really had in mind.

Roger Stafford

Subject: error in while function

From: Yi Cao

Date: 27 Feb, 2008 12:12:02

Message: 4 of 4

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

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