Introduction: How to Plot a Function in MATLAB
MATLAB is one of the most actively used mathematical analysis softwares in the engineering field. It can be used for control modeling, electrical signal processing, tests and measurements, and much more. This Instructable shows you how to plot the results of a given function using MATLAB R2013a.
PLEASE NOTE: You must have access to this software (on the Baylor campus, this can be found in Rogers Engineering & Computer Science building. The basement of Moody Library has MATLAB R2012a, which is very similar to R2013a and can be used instead). To purchase a personal copy of MATLAB, please visit www.mathworks.com/products/matlab.
Time to complete: 5-10 minutes
Step 1: Open Matlab
Go to the start button in the bottom left-hand corner of your screen, and search for "MATLAB." Once MATLAB R2013a appears on the menu, click it to run the program.
Step 2: Change the Folder Path
Click on "Browse for Folder" icon in the top left corner, as shown. A new window will pop up, and you can choose the destination to save your files. Choose something you can access later and is easy to remember (ex: a flash drive, "My Documents," etc.). After selecting your destination, click "ok" to close the window.
Step 3: Create a New Script File
In the upper left-hand corner, click the "New Script" button as shown. This will open a new window, known as a script file. A script file allows you to store your code and access it at a later point.
Step 4: Input Data
Now you are able to type in your function and data. Your first line of data will be your x values. To create this data input, type the following line of code "x = linspace(0,10);" The "linspace" command creates 100 evenly spaced data points between 0 and 10, and stores them under the "x" variable name. Putting a semicolon at the end of the code line will hide the "x" variable values from showing when you run the code.
Step 5: Write a Function
In this example, we will use the function y = x^2. To create a new line of code, simply press the "Enter" button. Your second line of code will be "y = x.^2;", as shown. This will square each data point stored in x (as written in line 1 of the code and step 4 of this Instructable) and suppress the "y" variable values.
Step 6: Plot the Function
Communicating your results is the most important part of data analysis. To do so, we will create a plot of the function. In your third line of code, type the following "plot(x,y,'LineWidth',2,'Color',[1 0 0])". This will generate a graph that plots your stored "x" points along the x-axis and your "y" points along the y-axis with a red line.
Step 7: Label Your Plot
Properly labeling your data is a crucial part of communicating results, as well. In lines 4-6 of your code, type the following commands" xlabel('x')","ylabel('y')","title('My Fun Plot')". This will label your x-axis as "x", your y-axis as "y", and create a title of "My Fun Plot" to go over your entire plot.
Step 8: Save Your Script File
By saving your script file, you are able to access and run it at a later date. Go to the "Save" button in the top left corner, click on the floppy disc icon, and save your file as "my_plot.m" in the destination set in Step 2 (Change the Folder Path). Click "Save" to close the window. Any ".m" file can be read by MATLAB.
Step 9: Run Your Script File
To run your script file, press F5 on your keyboard. This will run your code, save your variables, and generate the plot in a new window called "Figure 1." Congratulations! You have analyzed a function and plotted your results.

