Can you check my error code?

1 次查看(过去 30 天)
SEANGHAI THA
SEANGHAI THA 2019-12-6
Hello! Can you correct my code? Because I have some errors on my code.
%% Starting codes
x = linspace (-2,4,101);
span = [0 1 2];
for i = -1: 2
h1 = plot (x (x> (span (1) + i * 2) & x <(span (2) + i * 2)), ...
zeros (size (x (x> (span (1) + i * 2) & x <(span (2) + i * 2)))));
hold on;
h2 = plot (x (x> (span (2) + i * 2) & x <(span (3) + i * 2)), ...
ones (size (x (x> (span (2) + i * 2) & x <(span (3) + i * 2)))));
set (h1, 'linewidth', 3, 'color', 'b');
set (h2, 'linewidth', 3, 'color', 'b');
end
ylim ([-1 2]);
grid on;
title ('f (x) vs Fourier Series', 'front size', 16);
n = 0: 100;
for i = 1: length (x)
f (i) = 0.5-sum (2 / pi. * sin ((2 * n + 1) * pi * x (i)) ./ (2 * n + 1));
set (f (i), 'linewidth', 3, 'color', 'b');
end
plot (x, f, 'r');
hold off
And I got errors like the following:

回答(1 个)

Ridwan Alam
Ridwan Alam 2019-12-6
编辑:Ridwan Alam 2019-12-6
x = linspace (-2,4,101);
span = [0 1 2];
for i = -1: 2
h1 = plot (x (x> (span (1) + i * 2) & x <(span (2) + i * 2)), ...
zeros (size (x (x> (span (1) + i * 2) & x <(span (2) + i * 2)))));
hold on;
h2 = plot (x (x> (span (2) + i * 2) & x <(span (3) + i * 2)), ...
ones (size (x (x> (span (2) + i * 2) & x <(span (3) + i * 2)))));
set (h1, 'linewidth', 3, 'color', 'b');
set (h2, 'linewidth', 3, 'color', 'b');
end
ylim ([-1 2]);
grid on;
title ('f (x) vs Fourier Series', 'FontSize', 16); % not 'front size'
n = 0: 100;
for i = 1: length (x)
f (i) = 0.5-sum (2 / pi. * sin ((2 * n + 1) * pi * x (i)) ./ (2 * n + 1));
set (f (i), 'linewidth', 3, 'color', 'b');
end
plot (x, f, 'r');hold off;
  2 个评论
SEANGHAI THA
SEANGHAI THA 2019-12-6
Oh! Many thank for your help!
Ridwan Alam
Ridwan Alam 2019-12-6
Sure. Glad to help. Please accept the response as an answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by