plot a sine wave in matlab

To plot a sine wave in matlab, you can use the plot function. The plot function takes in two arrays, one for the x-values and one for the y-values. In this case, the x-values will represent the domain of the sine wave, and the y-values will represent the values of the sine wave.

You can generate an array of x-values using the linspace function. linspace takes in the starting value, ending value, and number of points to generate between those values. For example, to generate an array of 100 x-values between 0 and 2pi (the period of a sine wave):

main.m
x = linspace(0, 2*pi, 100);
28 chars
2 lines

Then you can calculate the y-values of the sine wave using the sin function:

main.m
y = sin(x);
12 chars
2 lines

Finally, you can plot the sine wave using the plot function:

main.m
plot(x, y);
12 chars
2 lines

This will generate a simple plot of a sine wave. You can customize the plot by adding axis labels, changing the color or line style, and adding a title. For example:

main.m
plot(x, y, 'r--');
xlabel('x');
ylabel('sin(x)');
title('Sine Wave');
70 chars
5 lines

This will plot a red, dashed sine wave with x-axis label "x", y-axis label "sin(x)", and title "Sine Wave".

related categories

gistlibby LogSnag