error in fourie script

2 vues (au cours des 30 derniers jours)
f4r3in
f4r3in le 15 Fév 2017
Réponse apportée : VBBV le 17 Sep 2024
hi I use MatLab 2016a and write this script but I receive this error. how should I fix it:
g=@(x)1.*(x>0)+(-1).*(x<0)
a_0=(1/pi)*quad(g,-pi,pi);
for n=1:10
a_n(n)=(1/pi)*quad(g*cos(n*x),-pi,pi);
b_n(n)=(1/pi)*quad(g*sin(n*x),-pi,pi);
end
a_n
b_n
f=a_0;
for n=1:10
f=f+a_n(n)*cos(n*x)+b_n(n)*sin(n*x);
end
ezplot(x,f)

Réponses (1)

VBBV
VBBV le 17 Sep 2024
g=@(x) 1.*(x>0)+(-1).*(x<0);
a_0=(1/pi)*quad(g,-pi,pi)
a_0 = 0
x = linspace(-pi,pi,200);
for k = 1:numel(x)
for n=1:10
a_n(n,k)=(1/pi)*(g(x(k))*cos(n*x(k)));
b_n(n,k)=(1/pi)*(g(x(k))*sin(n*x(k)));
end
A_n(k) = sum(a_n(:,k));
B_n(k) = sum(b_n(:,k));
f(k)=a_0+A_n(k).*cos(n.*x(k))+B_n(k).*sin(n.*x(k));
end
plot(x,f)

Community Treasure Hunt

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

Start Hunting!

Translated by