Question: I ' m plotting a fourier series graph. Below are my matlab codings. But they seem so stupid ( hoewever I still achive the things

I'm plotting a fourier series graph. Below are my matlab codings. But they seem so stupid (hoewever I still achive the things I want, Just the code is so stupid that it takes 3 for loop and 3 if, how can i modify it so that it only take 1 loop and 3 if, to make it more efficient, while producing the same result (my graph)?
%BEJ20203 Pre-project Task 2
T0=1;
tau =0.5;
t =-10:0.01:10;
figure;
% Define the function for coefficient x_n
%Use @(n) because later I can sub n with something using x_n(n)
Coefficient_of_individual_term = @(n)(tau / T0)* sinc(n * tau / T0);
%If don't understand, gemini answer me better than the matlab documentation
%Method 2, directly open a new matlab to test to see what I get,
% then I %would understand
Final_x_t = zeros(size(t));
%n =+-3
for n =-20:20; %Later ask, how to implement using 1 for, and 3 if?
%because this loop would sub in n =0,
% this would cause incorrectness!
% i want it to skip when n =0
%So, only if when the n is not 0, execute the following!
if abs(n)<=5
Individual_term_of_x_t =(tau / T0)* sinc(n * tau / T0)* exp(1j *2* pi * n * t / T0);
Final_x_t = Final_x_t + Individual_term_of_x_t;
%The plot function, put inside this "if", or it would show nothing
%subplot put first, before the plot function, or it would bug! (Found
%by trial and error, and deduction, just now the title won't show,
%observation!
subplot(4,1,1)
plot(t, real(Final_x_t));
title('x(t) for n =0 to 5');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
end %end the if statement
end %end the for loop
%n =+-10
%Reset again the x_t, put here, or the following one, would produce
%incorrect result, because the Final_x_t would have value already
Final_x_t = zeros(size(t));
for n =-20:20;
if abs(n)<=10%If don't want calculate n =0, then put if n~=0
Individual_term_of_x_t =(tau / T0)* sinc(n * tau / T0)* exp(1j *2* pi * n * t / T0);
Final_x_t = Final_x_t + Individual_term_of_x_t;
subplot(4,1,2)
plot(t, real(Final_x_t));
title('x(t) for n =0 to 10');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
end
end
%n =+-20
Final_x_t = zeros(size(t));
for n =-20:20;
if abs(n)<=20
Individual_term_of_x_t = Coefficient_of_individual_term(n)* exp(1j *2* pi * n * t / T0);
Final_x_t = Final_x_t + Individual_term_of_x_t;
subplot(4,1,3)
plot(t, real(Final_x_t));
title('x(t) for n =0 to 20');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
end
end
% How to plot the coefficients x_n on the fourth row?
subplot(4,1,4);
%How to declare n =-20,-19,...+20? Use mass declaration, %mass declaration, of n values,
% and very fast declaration, from n =-20, to 20, this would become an array
n_values =-20:20;
%Apply the function, Coefficient_of_individual_term, on every elements of the n_values, which is a array
%Or in other words, sub in all the n_values=-20 : 20, into the function of Coefficient_of_individual_term
%It's mass substitution of element array into the function , so use arrayfun, instead of sub
%But why? And the outcome is what? although this is intermediate, still need the outcome!
x_n_values = arrayfun(Coefficient_of_individual_term, n_values);
%How to do discrete plotting? Use stemplot, only got bubble,
stem(n_values, abs(x_n_values)); %Do they need all to be absolute value according to theory?
title('Magnitude of Coefficients X_n');
xlabel('n');
ylabel('|X_n|');
grid on;
%%Actually below don't need!
%%Apply if-else for conditional coefficient plotting
% for idx =1:length(n_values)
% n = n_values(idx);
% if abs(n)<=20
%% Only plot coefficients for n within range
% coeff_value = abs(Coefficient_of_individual_term(n));
% else
% coeff_value =0; % Default or out-of-range handling
% end
% end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!