Plotting y=x Function in Matlab: Tips for Beginners

In summary, to plot y=x in Matlab, use the "plot" function and specify the values of x and y. You can also change the color, style, and axis limits of the plot, and add labels and a title using various functions. Additionally, you can save your plot as an image file using the "saveas" function.
  • #1
djdato
5
0
I am new to Matlab and i need to plot a function. However, using y=x as an example it always comes up saying that x is not defined. How do i plot the function without specifying a specific value for x? Graphing y=x is not my real problem. I am used to using TI-83/4 graphics calculators and typing a function like y=x^2 and just graphing it. But i can't do it on MATLAB as it always needs me to define x.

Regards,
djdato
 
Last edited:
Physics news on Phys.org
  • #2
My advice is: RTM
 
  • #3


Hi djdato,

Thank you for reaching out for help with plotting the y=x function in Matlab. I can understand how it can be confusing for beginners to plot a function without specifying a value for x.

In Matlab, all variables need to be defined before they can be used in a function or calculation. This is different from TI-83/4 graphics calculators where you can directly type in a function without defining the variable first.

To plot the y=x function in Matlab, you can define a range of values for x using the "linspace" function and then use that range to plot the function. For example, you can use the following code:

x = linspace(-10,10); % defines a range of values for x from -10 to 10
y = x; % defines y as the same values as x
plot(x,y); % plots the function y=x

You can also use the "ezplot" function in Matlab to plot simple functions without having to define a range of values for the variable. For the y=x function, you can use the following code:

ezplot('x'); % plots the function y=x

I hope this helps you plot the y=x function in Matlab. If you have any further questions or need more assistance, please don't hesitate to ask. Good luck with your plotting!

Best,
 

Related to Plotting y=x Function in Matlab: Tips for Beginners

1. How do I plot y=x in Matlab?

To plot y=x in Matlab, you can use the "plot" function and specify the values of x and y as inputs. For example, if you want to plot the line y=x from x=1 to x=10, you would use the following code:

x = 1:10;
y = x;
plot(x,y)

This will create a simple line plot of y=x with x values ranging from 1 to 10.

2. What if I want to change the color or style of the plot?

You can use additional inputs in the "plot" function to specify the color and style of the line. For example, to plot a red dashed line, you would use the code:

plot(x,y,'r--')

This will create a red dashed line instead of the default blue solid line. You can also use other colors and line styles, such as 'g-' for a green solid line or 'k:' for a black dotted line.

3. Can I add labels and a title to the plot?

Yes, you can use the "xlabel", "ylabel", and "title" functions to add labels and a title to your plot. For example, if you want to label the x-axis as "x values" and the y-axis as "y values" and add a title "y=x Plot", you would use the following code:

xlabel('x values')
ylabel('y values')
title('y=x Plot')

These functions will add the specified labels and title to your plot.

4. How do I change the axis limits of the plot?

You can use the "xlim" and "ylim" functions to change the axis limits of your plot. For example, if you want the x-axis to range from 0 to 20 and the y-axis to range from -5 to 5, you would use the code:

xlim([0 20])
ylim([-5 5])

This will change the axis limits of your plot accordingly.

5. Is there a way to save my plot as an image file?

Yes, you can use the "saveas" function to save your plot as an image file. For example, if you want to save your plot as a PNG file named "y=x_plot", you would use the code:

saveas(gcf,'y=x_plot.png')

This will save your plot as a PNG file in your current working directory.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
5K
Back
Top