For loop not acting as expected (Logical Error)
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Bahaa Soliman
am 19 Mai 2020
Kommentiert: Bahaa Soliman
am 20 Mai 2020
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 Kommentare
per isakson
am 19 Mai 2020
"Can you attach an image of your equations?" asks for the equations in mathematical notation, not Matlab code.
Akzeptierte Antwort
Walter Roberson
am 19 Mai 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!