Applied Numerical Methods Using MATLAB. Won Y. Yang

Applied Numerical Methods Using MATLAB - Won Y. Yang


Скачать книгу
integer number %% % %bx floating number in 16 hexadecimal digits(64 bits) '' '

      %nm113.m f=input('Input the temperature in Fahrenheit[F]:'); c=5/9*(f-32); fprintf('%5.2f(in Fahrenheit) is %5.2f(in Centigrade).\n',f,c) fid=fopen('nm113.txt','w') fprintf(fid,'%5.2f(Fahrenheit) is %5.2f(Centigrade).\n',f,c) fclose(fid)

      In case you want the keyboard input to be recognized as a string, you should add the character 's' as the second input argument.

       >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: <:
Скачать книгу
Librs.Net