Why I'm I getting results in terms of t for an and bn even though it is an integration with specific limits?

%Excercise 4
syms t
syms N
%prompt = "Please enter a function rules for variable segments of the function f: ";
%'Enter 2D array with [] around it and commas between the columns and semicolon at end of each row '
f = input('Please enter function rules for variable segments of the function f:')
flim= input('Please enter real numbers to define the limits of the segments of f:')
N= input('Please enter an intiger to specifie the max order of Fourier coefficients of f:')
%flimFirst= flim(1);
%filmEnd= flim(end);
%a0= 2/(flim(end)-flim(1))*int(f(1),flim(1),flim(2));
%a0= sprintf('%.4f', a0)
%a02= 2/(flim(end)-flim(1))*int(f(2),flim(2),flim(3));
%a02= sprintf('%.4f', a02)
T = flim(end)-flim(1);
w0 = 2*pi/T;
a0= (2/T)*int(f(length(f)),t,flim(length(flim)-1),flim(length(flim)));
sprintf('%.4f', a0)
a02= (2/T)*int(f(1),t,flim(1),flim(2));
sprintf('%.4f', a02)
a03= (2/T)*int(f(2),t,flim(2),flim(3));
sprintf('%.4f', a02)
for i = 1:N
for k= 2:length(flim)
an= (2/T)*int((f(k-1))*cos(N*w0*t)),t,flim(k-1),flim(k);
an(i)=an;
bn= (2/T)*int((f(k-1))*sin(N*w0*t)),t,flim(k-1),flim(k);
bn(i)=bn;
fs= (a0/2)+(an*cos(w0*N*t))+(bn*sin(w0*N*t));
end
end
an
%sprintf('%.4f', an);
bn
%sprintf('%.4f', bn);
fs
%sprintf('%.4f', fs);

3 Comments

Original question by Stefanos retrieved from Google Cache:
"Why I'm I getting results in terms of t for an and bn even though it is an integration with specific limits?"
%Excercise 4
Theme
syms t
syms N
%prompt = "Please enter a function rules for variable segments of the function f: ";
%'Enter 2D array with [] around it and commas between the columns and semicolon at end of each row '
f = input('Please enter function rules for variable segments of the function f:')
flim= input('Please enter real numbers to define the limits of the segments of f:')
N= input('Please enter an intiger to specifie the max order of Fourier coefficients of f:')
%flimFirst= flim(1);
%filmEnd= flim(end);
%a0= 2/(flim(end)-flim(1))*int(f(1),flim(1),flim(2));
%a0= sprintf('%.4f', a0)
%a02= 2/(flim(end)-flim(1))*int(f(2),flim(2),flim(3));
%a02= sprintf('%.4f', a02)
T = flim(end)-flim(1);
w0 = 2*pi/T;
a0= (2/T)*int(f(length(f)),t,flim(length(flim)-1),flim(length(flim)));
sprintf('%.4f', a0)
a02= (2/T)*int(f(1),t,flim(1),flim(2));
sprintf('%.4f', a02)
a03= (2/T)*int(f(2),t,flim(2),flim(3));
sprintf('%.4f', a02)
for i = 1:N
for k= 2:length(flim)
an= (2/T)*int((f(k-1))*cos(N*w0*t)),t,flim(k-1),flim(k);
an(i)=an;
bn= (2/T)*int((f(k-1))*sin(N*w0*t)),t,flim(k-1),flim(k);
bn(i)=bn;
fs= (a0/2)+(an*cos(w0*N*t))+(bn*sin(w0*N*t));
end
end
an
%sprintf('%.4f', an);
bn
%sprintf('%.4f', bn);
fs
%sprintf('%.4f', fs);

Sign in to comment.

Answers (1)

syms t
f = [pi+t,pi-t];
flim= [-pi,0,pi];
N = 5;
T = flim(end)-flim(1);
w0 = 2*pi/T;
fs = 0.0;
for i = 0:N
for k= 2:length(flim)
an= (2/T)*int(f(k-1)*cos(i*w0*t),t,flim(k-1),flim(k));
bn= (2/T)*int(f(k-1)*sin(i*w0*t),t,flim(k-1),flim(k));
fs= fs + an*cos(w0*i*t)+bn*sin(w0*i*t);
end
if i==0
fs = fs/2;
end
end
fs = matlabFunction(fs);
t = -pi:0.01:pi;
plot(t,fs(t))

15 Comments

