Practical 2: Variables, Script M-files and Graphing

Plotting Graphs

Exercise

> Create and run a script M-file called plotMe.m for the following command. Remember to add your comment headers at the top.

plot([1 3], [2 5])

Note: The graph will appear in a separate Figure Window named Figure 1 that is often hidden behind the main MATLAB window. Maximise it on your taskbar if it does not pop up.

> What does the graph look like? Verify which points of 1, 2, 3, 5 are x-coordinates or y-coordinates.

 

> Modify your plotMe script M-file to plot the functions y = 3x + 2 and z=4x over the region of 1 \le x \le 3 by following these steps.

  1. Define the endpoints of x as the two variables x_{1} & x_{2}.
  2. Define the endpoints of y, as the two variables y_{1} & y_{2}, in terms of x_{1} & x_{2).
  3. Change the plot command to:
    plot([x1 x2], [y1 y2])
  4. Run your file to check that your updated graph is correct.
  5. Define the endpoints of z, as the two variables z_{1} & z_{2}, in terms of x_{1} & x_{2}.
  6. Change the plot command so that both lines are plotted on one graph as follows:
    plot([x1 x2], [y1 y2], [x1 x2], [z1 z2])
  7. Check that your updated graph is correct.