\( a_{2k} = \frac{2}{m} \displaystyle\sum_{i=1}^{m}f(\theta_{i})\cos(k\theta_{i}), k = 0, ..., n \)
en \( a_{2k-1} = \frac{2}{m} \displaystyle\sum_{i=1}^{m}f(\theta_{i})\sin(k\theta_{i}), k = 1, ..., n \)
Met benadering :\( \frac{a_{0}}{2} + \displaystyle\sum_{k = 1}^{n}(a_{2k}\cos(k\theta_{i}) + a_{2k-1}\sin(k\theta_{i})) \)
Mijn code ziet eruit als volgt :Code: Selecteer alles
function benadering = oefening8(m,n)
theta = linspace(0, 2*pi, m);
vierkant = f_theta(theta);
som_m = zeros(1,m);
som_n = zeros(1,n);
coef = zeros(1, 2*n + 1);
benadering = zeros(1, m);
for k = 0 : n
for i = 1 : m
som_m = som_m + vierkant(i)*cos(k*theta(i));
end
coef(2*k + 1) = 2/m * som_m(m);
end
som_m = 0;
for k = 1 : n
for i = 1 : m
som_m = som_m + vierkant(i)*sin(k*theta(i));
end
coef(2*k) = 2/m * som_m;
end
for i = 1 : 2 : m
for k = 1 : n
som_n = som_n + (coef(2*k + 1)*cos(k*theta(i)) + coef(2*k)*sin(k*theta(i)));
end
benadering(i) = coef(1)/2 + som_n(n);
end
subplot(1,2,1);
plot(vierkant.*cos(theta), vierkant.*sin(theta), 'r');
hold on;
plot(benadering.*cos(theta), benadering.*sin(theta));
axis([-3/2, 3/2, -3/2, 3/2]);
title('De discrete Fourierbenadering');
subplot(1,2,2);
fout = benadering - vierkant;
plot(theta, fout);
axis([0, 2*pi, -1/2, 1/2]);
title('De foutenkromme');
Ik krijg wel een grafiek wanneer ik deze functie uitvoer, maar (om het nog op z'n zachts uit te drukken) de grafiek ziet er niet uit zoals het zou moeten.
Hetzelfde moeten we doen met de continue benadering. Hiervoor hebben we dit gegeven :
\( a_{2k} = \frac{1}{\pi} \int_{0}^{2\pi} f(\theta)\cos(k\theta)d\theta, k = 0, ..., n \)
en \( a_{2k-1} = \frac{1}{\pi} \int_{0}^{2\pi} f(\theta)\sin(k\theta)d\theta, k = 1, ..., n \)
Met benadering :\( \frac{a_{0}}{2} + \displaystyle\sum_{k=1}^{n}(a_{2k}\cos(k\theta) + a_{2k-1}\sin(k\theta)) ^\)
Mijn code ziet eruit als volgt :Code: Selecteer alles
function benadering = oefening9(n)
theta = linspace(0, 2*pi, n + 1);
vierkant = f_theta(theta);
coef = zeros(1, 2*n + 1);
som = zeros(1, n + 1);
for k = 0 : n
if ((0 <= theta(k + 1)) && (theta(k + 1) < pi/4))
functie = @(x) sqrt(1 + (tan(x)).^2).*cos(k*x);
integraal = quad(functie, 0, pi/4);
elseif ((3*pi/4 <= theta(k + 1)) && (theta(k + 1) < 5*pi/4))
functie = @(x) sqrt(1 + (tan(x)).^2).*cos(k*x);
integraal = quad(functie, 3*pi/4, 5*pi/4);
elseif((7*pi/4 <= theta(k + 1)) && (theta(k + 1) <= 2*pi))
functie = @(x) sqrt(1 + (tan(x)).^2).*cos(k*x);
integraal = quad(functie, 7*pi/4, 2*pi);
elseif ((pi/4 <= theta(k + 1)) && (theta(k + 1) < 3*pi/4))
functie = @(x) sqrt(1 + (cot(x)).^2).*cos(k*x);
integraal = quad(functie, pi/4, 3*pi/4);
elseif ((5*pi/4 <= theta(k + 1)) && (theta(k + 1) < 7*pi/4))
functie = @(x) sqrt(1 + (cot(x)).^2).*cos(k*x);
integraal = quad(functie, 5*pi/4, 7*pi/4);
end
coef(2*k + 1) = (1/pi) * integraal;
end
for k = 1 : n + 1
if ((0 <= theta(k)) && (theta(k) < pi/4))
functie = @(x) sqrt(1 + (tan(x)).^2).*sin(k*x);
integraal = quad(functie, 0, pi/4);
elseif ((3*pi/4 <= theta(k)) && (theta(k) < 5*pi/4))
functie = @(x) sqrt(1 + (tan(x)).^2).*sin(k*x);
integraal = quad(functie, 3*pi/4, 5*pi/4);
elseif((7*pi/4 <= theta(k)) && (theta(k) <= 2*pi))
functie = @(x) sqrt(1 + (tan(x)).^2).*sin(k*x);
integraal = quad(functie, 7*pi/4, 2*pi);
elseif ((pi/4 <= theta(k)) && (theta(k) < 3*pi/4))
functie = @(x) sqrt(1 + (cot(x)).^2).*sin(k*x);
integraal = quad(functie, pi/4, 3*pi/4);
elseif ((5*pi/4 <= theta(k)) && (theta(k) < 7*pi/4))
functie = @(x) sqrt(1 + (cot(x)).^2).*sin(k*x);
integraal = quad(functie, 5*pi/4, 7*pi/4);
end
coef(2*k) = 1/pi * integraal;
end
for k = 1 : n
som = som + coef(2*k + 1)*cos(k*theta) + coef(2*k)*sin(k*theta);
end
benadering = coef(1)/2 + som(n);
subplot(1,2,1);
plot(vierkant.*cos(theta), vierkant.*sin(theta), 'r');
hold on;
plot(benadering.*cos(theta), benadering.*sin(theta));
axis([-3/2, 3/2, -3/2, 3/2]);
title('De continue Fourierbenadering');
subplot(1,2,2);
fout = benadering - vierkant;
plot(theta, fout);
axis([0, 2*pi, -2, 2]);
title('De foutenkromme');
Kan iemand mij op het juiste pad zetten ?