필터 지우기
필터 지우기

Indexing error while using symsum inside a function.

조회 수: 2 (최근 30일)
Bear Foot
Bear Foot 2023년 12월 5일
편집: Walter Roberson 2023년 12월 5일
Does anyone know why I am recieving the indexing error below?
x = 0:0.01:5
x = 1×501
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
y = [2 -1 3 -2 4 -3 2 -4 1 -2]
y = 1×10
2 -1 3 -2 4 -3 2 -4 1 -2
Test(x,y)
Error using indexing
Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression.

Error in solution>Test (line 7)
f = symsum(y(k)*sin(2*pi*k*x),k,1,size(y));
function f = Test(x,y)
syms k
f = symsum(y(k)*sin(2*pi*k*x),k,1,size(y));
end
  댓글 수: 2
Chunru
Chunru 2023년 12월 5일
What is the desired output? A value or a symbolic expression?
Bear Foot
Bear Foot 2023년 12월 5일
@Chunru, I am trying to get a value.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2023년 12월 5일
y = [2 -1 3 -2 4 -3 2 -4 1 -2]
Your y is a numeric vector.
Test(x,y)
you are passing that numeric vector as the second parameter to Test
function f = Test(x,y)
Internally, Test knows that numeric vector under the name y
syms k
f = symsum(y(k)*sin(2*pi*k*x),k,1,size(y));
You try to index that numeric vector with a symbolic variable. In MATLAB, you can never index an array at a symbolic variable.
What you need to do is
function f = Test(x,y)
f = sum(y.*sin(2*pi*(1:numel(y)).*x));
end
That is, form a definite list of terms and sum() them.
  댓글 수: 2
Bear Foot
Bear Foot 2023년 12월 5일
이동: Walter Roberson 2023년 12월 5일
@Walter Roberson, I appreciate the help, are there any code adjustments I can make to fix the arrays have incompatible sizes error?
x = 0:0.01:5
x = 1×501
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
y = [2 -1 3 -2 4 -3 2 -4 1 -2]
y = 1×10
2 -1 3 -2 4 -3 2 -4 1 -2
Test(x,y)
Arrays have incompatible sizes for this operation.

Error in solution>Test (line 5)
f = sum(y.*sin(2*pi*(1:numel(y)).*x));
function f = Test(x,y)
f = sum(y.*sin(2*pi*(1:numel(y)).*x));
end
Walter Roberson
Walter Roberson 2023년 12월 5일
편집: Walter Roberson 2023년 12월 5일
x = 0:0.01:5
x = 1×501
0 0.0100 0.0200 0.0300 0.0400 0.0500 0.0600 0.0700 0.0800 0.0900 0.1000 0.1100 0.1200 0.1300 0.1400 0.1500 0.1600 0.1700 0.1800 0.1900 0.2000 0.2100 0.2200 0.2300 0.2400 0.2500 0.2600 0.2700 0.2800 0.2900
y = [2 -1 3 -2 4 -3 2 -4 1 -2]
y = 1×10
2 -1 3 -2 4 -3 2 -4 1 -2
z = Test(x,y)
z = 1×501
0 -1.5185 -2.3932 -2.1983 -0.8690 1.2779 3.6519 5.5843 6.5444 6.3013 4.9798 2.9977 0.9134 -0.7586 -1.6974 -1.8442 -1.3737 -0.5903 0.2024 0.8070 1.1756 1.3802 1.5365 1.7183 1.9072 2.0000 1.8690 1.4455 0.7835 0.0671
plot(x, z)
function f = Test(x,y)
f = sum(y(:).*sin(2*pi*(1:numel(y)).'.*x),1);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by