How can I fix the error "Index in position 2 exceeds array bounds. Index must not exceed 1.

4 views (last 30 days)
Hello! the code below was from a lab that was given in class. I inpputted it in MATLAB but it keeps giving me an error. I tried to change things like (),[], ',' but it still gives me the same error. I'm out of options how to fix it. Can anyone please help? thank you! the code is:
for k = 1:length(x_points)
fprintf('\nA %s exists at x = %4.2f, y = %4.2f.\n\n',outcomes{1,k},...
double(x_points(k)),double(y_points(k)))
end
fplot(y,[double(x_points(1))-1 double(x_points(length(x_points)))+1])
grid minor
  2 Comments
Voss
Voss on 26 Feb 2022
Looks like outcomes has 1 column, but x_points has more than 1 element, so when you get to the second iteration of the for loop (k = 2), outcomes{1,k} (i.e., outcomes{1,2}) doesn't exist.
Check that outcomes and x_points and y_points are all defined correctly. You can post the code that creates those variables, if you have it, and someone can probably tell you how to fix it.
Trixiajane Hess
Trixiajane Hess on 26 Feb 2022
Edited: Cris LaPierre on 26 Feb 2022
Hi so these are the codes prior to the first post:
clear,clc,close all
syms x
y = x^4-(16/3)*x^3+8*x^2-4; % initialize function
dydx = diff(y); % find derivative
d2ydx2 = diff(dydx); % find second derivative
x_points = solve(dydx); % find roots of derivative dy/dx = 0
y_points = subs(y,x,x_points);
for k = 1:length(x_points)
eval(k) = subs(d2ydx2,x,x_points(k)); % solving 2nd derivative for x value
if double(eval(k)) > 0 % checking to see if loocal minima
outcomes(k) = "local minima";
elseif double(eval(k)) == 0 % checking to see if inflection point
outomes(k) = "inflection point";
else
outcomes(k) = "local maxima"; % otherwise, point is local maxima
end
end
Thank you!

Sign in to comment.

Answers (1)

Voss
Voss on 26 Feb 2022
outcomes was defined as a string array, so it should be indexed with () not {}. And there was a typo after the elseif line ("outomes" instead of "outcomes"). Fixing those two things seems to have it working now:
clear,clc,close all
syms x
y = x^4-(16/3)*x^3+8*x^2-4; % initialize function
dydx = diff(y); % find derivative
d2ydx2 = diff(dydx); % find second derivative
x_points = solve(dydx); % find roots of derivative dy/dx = 0
y_points = subs(y,x,x_points);
for k = 1:length(x_points)
eval(k) = subs(d2ydx2,x,x_points(k)); % solving 2nd derivative for x value
if double(eval(k)) > 0 % checking to see if loocal minima
outcomes(k) = "local minima";
elseif double(eval(k)) == 0 % checking to see if inflection point
outcomes(k) = "inflection point";
else
outcomes(k) = "local maxima"; % otherwise, point is local maxima
end
end
for k = 1:length(x_points)
fprintf('\nA %s exists at x = %4.2f, y = %4.2f.\n\n',outcomes(k),...
double(x_points(k)),double(y_points(k)))
end
A local minima exists at x = 0.00, y = -4.00. A inflection point exists at x = 2.00, y = 1.33. A inflection point exists at x = 2.00, y = 1.33.
fplot(y,[double(x_points(1))-1 double(x_points(length(x_points)))+1])
grid minor
  2 Comments
Trixiajane Hess
Trixiajane Hess on 26 Feb 2022
Oh! it worked! I brushed through the codes, rechecked it so many times but I guess I still missed the typo. thank you!
Voss
Voss on 26 Feb 2022
You're welcome! Let me know if you have any questions. If not, please mark the answer as 'Accepted'. Thanks!

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!