For loop not acting as expected (Logical Error)

1 回表示 (過去 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.
  1 件のコメント
Bahaa Soliman
Bahaa Soliman 2020 年 5 月 20 日
Thank you Walter :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by