How to make a plot of a Fourier series using the fourierSineComponents function?

1 vue (au cours des 30 derniers jours)
Campbell
Campbell le 28 Sep 2023
Modifié(e) : Akshat le 30 Sep 2023
I'm trying to plot a fourier sine series, and I am getting an error saying that my function does not have enough inputs, and then an error at line 14 for m=1:nl. How should I correct this problem?
function fourierSineComponents(nl)
for n = 1:300
A(n) = -3*pi + pi/50*(n);
end
for n = 1:300
F(n)= pi/2;
end
plot(A,F)
for m=1:nl
k=(m*2)-1;
Fadd=(4/(pi))*(1/k.^2)*cos(k.*A);
plot(A,-Fadd);
F=F-Fadd;
end
plot(A, F)

Réponses (1)

Akshat
Akshat le 30 Sep 2023
Modifié(e) : Akshat le 30 Sep 2023
I understand that you are encountering an error while plotting Fourier Sine Series. I investigated the attached code and found that error is realted to the absence of 'nl' input argument while calling the 'fourierSineComponents' function. One of the workarounds to resolve the error is adding a call to the function 'fourierSineComponents' above the function definition in the MATLAB script. For example, you can add the following line at the top of your script.
fourierSineComponents(1) % customize the value as per needs
function fourierSineComponents(nl)
Furthermore, an alternative workaround involves invoking the script while providing the 'nl' argument. For instance, if your script is named 'script.m', then you can execute the code from the command line as follows.
>> script(1) % customize the value as per needs
Selecting any of the above options would be sufficient to fix the issue. Moreover, while reviewing the code, I observed warnings related to the assignment of vectors 'A' and 'F'. These warnings are raised to draw attention to the fact that the sizes of these vectors are being modified in each iteration. This could potentially decrease performance and speed of the code. Hence, to enhance performance, particularly in terms of speed, I highly advise preallocating these vectors. Here is a code snippet for your reference that does the same.
A = zeros(1,300); % prealolocating A and F for enhanced performance
F = zeros(1,300);
for n = 1:300
A(n) = -3*pi + pi/50*(n);
end
for n = 1:300
F(n)= pi/2;
end
I hope this helps.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by