plot sin(x) in matlab

To plot the sine function in MATLAB, you can use the following code:

main.m
% Generate x values from -2*pi to 2*pi
x = -2*pi:0.01:2*pi;

% Calculate y values for sin(x)
y = sin(x);

% Plot the sine function
plot(x, y);

% Add title and labels
title('Plot of sin(x)');
xlabel('x');
ylabel('y');

% Display the plot
grid on;
247 chars
17 lines

In this code, we first generate a range of x values from -2pi to 2pi using the colon operator. Then, we calculate the corresponding y values for the sine function using the sin() function in MATLAB. Finally, we plot the sine function using the plot() function and add a title, xlabel, and ylabel to the plot. The grid on command adds a grid to the plot for better visualization.

related categories

gistlibby LogSnag