Info

This question is closed. Reopen it to edit or answer.

How to solve an equation and build a vector using a for loop?

1 view (last 30 days)
I'm trying to solve the equation below using a for loop just to make the coding a little bit neater. Heres's the problem. And here is my code:
r = 0:0.0025:0.0175; %radius values
for i = 0:8 % values
dela_delv = -r(i).*(r(i)-r(i+1));% equation
end
I keep getting the error: Array indices must be positive integers or logical values. Error in ME_345_Lab_5 (line 128) dela_delv(i) = -r(i).*(r(i)-r(i+1));% equation >> Some help and guidance would be very much appreciated. TIA

Answers (1)

Star Strider
Star Strider on 18 Oct 2018
Note that zero is not a positive integer. Positive integers are integers greater than zero.
Try this:
r = 0:0.0025:0.0175; %radius values
for i = 1:numel(r)-1 % values
dela_delv(i) = -r(i).*(r(i)-r(i+1)); % equation
end
Also, ‘r(i+1)’ does not exist if you are using every value of ‘r’ in the array, so I shortened the loop by 1 to avoid this problem.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!