Azzera filtri
Azzera filtri

Indexing error while using symsum inside a function.

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
Chunru
Chunru il 5 Dic 2023
What is the desired output? A value or a symbolic expression?

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 5 Dic 2023
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 Commenti
Bear Foot
Bear Foot il 5 Dic 2023
Spostato: Walter Roberson il 5 Dic 2023
@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 il 5 Dic 2023
Modificato: Walter Roberson il 5 Dic 2023
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

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by