# Draw a simpl graph in matlab

• MATLAB
• boreilly
In summary, the person is looking for help on how to draw a straight line graph with a best fit line and determine its slope in MATLAB. They have had difficulty finding resources online and have a list of voltage measurements and corresponding speeds. They are provided with a link that explains how to perform linear curve fitting in MATLAB.

#### boreilly

i have done a simple experient and now i want to draw a graph in MATLAB. how do i do that? its a straight line graph so i'd like to be able to put on a best fit line and get the slope of that. As basic as this is I'm struggling to find help how to do it on the net.seems i can find a billion pages on graphing functions etc and big fancy 3d graphs. I have a list of voltage measurements and a corresponding speed for each one

http://www.swarthmore.edu/NatSci/echeeve1/Ref/Matlab/CurveFit/LinearCurveFit.html [Broken]

Last edited by a moderator:
perfect. exactly. thanks.

## 1. How do I draw a simple graph in MATLAB?

To draw a simple graph in MATLAB, you can use the plot() function. This function takes in two vectors as its input, where the first vector represents the x-coordinates and the second vector represents the y-coordinates of the points on the graph. For example, plot([1,2,3],[4,5,6]) will plot three points with coordinates (1,4), (2,5), and (3,6) on a graph.

## 2. Can I customize the appearance of my graph in MATLAB?

Yes, you can customize the appearance of your graph in MATLAB by using additional arguments in the plot() function. For example, plot(x,y,'r--o') will plot a red dashed line with circles at each data point. You can also add a title, axis labels, and legend to your graph using the title(), xlabel(), ylabel(), and legend() functions, respectively.

## 3. How do I save my graph in MATLAB?

To save your graph in MATLAB, you can use the saveas() function. This function takes in two arguments - the handle of the figure you want to save and the file name. For example, saveas(gcf, 'mygraph.png') will save the current figure as a PNG image with the file name "mygraph". You can also specify the file format by using the appropriate file extension, such as .jpg or .pdf.

## 4. How can I add multiple lines to my graph in MATLAB?

To add multiple lines to your graph in MATLAB, you can use the hold on command. This command allows you to plot multiple lines on the same graph without overwriting the previous lines. For example, plot(x,y1) followed by hold on and plot(x,y2) will plot two lines on the same graph. Don't forget to use hold off when you're done plotting to turn off this feature.

## 5. How can I plot a function in MATLAB?

To plot a function in MATLAB, you can use the fplot() function. This function takes in two arguments - the function you want to plot and the range of values for the independent variable. For example, fplot(@(x) x^2, [-5,5]) will plot the function f(x) = x^2 for x values ranging from -5 to 5. You can also customize the appearance of the graph by adding additional arguments, such as line style and color.