Arrays

Plotting graphs

Last practical we introduced some of the basic plotting functionality, including plotting a straight line and modifying the graph axes, title etc. This week we will look at using arrays to plot functions.

How does the plot command work? Actually, it draws a straight line segment between each set of coordinates that you give it. If the line you are trying to draw is a curve, it will appear smooth by plotting many tiny lines.

Exercise

> Create a script M-file called plotArray.m for the following code in order to plot a sine curve between 0 and 2∏. Don’t forget to add comment headers.

clear all

points = 3

x = linspace(0,2*pi,points)

y = sin(x)

plot(x,y)

> What is wrong with this graph? Change the variable called points to 5 and save before re-running the code, then increase it to 10, 20, 40, 80, 200. Examine each new graph and stop when you think that it is acceptable.