For loop not acting as expected (Logical Error)

2 views (last 30 days)
I'm trying to run a for loop with the aim to integrate two variables and sum them up using built-in MATLAB Functions. The code is working error-free. However, the loop itself does not seem to go beyond the first value (i.e: it stops running after analyzing k = 0) (I checked the command window for that!). In other words, when I input the values of Ak, Bk, or x(t), it simply provides a value of zero (it even ignores the matrix setup before the loop entirely!). Any suggestions why the for loop terminates after the first cycle? (Equations attached below!)
Code:
clear all;
close all;
clc;
%Initial Variables
N = 8096; %Sum of additions to be done
T = 1e-3; %OFDM Symbol Period (TIME Domain!)
syms Ak; %First Data Sequence
syms Bk; %Second Data Sequence
t_new = T/8096; %Value in the range of 0-T to be constantly simulated on!
t = 0; %Updated value of t
syms x(t); %Complex Signal Function (Cosine Component)
sum_x = 0; %Sum of x (Complex Signal) values!
%Initial Equation Setup:
x(t) = zeros(1,N-1);
Ak = zeros(1,N-1);
Bk = zeros(1,N-1);
%Setting up equations:
for k = 0:1:10 %Set final value as N-1 in the end!
Ak = int((x(t)*cos(2*pi*k*t)),t,0,T);
Bk = int((x(t)*sin(2*pi*k*t)),t,0,T);
x(t) = sum(Ak*cos(2*pi*k*t) + Bk*sin(2*pi*k*t));
end
  4 Comments
per isakson
per isakson on 19 May 2020
"Can you attach an image of your equations?" asks for the equations in mathematical notation, not Matlab code.
Bahaa Soliman
Bahaa Soliman on 19 May 2020
I Attached the equations above, as requested.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 May 2020
syms x(t)
That says that x will be a function
x(t) = zeros(1,N-1);
That says that given any value, t, x is to return a vector of N-1 zeros.
Ak = int((x(t)*cos(2*pi*k*t)),t,0,T);
x(t) is that vector of zeros. Multiply it by anything and you get 0. Integral of 0 over a real range is 0. So Ak is a vector of N-1 zeros.
Bk = int((x(t)*sin(2*pi*k*t)),t,0,T);
For the same reasons, Bk is a vector of N-1 zeros.
x(t) = sum(Ak*cos(2*pi*k*t) + Bk*sin(2*pi*k*t));
zeros times something is zeros, zeros times something is zeros, add the two and you get zeros. sum() of all those zeros is scalar 0. Now you redefine x(t) as being a symbolic function that returns a scalar 0.
You loop, but the same logic for vectors of zeros tells you that all further iterations of the for k loop are going to end up with x(k) being scalar 0.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!