I have a script called “FourierSeries.m”, which computes the Fourier Series of a square wave. A variable x ranges from –π to +π, with an interval of 0.01. Create a square wave variable by using the equation: 𝑆(𝑥) = (2 ∗ 𝐻(x))-1.

3 次查看(过去 30 天)
H(x) = 0 when x < 0; 1 when x > 0; 0.5 when x = 0
I current have the follwing in my script but I am not getting a plotted figure
Z = S(x) and y = H(x)
function [Z] = fourierseries(y)
y = [0,0.5,1];
x = input('input value for x');
x = -pi:0.01:pi;
Z = (2*y) - 1;
if x < 0
y = 0;
elseif x > 0
y = 1;
elseif x == 0
y = 0.5;
end
plot(x,Z);
end

回答(1 个)

Walter Roberson
Walter Roberson 2018-12-3
you input x but then you overwrite it with aa vector .Then you test the entire vector in if statements . remember that if and while have implicit all() so you are doing if all(x<0). None of the three tests is true because the conditions are not true for ALL the x values simultaneously .so you leave y unchanged . Then you ignore y so you might as well not have bothered .
so we get to the plot and we try to use the vector x against Z. Z was assigned y before y changed so Z is the vector of 3 specific values . But x is not the same size so the plot fails.
If you had not written the vector over the input x then x would be whatever was input and Z would be the three values . If x had been entered as aa scalar then it would be aa scalar at the plot. plot of aa scalar against aa vector of 3 values is permitted and treated as three point plots. But you do not indicate aa marker shape and the default is no marker so nothing would appear .
  2 个评论
Walter Roberson
Walter Roberson 2018-12-3
Change everything except the assignment of the vector to x.
Everything .
including the fact that you define a function when you need to define aa script . including the fact that you are told specifically how to calculate the square wave .

请先登录,再进行评论。

类别

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