Creating A Graph with MATLAB
From time to time situations arise that require the graphing of a solution, with its graphing technology MATLAB provides a convenient and intuitive way of plotting graphs and adding assets to those plots. In this post we will be looking at using MATLAB to create a graph showing the relationship between water temperature and its density.
Table showing the Density of water at various temperatures.
Temp./ ^{o}C |
Density/ mL |
Temp./ ^{o}C |
Density/ mL |
15 |
0.9991 |
24 |
0.973 |
16 |
0.9990 |
25 |
0.9971 |
17 |
0.9988 |
26 |
0.9968 |
18 |
0.9986 |
27 |
0.9965 |
19 |
0.9984 |
28 |
0.9963 |
20 |
0.9982 |
29 |
0.9960 |
21 |
0.9980 |
30 |
0.9957 |
22 |
0.9978 |
31 |
0.9954 |
23 |
0.9976 |
32 |
0.9951 |
Here we go! launch MATLAB.
Enter in the MATLAB command window.
x = 15:32;
Enter in the MATLAB command window.
y = [0.9991 0.9990 0.9988 0.9986 0.9984 0.9982 0.9980 0.9978 0.9976 0.973 0.9971 0.9968 0.9965 0.9963 0.9960 0.9957 0.9954 0.9951];
Enter in the MATLAB command window. This will create the initial graph.
plot(x,y, '*-')
Now that we have the initial plot we can move on to adding x and y axis labels and a chart title.
Enter in the MATLAB command window.
ylabel('Density/ mL') xlabel('Temp./ ºC') title('Graph showing density of water at various temperatures')
At this point you may be asking yourself, how can this graph help me? well it just so happens that MATLAB has this function called interp1 which allows you to find a y axis value for a given x axis value. We will use this function to find the density of water at any given temperature.
Lets look at the density for water at 26.33 ºC and 27 ºC. In the command window enter.
interp1(x, y,26.33)
The resulting y value = 0.9967
In the command window enter.
interp1(x, y,27)
The resulting y value = 0.9965
Lets graph the solution for x = 26.33 and y = 0.9967
line([26.33 26.33],[0.995 0.9967])
Enter in the MATLAB command window
line([26.33 14],[0.9967 0.9967])
The end🙂, I hope this helps some chemistry student out there.