필터 지우기
필터 지우기

Function handle issue regarding Trig Fourier Series

조회 수: 3 (최근 30일)
Callum Stewart
Callum Stewart 2021년 3월 26일
답변: Star Strider 2021년 3월 26일
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.

답변 (1개)

Star Strider
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
.

카테고리

Help CenterFile Exchange에서 Bartlett에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by