I need to plot the same graphic of the image, but without any loop.

1 回表示 (過去 30 日間)
Luccas S.
Luccas S. 2021 年 4 月 18 日
コメント済み: Walter Roberson 2021 年 4 月 18 日
Equation for f(x)
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 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 4 月 18 日
I didnot find any output using your loop code also.
Luccas S.
Luccas S. 2021 年 4 月 18 日
編集済み: Luccas S. 2021 年 4 月 18 日
I ended up not transcribing k = k +1 for publication, now i have changed.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 4 月 18 日
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 件のコメント
Luccas S.
Luccas S. 2021 年 4 月 18 日
Right, I am running the code in octave but it is not working in matlab. (Matrix dimensions must agree.)
Walter Roberson
Walter Roberson 2021 年 4 月 18 日
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)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by