Code runs very slow

2 次查看(过去 30 天)
clc;
close all;
clear all;
%Define domain
syms x
L = pi;
N = 1024;
dx = 2*L/(N-1);
%Define rectangular function
f = rectangularPulse(-1,1,x);
%Compute Fourier series
CC = jet(20);
F0 = sum(f.*ones(size(x)))*dx/pi;
fFS = F0/2;
for k = 1:20
A(k) = sum(f.*cos(pi*k*x/L))*dx/pi;
B(k) = sum(f.*sin(pi*k*x/L))*dx/pi;
fFS = fFS + 2*A(k)*cos(k*pi*x/L) - 2*B(k)*sin(k*pi*x/L);
fplot(x,fFS,'-','Color', CC(k,:),'LineWidth',2);
hold on;
end
Hi all, anyone helps me fix this code please, it runs so slow to see the plot

采纳的回答

Walter Roberson
Walter Roberson 2022-3-4
L = pi;
N = 1024;
x = linspace(-5,5,N);
dx = 2*L/(N-1);
%Define rectangular function
f = rectangularPulse(-1,1,x);
%Compute Fourier series
CC = jet(20);
F0 = sum(f.*ones(size(x)))*dx/pi;
fFS = F0/2;
for k = 1:20
A = sum(f.*cos(pi*k*x/L))*dx/pi;
B = sum(f.*sin(pi*k*x/L))*dx/pi;
fFS = fFS + 2*A*cos(k*pi*x/L) - 2*B*sin(k*pi*x/L);
plot(x,fFS,'-','Color', CC(k,:),'LineWidth',2);
hold on;
end

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by