Code runs very slow

조회 수: 1 (최근 30일)
Tu Nguyen
Tu Nguyen 2022년 3월 4일
댓글: Tu Nguyen 2022년 3월 4일
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
  댓글 수: 1
Tu Nguyen
Tu Nguyen 2022년 3월 4일
Thank you so much

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by