Sunteți pe pagina 1din 4

Lab100 Week 11: Plotting graphs

This week you will see how to plot a function in Scilab. However there are some pitfalls to avoid. The
command plot is easy, the hard bit is to get the range of the variables right.
After each question, make rough sketches of the plots you see in the graphics window, and write down
the corresponding mathematical functions.
You meet the Scilab commands
plot linspace xgrid title figure clf() clear
Q 11.1 A simple plot.
Start Scilab.
x = -3:5
y = 2*x + 3
plot(x,y)

// plotting values (range)


// a linear function of x
// plot x against y

A plot will be displayed in the Figure window.


This function is plotted on the range x = -3:5.
Q 11.2 Choosing the range.
Choose suitable plotting ranges for these two simple mathematical functions y = x2 and y = 4x 1
and plot. When x is a Scilab vector, x.^2 with the dot, is the vector of squares.
Q 11.3 Errors.
Scilab automatically overlays graphs if the graphics window is already open. To clear the figure
type clf(). To clear all variables type clear. To open a new figure window type scf(n) where n
is the figure window number e.g. scf(2) will open a new figure window called 2.
The order of defining the range, the function and then using the plot command is very important.
eg
clear, clf()
y = x*2
x = [ 0,1,2 ]
plot( x,y )

% To clear the window.

gives the error message !--error 4 undefined variable :

y Why?

Q 11.4 Another example.


A vector y need not be explicitly defined before the plot command. Try:
x = -10:10
plot(x, 3*x + 7)
xgrid(1)
1

Q 11.5 Another example.


One way to define the range is to use the linspace function. This command gives 100 points in
the interval.
clear, clf()
% To clear the window.
x = linspace(0,2)
y = 2*x-1
plot(x,y)
Now plot the function against x2 rather than against x:
x = linspace(0,2) ;
plot(x.^2, 3*x + 7)
Important: When multiplying Scilab vectors pointwise you need the dot eg x.*x or x.^2 Surprised?
Make a note of this.
Q 11.6 Plot two functions in one figure.
We can compare two functions on the same plot, Scilab does this automatically if we dont close
the first plot window.
clear, clf()
x = linspace(0,2);
y = 2*x-1;
plot( x,y )
z = sin(x);
plot( x,z )
xgrid(1)
What are the approximate coordinates of the intersection?
Q 11.7 An alternative method.
The functions are a shifted sin, and a displaced cos.
clear, clf()
x = linspace(0,10) ;
y = sin(x) - x/10 ;
z = cos(x) - 1/10 ;
plot( x,y )
plot( x,y, x,z )
plot( x,y, x,z, x,0*x )
title( y and z against x )
Make a note of how the figure builds up.
2

Note: When the figure window is open you can select Edit, Axes Properties to edit options like
axis labels, titles.
Q 11.8 Argand diagram.
Complex numbers are plotted in the Argand diagram. First set up the axes, add a grid and hold
the diagram.
clf()
mtlb_axis([-5,5,-5,5]),
xgrid(1)
Then define the points and vectors to draw.
z = -1+1.5*%i
plot(real(z),imag(z),x), // plots real and imaginary parts of z
nought = 0+0*%i
plot([real(nought);real(z)],[imag(nought);imag(z)])
z2=z^2
plot([real(nought);real(z2)],[imag(nought);imag(z2)])
Identify the points and lines.
Q 11.9 Polar coordinates.
A plot in polar coordinates:
clf()
theta = %pi/4
// radians
theta*360/(2*%pi)
// degrees
z = 3+4*%i
theta = atan( real(z)/imag(z) ) // radians
r = abs(z)
r*( sin(theta) + %i*cos(theta) ) // z !!!
// now to plot
theta = linspace(0,2*%pi) ; // 100 points
r = 1+cos( 2*(theta-%pi/6) ) ;
polarplot(theta,r)
You may need to resize your window to see the entire plot.
Add a title to your plot using Edit, Axes Properties (remember to click out of the box for the title
to appear).
Why are the two eggs orientated at 30degrees? What are the effects on this curve of doubling
(i) the period shift at 2*
3

(ii) the phase shift at -pi/6 ?


Note: You can use the command polar(z) to transfer a complex number z into its polar form e.g.

[Rho, Theta]=polar(z)
Theta =
0.9272952 - 8.327D-17i
Rho =
5.
Q 11.10 Solving equations.
Using a suitable interval for x plot
y = 2 (x 3)2
and
y = x/2 + 1
Do the two graphs intersect?

ASSESSED WORK by Sunday: select quizwk11 from your MyModules page.

Remember to EXIT from Scilab before logging out.

S-ar putea să vă placă și