How to make a plot of a Fourier series using the fourierSineComponents function?
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
Akshat
2023-9-30
编辑:Akshat
2023-9-30
Hi @Campbell
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
Read more about Preallocation: https://in.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
I hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!