Azzera filtri
Azzera filtri

Function handle issue regarding Trig Fourier Series

3 visualizzazioni (ultimi 30 giorni)
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.

Risposte (1)

Star Strider
Star Strider il 26 Mar 2021
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
.

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by