I keep getting "Array indices must be positive integers or logical values."

조회 수: 3 (최근 30일)
Tracy
Tracy 2020년 11월 27일
댓글: Tracy 2020년 11월 28일
clear;clc
x = .993 : 0.01 : 1.283;
for n = 1 :1: length(x)
f(x) = 7*x(n)^3 - 2*x(n)^2 + 7*x(n) - 14;
g(x) = -19*x(n)^3 + 3*x(n)^2 + 16*x(n) + 8;
end
plot(x,f,'+r',x,g,'-.b')
xlabel('x values')
ylabel('f(x) and g(x) values')
legend('f(x)','g(x)')
I keep getting:
Array indices must be positive integers or logical values.
Error in (line 9)
f(x) = 7*x(n)^3 - 2*x(n)^2 + 7*x(n) -14;
Please help

답변 (3개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 11월 27일
편집: Sulaymon Eshkabilov 2020년 11월 27일
clearvars;clc
x = .993 : 0.01 : 1.283;
for n = 1 :length(x)
f(n) = 7*x(n)^3 - 2*x(n)^2 + 7*x(n) - 14; % Index has to be n
g(n) = -19*x(n)^3 + 3*x(n)^2 + 16*x(n) + 8; % Index has to be n
end
plot(x,f,'+r',x,g,'-.b')
xlabel('x values')
ylabel('f(x) and g(x) values')
legend('f(x)','g(x)')

Andrei Bobrov
Andrei Bobrov 2020년 11월 27일
x = (.993 : 0.01 : 1.283)';
f = polyval([7;-2;7;-14],x);
g = polyval([-19; 3; 16; 8],x);

Image Analyst
Image Analyst 2020년 11월 27일

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by