Please Help! I keep getting an error code: Attempted to access abs(0.0005); index must be a positive integer or logical. Error in Lab10A (line 7) abs(Err)=999;
Show older comments
This is my whole function so you can see what I'm doing, for some reason my function won't run because of the error code listed above and my homework is due asap! Super frustrated :(
function [Output1,Output2] = Lab10A(R,V,h,Err) % The function takes four inputs: R = radius (m); V = Volume (m^3), h = %initial height(m) and Err = relative percent error that is %acceptable(%) and calculates output1 is equal to get the converged %solution for h(m) in scalar, Also output2 is the number of iterations.
i = 1; h(i) = h; n = 0; abs(Err)=999;
while abs(Err) > Err h(i+1) = h(i)- ((pi/3)*h(i)^2*(3*R-h(i))-V)/((2*pi*R*h(i))-(pi*h(i)^2)); abs(Err) = 100*abs(h(i+1)-h(i))/abs(h(i+1)); i = i+1; n = n+1; end Output1 = h(end); Output2 = n;
3 Comments
dpb
on 28 Jul 2013
Format the code so it's legible---
Use the debugger and step thru the code and see where your logic fails.
You've aliased the internal function abs() w/ an assignment (or at least tried to if it weren't for the fact that Err is a numeric value apparently so it failed to do so). Don't do that...
Taylor
on 28 Jul 2013
Jan
on 28 Jul 2013
Please, Taylor, format your code properly. Follow the "? Help" link to learn the basics about this forum. Thanks.
Answers (1)
This happens, when abs is defined as a variable:
clear('abs');
abs(0.05) % Works
abs = 1:10;
abs(0.05) % Fails
In your case it is:
abs(Err) = 999;
which tries to create a variable abs and assign 999 to the index with the number Err. But it is not meaningful to assign anything to "abs(Err)". Perhaps you mean "abs_Err" or any other matching name.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!