MATLAB Plot of a periodic function

In summary, the conversation discusses a function and its different representations. The first representation, using a loop and a variable n, shows an increasing slope regardless of the period N. However, the second representation, using Euler's expansion, shows a periodic function. The use of a loop and an array in plotting the function is also mentioned, along with the suggestion to use a period before multiplication signs to avoid matrix multiplication.
  • #1
ColdStart
19
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
  • #2
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);
 
  • #3
thank you! yeah! i have no idea sometimes its confusing when in the books they put period outside of parenthesis!.. that make sense now!
 

1. How do I plot a periodic function in MATLAB?

To plot a periodic function in MATLAB, you can use the built-in function plot(). First, define the function using an anonymous function, specifying the period of the function. Then, use the plot() function with an appropriate range of values for the independent variable. For example:
f = @(x) cos(x); % define a cosine function
x = linspace(0, 2*pi, 100); % create an array of 100 values from 0 to 2*pi
plot(x, f(x)); % plot the function

2. How do I change the appearance of a periodic plot in MATLAB?

To change the appearance of a periodic plot in MATLAB, you can use the various formatting options available in the plot() function. For example, you can change the color, line style, and markers using the 'Color', 'LineStyle', and 'Marker' name-value pairs. You can also add a title, axis labels, and a legend using the title(), xlabel(), ylabel(), and legend() functions.

3. How do I plot multiple periodic functions on the same graph in MATLAB?

To plot multiple periodic functions on the same graph in MATLAB, you can use the hold on command. This command allows you to add multiple plots to the same figure without overwriting the previous ones. For example:
f1 = @(x) sin(x); % define a sine function
f2 = @(x) cos(x); % define a cosine function
x = linspace(0, 2*pi, 100); % create an array of 100 values from 0 to 2*pi
hold on;
plot(x, f1(x)); % plot the first function
plot(x, f2(x)); % plot the second function
hold off;
legend('sin(x)', 'cos(x)'); % add a legend

4. How do I add grid lines to a periodic plot in MATLAB?

To add grid lines to a periodic plot in MATLAB, you can use the grid on command. This will add grid lines to the current axes. You can also customize the grid using the grid() function, specifying the line style, color, and spacing of the grid lines. For example:
plot(x, f(x)); % plot the function
grid on; % add grid lines
grid('LineStyle', '--', 'Color', 'k', 'LineWidth', 0.5); % customize the grid

5. How do I save a periodic plot in MATLAB as an image file?

To save a periodic plot in MATLAB as an image file, you can use the saveas() function. This function allows you to specify the file name and format of the image. For example:
plot(x, f(x)); % plot the function
saveas(gcf, 'plot.png', 'png'); % save the plot as a PNG file

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
2
Views
527
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
897
Back
Top