Then start anew from your code and add the necessary changes.
if i==0
fs = fs/2;
end
a0 is calculated first (for i=0). The Fourier series starts with a0/2 ...
for k= 2:length(flim)
an= (2/T)*int(f(k-1)*cos(i*w0*t),t,flim(k-1),flim(k));
an(i)=an;
bn= (2/T)*int(f(k-1)*sin(i*w0*t),t,flim(k-1),flim(k));
bn(i)=bn;
fs= fs + an*cos(w0*i*t)+bn*sin(w0*i*t);
end
if i==0
fs = fs/2;
end
end
Method 1:
an
bn
fs
Method 2:
for j= 0:N
disp(an(j))
disp(bn(j))
disp(fs(j))
end
I did the following two methods to print the arrays pf an and bn and fs. Why does non of the two work?
Array indices must be positive integers or logical values.
[Error using method 1]
Error in sym/privsubsasgn (line 1311)
L_tilde2 = builtin('subsasgn',L_tilde,struct('type','()','subs',{varargin}),R_tilde);
Error in indexing (line 1142)
C = privsubsasgn(L,R,inds{:});
Error in Test2 (line 11)
an(i)=an;
It should be obvious that the array you save the results in cannot have the same name as the temporary variable which contains the value of the integral.
And array indices start with 1 ; so you will have to shift i to i+1 when saving an and bn.
Using that code they are represented ans how can I represent them as one array with all the answers?(One array for an with all the ans and another for bn)?
disp('an=')
for i= 1:N
disp(ann(i))
end
disp('bn=')
for i= 1:N
disp(bnn(i))
end
What is your actual code for the loop where you want to save the Fourier coefficients after the changes you made ?
for i = 0:N
for k= 2:length(flim)
an= (2/T)*int(f(k-1)*cos(i*w0*t),t,flim(k-1),flim(k));
ann(i+1)=an;
bn= (2/T)*int(f(k-1)*sin(i*w0*t),t,flim(k-1),flim(k));
bnn(i+1)=bn;
fs= fs + an*cos(w0*i*t)+bn*sin(w0*i*t);
end
if i==0
fs = fs/2;
end
for i= 0:N
ann(i+1)
bnn(i+1)
end
fs
The ann(i+1) and bnn(i+1) are the sum of all an and bn computed for the different intervals in the k-loop (as done for fs).
And finally, ann(1) has to be divided by 2 (as done for fs).
How can I print all the values of the coefficients an and bn of the fourier series with the value of N varying from 1-5 and represent the results in an array?
You mean varying from 0-5 ?
By summing the an and bn for each i in the inner the k-loop in ann(i+1) and bnn(i+1), dividing ann(1) by 2 in the subsequent if-statement and displaying the arrays ann and bnn after the loop using the disp command.
6 for ann (a0/2,a1,...,a5), 5 for bnn (b1,...,b5).
You might also save 0 in the first position of bnn to generate arrays of equal length.
ok. You know best what your homework should look like.
I used this code to display them and it works for an and bn but a0 isnt displayed despite the if statement
for i= 1:N
disp('an=')
disp(ann(i+1))
end
if i==0
disp('an0=')
ann(i+1)= an/2;
disp(ann(i+1))
end
for i= 1:N
disp('bn=')
disp(bnn(i+1))
end
When you have a for loop with : operator, then after the loop, the loop control variable is assigned one of the following:
  • [] (empty double) if no increment is specified and the terminal value (right side of : operator) is less than the left side of the colon operator
  • [] (empty double) if positive increment is specified and the terminal value (far right side of : operator) is less than the left side of the colon operator
  • [] (empty double) if negative increment is specified and the terminal value (far right side of : operator) is greater than the left side of the colon operator
  • last value assigned by the colon operator if none of the operations inside the loop assign to the loop control variable or if the loo control variable is not assigned to in the last iteration of the loop
  • last value assigned to the loop control variable if the loop control variable is assigned to in the last iteration of the loop
Or to state more compactly: [] if the loop body was never entered, and otherwise the last value assigned to the loop control variable.
So, with your for i=1:N provided that N >= 1, the value would be the last value assigned to the loop control variable. In that loop, it would be floor(N) .
You then test i==0 after the loop. Is that ever a possibility? Assuming that the disp() calls do not somehow assign anything to i then after the first loop, i is going to be empty (which is not 0) if the loop was never executed at all, and i is going to be a minimum of 1 if N>=1. So i will never be 0 after that first loop.
Exception: if you overwrote disp as a function that does an assignin('caller', 'i', 0) then Yes, in theory i could be 0 after the loop. That would be rather poor programming practice though.
Summarizing Walters's response:
You should improve your MATLAB skills and invest two hours of your time to visit MATLAB's online course for free:
Why should we remove our comments here, as you requested in your flag ?

Sign in to comment.

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Asked:

on 13 Dec 2022

Edited:

on 9 Jan 2023

Community Treasure Hunt

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

Start Hunting!