Let’s write a script to do the following things in sequence (include comments at each step!):
n
with a value of 20t
containing 1
to n
(in steps of 1)sig
that is equal to 0.3
times the square of t
figure
to pop up a new figure windowplot()
to plot sig
as a function of t
;
) to end each line (to suppress unwanted output)>> doc plot % if in doubt check help
There are many plotting / graphing related functions. Briefly play around with the following:
xlabel()
, ylabel()
, title()
you can provide string to them in single quotes ‘like this’. The single quotes are the way to define character strings. Ultimately, they can of course also be stored in variables.
plot(t, sig, 'rs-' ) % red, square symbols, solid line
plot(t, sig, 'rs-', t, sig./2, 'b:' )
The plot
command is very versatile and you’ll see people use it in many different ways. If you read the help/documentation for the command you’ll see that the first/normal use is to provide both x
and y
coordinates for the points you want to plot, e.g.:
x = 1:10;
y = rand(1,10); % 10 random numbers
plot(x,y) % simple plot
plot(x,y, 'rs--') % plot with line style
%
y2 = rand(1,10); % another 10 numbers
plot(x,y, 'rs--', x,y2, 'bo-') % two plots in one
myPlot()
that takes two input arguments and returns no output argument. All the function should do is to use the matlab plot()
command as per usual, but with your preferred properties for the plot. Look at help plot
to get some inspiration, but inside your function you might want to try to use, e.g. the properties'linewidth', 2 % or whichever thickness you like
'color', [1 1 0] % or some other color triplet in red, green, blue
'markersize', 15
'marker', 's'
Loops, if/else, controlling flow - loops and controls