create a plot of y = x^2 in matlab

To create a plot of y = x^2 in MATLAB, you can follow the steps below:

  1. Create a vector for x values using the linspace() function:
main.m
x = linspace(-10, 10, 200);
28 chars
2 lines
  1. Calculate the corresponding y values using the function y = x.^2:
main.m
y = x.^2;
10 chars
2 lines
  1. Plot the result using the plot() function:
main.m
plot(x, y);
12 chars
2 lines
  1. Add any desired plot decorations, such as axis labels and a title:
main.m
xlabel('x');
ylabel('y');
title('y = x^2');
44 chars
4 lines

The complete code snippet would look like this:

main.m
x = linspace(-10, 10, 200);
y = x.^2;
plot(x, y);
xlabel('x');
ylabel('y');
title('y = x^2');
94 chars
7 lines

This will generate a plot of y = x^2 with x values ranging from -10 to 10 and 200 data points evenly spaced along that range.

gistlibby LogSnag