For loop not acting as expected (Logical Error)

조회 수: 2 (최근 30일)
Bahaa Soliman
Bahaa Soliman 2020년 5월 19일
댓글: Bahaa Soliman 2020년 5월 20일
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
per isakson
per isakson 2020년 5월 19일
"Can you attach an image of your equations?" asks for the equations in mathematical notation, not Matlab code.
Bahaa Soliman
Bahaa Soliman 2020년 5월 19일
I Attached the equations above, as requested.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 19일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by