Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Tracy
Tracy le 27 Nov 2020
Commenté : Tracy le 28 Nov 2020
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

Réponses (3)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 27 Nov 2020
Modifié(e) : Sulaymon Eshkabilov le 27 Nov 2020
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 le 27 Nov 2020
x = (.993 : 0.01 : 1.283)';
f = polyval([7;-2;7;-14],x);
g = polyval([-19; 3; 16; 8],x);

Image Analyst
Image Analyst le 27 Nov 2020

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by