Indexing errors on sum

2 visualizaciones (últimos 30 días)
Reid Sams
Reid Sams el 29 de Sept. de 2020
Respondida: Steven Lord el 30 de Sept. de 2020
In = [38.68 7.84 5.14 3.92 3.14 2.62 2.22 1.78 1.74 1.55];
X = [1.4875 -0.4216 0.0390 -0.0008]
for c = 1:length(In)
Yf = @(In)sum(X(1:end).*In(c).^(0:Order));
Y(c) = Yf(c)
end
plot(In,Y)
This gives me the error Index exceeds the number of array elements (1).
the code is supposed to take the length of X(a list of coeficients) and plots the function
Y = X(1)*In.^(0) + X(2)*In.^(1) + X(3)*In.^(2) + X(4)*In.^(3)
So if the length of X is 5, then the function should be
Y = X(1)*In.^(0) + X(2)*In.^(1) + X(3)*In.^(2) + X(4)*In.^(3) + X(5)*In.^(4)
Am I doing this wrong? Is it possible to have matlab build the function itself like this?

Respuesta aceptada

Steven Lord
Steven Lord el 30 de Sept. de 2020
I'm guessing you've defined a variable named sum that's preventing you from calling the sum function. If my guess is correct clear the variable and recreate the anonymous functions.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by