Vous êtes sur la page 1sur 4

UNIVERSITI SAINS MALAYSIA SCHOOL OF COMPUTER SCIENCES

CPT115 MATHEMATICAL METHODS FOR COMPUTER SCIENCE Lab 2 Chapter 2 and 3 1. Conditional statements: Try the following commands: a=2; if a==2 disp(' a is really equal to two ') else disp(' a is NOT two !') end; (if runs commands conditionally. When the condition is true, the first block of instructions are being run, otherwise the else block will be run.) 2. Update the 2nd order linear equation program we tried before to print a message which informs the user of the sign of delta (i.e. the term under the square root). Save the program and run. Loops: Try the following command: for k=1:10 disp('hello'); end 4. Write a program which gets a number from input and calculates and prints its factorial on the screen. Save the program in a file and run.

3.

Chapter 4 One of the methods to draw the graph of a function in Matlab, is to them by calculating several points along the graph and connecting them. We first need to create a matrix containing desired x values (independent variable). Matlab will then calculate y values (dependent variable) for each of those x values. The respective x and y values are then used to plot the graph. 1. Creating x values: Try the following commands. Both commands can be used to produce desired x values.
1

x=1:1:10 (creates an array x1 which contains values 1,2,...,10) x1=linspace(1,10,10) (same as above, creates a linearly divided space 1-10) (the space is from 1 to 10, divided to 10 sections) 2. Creating y values: Use normal Matlab formulas to compose your function and calculate y values. y= 2*x.^2 - 3*x +1 (creates an array y containing y= + values) (Notice the . before ^, this means using matrix power)

3.

Plot the graph: Now that we have both x and y values for a specific range of x, we can plot the function: plot(x,y) (plots the graph of x->y values and connects them to form a line)

4.

You may also use optional commands to modify plot output. xlabel('values of x') ylabel('values of y') title('Graph of 2x^2-3x+1') grid (adds a label to horizontal axis) (adds a label to vertical axis) (adds a title for the graph) (turns the grids on)

Graph of 2x 2-3x+1 180 160 140 120 values of y 100 80 60 40 20 0

5 6 values of x

10

5.

You may draw multiple graphs on the same plot y1= x.^2 plot(x,y,x,y1) (creates an array containing y= function values) (plots x->y values as well as x->y1 on the screen)
2

Another method is to hold the plot window and plot separately. In order to test, first close the existing plot window and execute the following commands: 6. plot(x,y) hold on plot(x,y1) hold off

Identifying graphs: you can add a small text on the graphs to identify them. You can use plot text feature for this purpose. First run below command: gtext('This is for 2x^2-3x+1') Now return to plot window and use the crosshairs to place the text in desired location.

Drawing logarithmic functions 1. In order to draw logarithmic graphs (i.e. horizontal, vertical or both axis with logarithmic scales), you need to produce x values so that they increase in logarithmic scale. x=logspace(0,1,100) (Creates a logarithmically divided space from -1 to 1) (the space is divided into 100 sections) y=x.^2 (calculate y values) loglog(x,y) (plots x and y values on a graph with logarithmic axis) grid (turns on the grid so that you can see scales) semilogx(x,y) (same as above, but only x axis has logarithmic scale) semilogy(x,y) (same as above, but only y axis has logarithmic scale)

Drawing bar and stem plots 1. If you have x and y paired values and you want to draw bar chart and stem plots of them you can use below commands. x= [2001, 2002,2003,2004,2005]; y= [123.2,156.7, 189.1, 162.3, 200]; bar(x,y)

200 180 160 140 120 100 80 60 40 20 0

2001

2002

2003

2004

2005

stem(x,y)
200 180 160 140 120 100 80 60 40 20 0 2001

2001.5

2002

2002.5

2003

2003.5

2004

2004.5

2005

Note: You may use the menus and the toolbar in plot window to modify the graph.

-------- The End --------

Vous aimerez peut-être aussi