Applied Numerical Methods Using MATLAB. Won Y. Yang

Чтение книги онлайн.

Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 14

Applied Numerical Methods Using MATLAB - Won Y. Yang

Скачать книгу

       >ans=input('Answer <yes> or <no>: ','s')

      How do we plot the value(s) of a vector or an array? Suppose data reflecting the highest/lowest temperatures for five days are stored as a 5 × 2 array in an ASCII file named ‘temp.txt’.

      %nm01f01.m % plot the data of a 5x2 array stored in "temp.txt" load temp.txt % load the data file "temp.txt" clf, plot(temp) % clear any existent figure and plot title('The highest/lowest temperature of these days') ylabel('degrees[C]'), xlabel('day')

       The command ‘ plot()’ reads along the columns of the 5 × 2 array data given as its input argument and recognizes each column as the value of a vector.

       MATLAB assumes the domain of the horizontal variable to be [1 2 … 5] by default, where 5 equals the length of the vector to be plotted (see Figure 1.1a).

       The graph is constructed by connecting the data points with the straight lines and is piecewise‐linear (PWL), while it looks like a curve as the data points are densely collected. Note that the graph can be plotted as points in various forms according to the optional input argument described in Table 1.2.

      1 (Q1) Suppose the data in the array named ‘temp’ are the highest/lowest temperatures measured on the 11th, 12th, 14th, 16th, and 17th days, respectively. How should we modify the above script to have the actual days shown on the horizontal axis?

      2 (A1) Just make the day vector [11 12 14 16 17] and use it as the first input argument of the ‘ plot()’ command.>days=[11 12 14 16 17]; plot(days,temp)Running these statements yields the graph in Figure 1.1b.

      3 (Q2) How do we change the ranges of the horizontal/vertical axes into 10–20 and 0–30, respectively, and draw the grid on the graph?

      4 (A2)>axis([10 20 0 30]), grid on

      5 (Q3) How can we change the range of just the horizontal or vertical axis, separately?

      6 (A3)>xlim([11 17]); ylim([0 30])

      7 (Q4) How do we make the scales of the horizontal/vertical axes equal so that a circle appears round, not like an ellipse?

      8 (A4)>axis('equal')

      9 (Q5) How do we fix the tick values and their labels of the horizontal/vertical axes?

      10 (A5)>set(gca,'xtick',[11:3:17],'xticklabel',{'11','14','17'}) >set(gca,'ytick',[5:5:15],'yticklabel',{'5','15','25'})where gca is the current (figure) axis and gcf is the current figure handle.

      11 (Q6) How do we have another graph overlapped onto an existing graph?

      12 (A6) If you run the ‘ hold on’ command after plotting the first graph, any following graphs in the same section will be overlapped onto the existing one(s) rather than plotted newly. For example:>hold on, plot(days,temp(:,1),'b*', days,temp(:,2),'ro') This will be good until you issue the command ‘ hold off’ or clear all the graphs in the graphic window by using the ‘ clf’ command.

Line type Point type (marker symbol) Color
Solid line · Dot + Plus * Asterisk r: Red m: Magenta
: Dotted line ̂: Δ >: > o Circle g: Green y: Yellow
‐‐ Dashed line p: * v: x: x‐Mark b: Blue c: Cyan (sky blue)
‐. Dash‐dot d: <: < s:□ Square k: Black

      Sometimes we need to see the inter‐relationship between two variables. Suppose we want to plot the lowest/highest temperature, respectively, along the horizontal/vertical axis in order to grasp the relationship between them. Let us try using the following command:

       >plot(temp(:,1),temp(:,2),'kx') % temp(:,2) vs. temp(:,1) in black 'x'

Скачать книгу