![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/759341/image.png)
How do I evaluate a function that contain an array index inside a sum?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Mike
am 6 Okt. 2021
Bearbeitet: Walter Roberson
am 19 Okt. 2021
I want to evaluate this function and save the result in an array in MATLAB
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/759241/image.png)
Where A and B are arrays, and N and P are constants. For example A = [1,2,3,4], B = [1,2,3], P = 6, and N = 4.
Also, I want to save the output of f(x) and its corresponding x value.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 6 Okt. 2021
Bearbeitet: Walter Roberson
am 19 Okt. 2021
Calculate definite vector values and sum()
k = (1 : N-1) .';
f = @(x) A0/2 + sum(A(k) .* cos(2*pi.*k.*x./P) + B(k) .* sin(2*pi*.k.*x./P),1) + A(N).*cos(2*pi.*N.*x/P)/2
For example A = [1,2,3,4], B = [1,2,3], P = 6, and N = 12
N cannot be greater than one less than length(A) or length(B).
Also, unless you store
in a separate variable like I used here, then you would need to make adjustments to the code to deal with the question of whether you are using 0-based indexing or 1-based indexing.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/759341/image.png)
Note: this version of the code is vectorized, provided that x is a row vector.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!