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:
matlab error in code

Subject: matlab error in code

From: matlab_learner

Date: 16 Mar, 2010 01:39:55

Message: 1 of 5

so i have written the following code and Matlab complains. can u tell
what i am doing wrong? it might b helpful to copy this and run.
tnx:

clear all
clc
a = 250;
h=.2;
t=.5;
for i = 1:2001
x(i) = (i-1) * h;
end

for i = 1:251
fi(i) = 0;
end
for i = 251:551
fi(i) = 100 * (sin( pi * ((x(i) - 50)/60)));
end
for i = 551:2001
fi(i) = 0;
end
for i = 2:2001
    if (x(i) - (a*t))<0
        fiExact(i) = 0;
    else
        x_position = ((x(i) - (a*t))/0.2)+5
    fiExact(i) = fi(x_position)
    end
end
but it says error @ first x_position value which is 6:
??? Attempted to access fi(6); index must be a positive integer or
logical.

Error in ==> num_4_exact at 56
    fiExact(i) = fi(x_position)

tnx

Subject: matlab error in code

From: Walter Roberson

Date: 16 Mar, 2010 02:14:35

Message: 2 of 5

matlab_learner wrote:

> x_position = ((x(i) - (a*t))/0.2)+5
> fiExact(i) = fi(x_position)

> but it says error @ first x_position value which is 6:
> ??? Attempted to access fi(6); index must be a positive integer or
> logical.

round-off error in the calculation of x_position...
just like is posted by someone new at least every second day,
which is why we recommend that people start by reading the Matlab FAQ.

Subject: matlab error in code

From: ImageAnalyst

Date: 16 Mar, 2010 02:17:18

Message: 3 of 5

Type format long on the command line and you'll see

x_position =
   6.000000000000014

Try rounding x_position
round(x_position)
before you use it.

Subject: matlab error in code

From: Roger Stafford

Date: 16 Mar, 2010 02:17:25

Message: 4 of 5

matlab_learner <cibeji@gmail.com> wrote in message <a969895a-2e5f-4b7c-a821-12a8c85fa040@w27g2000pre.googlegroups.com>...
> so i have written the following code and Matlab complains. can u tell
> what i am doing wrong? it might b helpful to copy this and run.
> ........

  In the line "x_position = ((x(i) - (a*t))/0.2)+5" you have assumed that because x(i) is an integral multiple of .2, then dividing by .2 will return you to an integer value, but that is not true on binary machines. Roundoff will cause very small errors in the least bit positions and render this false. Try displaying x_position to a high accuracy (17 decimal places or higher) to see how this is true.

  You need to rewrite your code so as to ensure an integer. There are a great many ways to do this.

  I should also point out that there are many more efficient ways to accomplish other aspects of the code. For example, x can be set up in one line of code: "x = (0:2000)*h;".

Roger Stafford

Subject: matlab error in code

From: matlab_learner

Date: 16 Mar, 2010 02:47:58

Message: 5 of 5

Well well...as u guys can see, i am a novice. i need people like u to
help me as i stumble along. never took a matlab class b4, going solo.
not the easiest... will try your suggestions.

On Mar 15, 7:17 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> matlab_learner <cib...@gmail.com> wrote in message <a969895a-2e5f-4b7c-a821-12a8c85fa...@w27g2000pre.googlegroups.com>...
> > so i have written the following code and Matlab complains. can u tell
> > what i am doing wrong? it might b helpful to copy this and run.
> > ........
>
>   In the line  "x_position = ((x(i) - (a*t))/0.2)+5" you have assumed that because x(i) is an integral multiple of .2, then dividing by .2 will return you to an integer value, but that is not true on binary machines.  Roundoff will cause very small errors in the least bit positions and render this false.  Try displaying x_position to a high accuracy (17 decimal places or higher) to see how this is true.
>
>   You need to rewrite your code so as to ensure an integer.  There are a great many ways to do this.
>
>   I should also point out that there are many more efficient ways to accomplish other aspects of the code.  For example, x can be set up in one line of code: "x = (0:2000)*h;".
>
> Roger Stafford

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