MATLAB MATLAB Plot of a periodic function

AI Thread Summary
The discussion revolves around plotting a mathematical function defined as x(n) = 1 + sin((2*pi)/N)*n + 3*cos((2*pi)/N)*n + cos(((4*pi)/N)*n + pi/2). Users are experiencing issues with the function displaying an increasing slope rather than a periodic behavior, even when setting the period N to values like 8 or 4. In contrast, when the function is expressed using Euler's expansion, it appears periodic, raising questions about the discrepancies between the two representations.Participants suggest that instead of using a loop to compute values for n, it is more efficient to create an array for n and compute the corresponding x(n) values in one go, which simplifies plotting. Additionally, there is a recommendation to use element-wise multiplication by including a dot (.) before multiplication operators to avoid matrix multiplication errors.
ColdStart
Messages
18
Reaction score
0
letsa say my function is:
x(n) = 1 + sin((2*pi)/N)*n + 3*cos((2*pi)/N)*n + cos( ((4*pi)/N)*n + pi/2);

when i plot it inside a loop, with a loop variable being n, i have something llike increasing slope... even when i set a period N to be 8,4 or whateva..

on the other side... when i plot the same function expanded using euler's expansion:
x(n) = 1 + (1/(2*1i))*(exp(1i*2*pi*n/N) - exp(-1i*2*pi*n/N)) + 1.5*(exp(1i*2*pi*n/N) + exp(-1i*2*pi*n/N)) + 0.5*(exp(1i*(4*pi*n/N + pi/2)) + exp(-1i*(4*pi*n/N + pi/2)));

i do get a function which looks periodic.. that is a little bit weird, or am i missing something?

p.s. and the second function is obtained from the Fourier ak Coofficients..
 
Physics news on Phys.org
Why use a loop. Make an array of all n-variables and then a array of all x(n) solutions for then to plot.

ie. n=0:0.01:2*pi;
x=...;
plot(n,x)

Also you might want to put a .(period) in front of all mutiplication signs, or else it does a matrix multiplication.
ie. >> x=1+sin(2.*pi*n)+3.*cos(2.*pi.*n)+cos((4.*pi.*n)+ pi/2);
 
thank you! yeah! i have no idea sometimes its confusing when in the books they put period outside of parenthesis!.. that make sense now!
 

Similar threads

Replies
10
Views
3K
Replies
2
Views
2K
Replies
3
Views
2K
Replies
1
Views
6K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
4
Views
6K
Replies
14
Views
6K
Back
Top