Function handle issue regarding Trig Fourier Series
3 次查看(过去 30 天)
显示 更早的评论
T = 5; % period
w0 = 2*pi/T;
F = @(s) [10.*(0<=s & s<1) + 0.*(1<=s & s<=5)];
N = 1000; % 2*N+1 is number of terms in the sum
B = N/T; % The highest frequency is B=N/T
dt = 1/(3*B); % the Nyquist rate is 1/(2*B)
a0 = (1/T)*integral(F,0,T); % mean value over the period
An = zeros(N); phase = zeros(N);
t = -6:dt:7;
sum = a0*ones(size(t));
for n=1:N
an = (2/T)*integral(F,0,T); % Fourier coefficients
bn = (2/T)*integral(F,0,T); % Fourier coefficients
sum = sum + an*cos(n*w0*t) + bn*sin(n*w0*t);
end
This is my code at the moment and im trying to calculate the an and bn values. The changed code below is what im trying to achieve but it will show up with an error.
an = (2/T)*integral(F*cos(n*w0*t),0,T); % Fourier coefficients
bn = (2/T)*integral(F*sin(n*w0*t),0,T); % Fourier coefficients
If i run it with this code, I get the error
"Operator '*' is not supported for operands of type 'function_handle'."
Any help would be appreciated.
0 个评论
回答(1 个)
Star Strider
2021-3-26
In this situaiton, it is necessary to evaluate the function, and also use the 'ArrayValued' name-value pair:
for n=1:N
an = (2/T)*integral(@(s)F(s).*cos(n.*w0.*t),0,T, 'ArrayValued',1); % Fourier coefficients
bn = (2/T)*integral(@(s)F(s).*sin(n.*w0.*t),0,T, 'ArrayValued',1); % Fourier coefficients
sum = sum + an.*cos(n.*w0.*t) + bn.*sin(n.*w0.*t);
end
It takes a few seconbds more to run, however it appears to produce an appropriate plot with:
figure
plot(t, sum)
grid
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bartlett 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!