Matlab drawing preliminary test

Source: Internet
Author: User
 

Because of the needs of the experiment report, we have matlab plotting.

X = 0: pi/1000:2 * pi; y1 = sin (2 * x); y2 = 2 * cos (2 * x); % output image plot (x, y1, 'r-', x, y2,' B -- '); title ('plot of f (x) = sin (2x) and its derivative '); % set the xlabel ('x') for the X and Y coordinates; ylabel ('y'); % create the legend ('f (x) = sin (2x )', 'd/dx f (x) ') % display grid on;

1. x = 0: pi/1000:2 * pi. It indicates that the x coordinate starts from 0 to 2 * pi. the incremental value is pi/1000. It indicates no echo (print the result)

2. y1 = sin (2 * x); returns the corresponding ordinate.

3. y2 = 2 * cos (2 * x); the same is true.

4.% output image, % Represents the annotator

5. plot (x, y1, 'r-', x, y2,' B -- '); plot is a drawing function, which indicates spending multiple curves on a layout, (The first line is x axis y1 is Y coordinate, and the draw line is r red.-Indicates solid line.) (the second line is x coordinate y2 is Y coordinate, and the draw line is B black. -- indicates the dotted line)

6. title ('plot of f (x) = sin (2x) and its derivative '); title

7.% set the xlabel ('x') for the X and Y coordinates; ylabel ('y'); that is, the label is added.

8. The subsequent explanations are clear.

This is the first matlab program, the first contact, which can be viewed directly by someone else's program.

Appendix: usage of the plot Function

There is no need to use all the functions. You just need to use them as needed and remember the most basic usage. ------------------------------------------------------------------------- Basic form> y = [0 0.58 0.70 0.95 0.83];> plot (y) the generated graph is a line drawn based on the X coordinate of the serial number and the X coordinate of array y.> X = linspace (* pi, 30); % to generate a set of linear offset values> y = sin (x);> plot (x, y) the generated graph is a smooth sine curve formed by the last 30 points. Multiple lines can draw many curves on the same screen. You only need to give several arrays, for example,> x = 0: pi/* pi;> y1 = sin (x);> y2 = cos (x);> plot (x, y1, x, y2) can draw multiple lines. Another method is to use the hold command. If hold on is set for a drawn image, MATLA draws the image generated by the new plot command on the original image. The command hold off will end the process. Example:> x = linspace (* pi, 30); y = sin (x); plot (x, y)> hold on> z = cos (x); plot (x, z)> hold off linear and color MATLAB has many options for curve line and color, the annotation method is to add a string parameter after each pair of arrays, which is described as follows: linear line:-solid line: dot line -. dotted Line-twists and turns line. Line type :. dot + plus sign * asterisks x-o circle color: y yellow; r red; g green; B blue; w white; k black; m purple; c green. the following example illustrates the usage:> x = 0: pi/15:2 * pi;> y1 = sin (x); y2 = cos (x);> plot (x, y1, 'B: +', x, y2, 'g -. * ') You can add a grid, title, X axis tag, and Y axis tag to the grid and tag on a graph. Run the following command to complete these tasks:> X = linspace (0, 2 * pi, 30); y = sin (x); z = cos (x);> plot (x, y, x, z)> grid> xlabel ('dependent Variable x')> ylabel ('dependent Variables Y and Z')> title ('sine and Cosine Curves ') you can also add a string at any position in the image. For example, you can use "> text (2.5, 0.7, 'sinx')" to indicate that it is in the coordinate x = 2.5, add the string sinx at y = 0.7. It is more convenient to use the mouse to determine the position of the string by entering the command:> gtext ('sinx') in the graphic window cross line is the position of the string, click here to place the string. By default, MATLAB automatically selects the ratio of horizontal and vertical coordinates of the image in the coordinate system. If you are not satisfied with this ratio, you can use the axis command to control it. Commonly used include: axis ([xmin xmax ymin ymax]) [] returns the maximum value, minimum value axis equal, and axis ('equal') of the X axis and Y axis respectively ') the length of the X axis and Y axis is the same as that of axis square or axis ('square '). The frame is square axis off or axis ('off ') you can also clear the coordinate scale and use axis auto axis image axis xy axis ij axis normal axis on axis (axis) to refer to the online help system. Multiple images can create several coordinate systems on the same screen, and use the subplot (m, n, p) command to divide a screen into m × n graphic regions, p indicates the current region number. Draw a chart in each region, for example,> x = linspace (* pi, 30); y = sin (x ); z = cos (x);> u = 2 * sin (x ). * cos (x); v = sin (x ). /cos (x);> subplot (2, 2, 1), plot (x, y), axis ([0 2 * pi-1]), title ('sin (x) ')> subplot (2, 2), plot (x, z), axis ([0 2 * pi-1]), title ('cos (x) ')> subplot (2, 2, 3), plot (x, u), axis ([0 2 * pi-1]), title ('2sin (x) cos (x) ')> subplot (2, 2, 4), plot (x, v), axis ([0 2 * p I-20 20]), title ('sin (x)/cos (x) ') graphics output in mathematical modeling, often need to output the generated graphics to Word documents. You can use the following methods: first, select the Export option in the File menu in the MATLAB graphic window to open the graphic output dialog box, in this dialog box, you can save the image in emf, bmp, jpg, pgm, and other formats. Then, open the corresponding document, and select the picture option in the Insert menu to insert the corresponding picture.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.