Array problem on finding slopes

I have a problem with error;
"Index exceeds the number of array elements (10)."
The code goes like this:
A=[0,198.6026,397.2053,993.01346,2376.65783,3968.16977,4566.799,5161.454,6956.4044,9930.1346];
B=[0.29835,0.3978,0.467415,0.546975,0.745875,0.975375,1.09395,1.3923,2.56275,4.5288];
LS=zeros(1,5);
RS=zeros(1,5);
for n=1:5
for i=1:length(A)
LS(n)=B(i+1)-B(i)/(A(i+1)-A(i));
RS(n)=B(i+2)-B(i+1)/(A(i+2)-A(i+1));
LS(n+1)=B(i+3)-B(i+2)/(A(i+3)-A(i+2));
RS(n+1)=B(i+4)-B(i+3)/(A(i+4)-A(i+3));
LS(n+2)=B(i+5)-B(i+4)/(A(i+5)-A(i+4));
RS(n+2)=B(i+6)-B(i+5)/(A(i+6)-A(i+5));
LS(n+3)=B(i+7)-B(i+6)/(A(i+7)-A(i+6));
RS(n+3)=B(i+8)-B(i+7)/(A(i+8)-A(i+7));
LS(n+4)=B(i+9)-B(i+8)/(A(i+9)-A(i+8));
RS(n+4)=B(i+length(A))-B(i+9)/(A(i+length(A))-A(i+9));
end
end

2 Comments

The error message is clear enough. The A array is of length 10, so
A(i+length(A))
makes no sense. The first time the loop executes, i=1. You are attempting to extract the 11th element from A but it only contains 10 elements.
Hi Scott. Thank you for pointing that out. And the for loop for n is also not required. I just realized that about an hour I posted the question. The problem is now solved. Cheers.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2020b

Tags

Asked:

on 13 Jun 2021

Edited:

on 13 Jun 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!