"Index exceeds the number of array elements (1)" error

First off I am not very fluent with MATLAB so bear with me if I have extensive amounts of mistakes. My main concern is why I am getting the error message that I have listed in the title. Matlab claims my error is in the underlined text (percentaccuracy = ((abs(lnx(n+1)-lnx(n)))/(lnx(n+1)))*100).
function lnx = HW9(x,P)
x = input('x = ');
P = input('P = ');
n = 1;
lnx(n) = ((((-1)^(n+1))/n)*((x-1)^n));
percentaccuracy = ((abs(lnx(n+1)-lnx(n)))/(lnx(n+1)))*100
while percentaccuracy >= P
n = n+1;
lnx(n) = ((((-1)^(n+1))/n)*((x-1)^n))+lnx(n-1);
percentaccuracy = (abs(lnx(n)-lnx(n-1))/(lnx(n)))*100;
end
end

Answers (1)

It appears that you intend ‘lnx’ to be a function.
Try this:
lnx = @(n) ((((-1)^(n+1))/n)*((x-1)^n));
then see the documentation on Anonymous Functions (link).

Asked:

on 3 Apr 2019

Answered:

on 3 Apr 2019

Community Treasure Hunt

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

Start Hunting!