Code runs very slow

1 view (last 30 days)
Tu Nguyen
Tu Nguyen on 4 Mar 2022
Commented: Tu Nguyen on 4 Mar 2022
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

Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2022
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

More Answers (0)

Categories

Find more on Mathematics and Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!