I need to plot the same graphic of the image, but without any loop.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Luccas S.
am 18 Apr. 2021
Kommentiert: Walter Roberson
am 18 Apr. 2021
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/587421/image.jpeg)
![Equation for f(x)](https://www.mathworks.com/matlabcentral/answers/uploaded_files/587446/Equation%20for%20f(x).jpeg)
With loop i get this:
clear
clc
x = -5*pi:pi/100:5*pi;
k = 0;
y = zeros(size(x));
for k = 0:100
k = k + 1;
y = y + 2*( ( (-(-1).^k) / k ) .* sin(x*k) );
end
plot(x, y)
ylabel('f(X)')
xlabel('X')
grid
I tried to do this without loop but i getting errors.
clear
clc
x = -5*pi:pi/100:5*pi;
k = 0:1:100;
y = zeros(size(x));
% How to variable k ?
y = y + 2.*( ( (-(-1).^k) / k ) .* sin(x*k) );
sum(x)
sum(y)
plot(x,y)
2 Kommentare
Akzeptierte Antwort
Walter Roberson
am 18 Apr. 2021
x = -5*pi:pi/100:5*pi;
k = (1:1:100).'; %not 0!
y = sum(2.*( ( (-(-1).^k)./ k ) .* sin(x.*k) ));
plot(x,y)
2 Kommentare
Walter Roberson
am 18 Apr. 2021
The above code works in R2016b and later.
Octave is not a programming language that I know.
xvals = -5*pi:pi/100:5*pi;
kvals = (1:1:100).'; %not 0!
[x, k] = meshgrid(xvals, kvals);
y = sum(2.*( ( (-(-1).^k)./ k ) .* sin(x.*k) ));
plot(x,y)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graphics Performance 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!