Indexing errors on sum

조회 수: 1 (최근 30일)
Reid Sams
Reid Sams 2020년 9월 29일
답변: Steven Lord 2020년 9월 30일
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?

채택된 답변

Steven Lord
Steven Lord 2020년 9월 30일
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.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by