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

2 次查看(过去 30 天)
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