For loop not acting as expected (Logical Error)
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Bahaa Soliman
el 19 de Mayo de 2020
Comentada: Bahaa Soliman
el 20 de Mayo de 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 comentarios
per isakson
el 19 de Mayo de 2020
"Can you attach an image of your equations?" asks for the equations in mathematical notation, not Matlab code.
Respuesta aceptada
Walter Roberson
el 19 de Mayo de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!