CH-02 (523169), страница 4
Текст из файла (страница 4)
For obtaining the first and last equations, we utilize the fact that sinceY and its derivatives are periodic, in addition to YN = Y1, (Y″)N = (Y″)1, HN = H1,we also have YN + 1 = Y2, Y0 = YN–1, (Y″)N + 1 = (Y″)2, (Y″)0 = (Y″)N–1, and H0 = HN–1.A program called CubeSpln has been prepared to handle both the nonperiodicand periodic cases. It formulates the matrix equation [A]{Y″} = {C} for solving thecurvatures at Xi for i = 1,2,…,N based on Equation 18. A Gaussian eliminationscheme is needed by this subroutine for obtaining the solutions of Y″. ProgramCubeSpln also has a block of statements for plotting of the spline curves. Thissubroutine is listed below.QUICKBASIC VERSION© 2001 by CRC Press LLC© 2001 by CRC Press LLCSample ApplicationWhen program CubeSpln is run, it gives a plot of the cubic spline curves asshown in Figure 5.
The given points are marked with + symbols. Between every twoadjacent points, a third-order polynomial is derived. There are two different thirdorder polynomials for the left and right sides of every in-between points, at whichthe slopes and curvatures determined by the two third-order polynomials are bothcontinuous.MATLAB APPLICATIONMATLAB has a function file called spline.m which can be applied to performthe curve fit of cubic spline. The function has three arguments: the first and secondarguments are for the coordinates of the data points for which a cubic spline curvefit is to be obtained, and the third argument should be an array containing a morefinely spaced abscissa at which the curve ordinates should be calculated for plottingthe spline curve.
Let us redo the problem for which Figure 5 has been obtained. TheMATLAB application and the resulting display are as follows:© 2001 by CRC Press LLCFIGURE 5. When program CubeSpln is run, it gives a plot of the cubic spline curves.The plot of the spline curve using XC and YC data superimposed with the given5 (X,Y) points marked by the * character is shown in Figure 6 which is identical toFigure 5 except different in the ranges of the axes.For dealing with data sets, more features of plot.m of MATLAB can be utilized.Figure 7 shows how different data sets can be marked with different characters, axescan be labeled, and various text can be added. The interactive MATLAB commandsentered are:Notice the commands xlabel, ylabel, and text are adding labels for the x and yaxes, and text, respectively.
The specific content string of label or text is to be spelledout inside the two single quotation signs. The first two arguments for text are thecoordinates, at which the left lower corner of the first character of that string.© 2001 by CRC Press LLCFIGURE 6. The plot of the spline curve using XC and YC data superimposed with the given5 (X,Y) points marked by the * character is identical to Figure 5 except different in the rangesof the axes.FIGURE 7. How different data sets can be marked with different characters, axes can belabeled, and various text can be added.© 2001 by CRC Press LLCMATHEMATICA APPLICATIONSMathematica has an interpolation function which fits a collection of specifiedpoints by a polynomial. The command InterpolationOrder specifies the order ofpolynomial and the default order is 3. Here are some examples of applications andplots.Input[1]: =x = {1,2,3,4,5}Output[1] ={1, 2, 3, 4, 5}Input[2]: =y = {2,4,7,8,11}Output[2] ={2, 4, 7, 8, 11}Input[3]: =Plot[Evaluate[Interpolation[y]][x], {x,1,5},Frame->True, AspectRatio->1]Output[3] =FIGURE 8.© 2001 by CRC Press LLCEvaluate calculates the values of the fitted polynomial within the specifiedinterval.
In this case, the y values are interpolated using the cubic polynomial.To add more features to a plot, Mathematica has commands Text allowing astring of characters to be placed at a desired location, and FrameLabel allowinglabels to be added to the axes. In fact, Figure 8 is not the result of Input[3] but theaddition of the five + markers with the statementInput[4]: =Show[%,Graphics[Table[Text[“+”,{x[[i]],y[[i]]}],{i,1,5}]]]Output[4] =—Graphics—% refers to the previous output and %% refers to the next-to-the-last output,and so on.
Show and Graphics enable additional graphics to be shown. Table listsa series of entries specified by the running index variable which in this case is Ihaving a starting value of 1 and incremented by 1 (omitted), and ending at a valueequal to 5. Notice that the four * markers are not exactly located at the coordinatesspecified, for the reason that the character itself is not centered but offsets like asuperscript.As another example, consider the plot shown in Figure 9 which is resulted fromof the following statements:Input[1]: =X = {1,2,3,4}Output[1] ={1, 2, 3, 4}Input[2]: =Y = {2, 4, 7, 13}Output[2] ={2, 4, 7, 13}Input[3]: =X1 = {0.5, 1.2, 2.5, 3.7}Output[3] ={0.5, 1.2, 2.5, 3.7}© 2001 by CRC Press LLCInput[4]: =Y1 = {3, 6, 5, 11}Output[4] ={3, 6, 5, 11}Input[5]: =X2 = {3.0, 3.6, 4.2, 5.1}Output[5] ={3.0, 3.6, 4.2, 5.1}Input[6]: =Y2 = {3, 6, 8, 11}Output[6] ={3, 6, 8, 11}Input[7]: =g1 = Show[Graphics[Table[Text[“*”,{X[[i]],Y[[i]] }],{i,1,5}],Table[Text[“ + ”,{X1[[i]],Y1[[i]]}],{i,1,5}],Table[Text[“.”,{X2[[i]],Y2[[i]]}],{i,1,5}]]]Output[7] =—Graphics—Input[8]: =g2 = Show[g1, Frame->True, AspectRatio->1,FrameLabel->{“X”,“Y”}]Output[8] =—Graphics—Input[9]: =Show[g2,Graphics[Text[“X–Y — *”,{0.7,12},{–1,0}],Text[“X1–Y1 — + ”,{0.7,11},{–1,0}],Text[“X2–Y2 — .”,{0.7,10}],{–1,0}]]]© 2001 by CRC Press LLCOutput[9] =FIGURE 9.The two intermediate plots designated as g1 and g2 are actually displayed onscreen but not presented here.
Only the final plot showing all of the ingredients ispresented in Figure 9. Giving plot names facilitates the later referral; a better arrangement than using the % option. In Figure 9, it is also illustrated that the label for thevertical axis is rotated by 90 degrees.Mathematica has a package called SplineFit can also be called into service forthe need of spline curve-fit.
For creating Figure 9, we may enter the request asfollows:Input[1]: = <<NumericalMath`SplineFit`Input[2]: = XYS = {{1,2},{2,4},{3,7},{4,8},{5,11}};Input[3]: = Spline = SplineFit[XYS, Cubic]Output[3] = SplineFunction[Cubic, {0.,4.}, <>]Input[4]: = ParametricPlot[Spline[x], {x,0,4}, Frame->True, AspectRatio->1]© 2001 by CRC Press LLCOutput[4] = —Graphics—Input[5]: = Show[%,Graphics[Table[Text[“X”,{XYS[[i]]}],{i,1,5}]]]Output[5] = —Graphics—In Input[1], << loads the specified package `SplineFit`. Input[2] illustrates howthe coordinates of points can be entered as pairs and ; can be used to suppress thedisplay of the output.
“Cubic” spline fit is requested in Input[3] and in Input[5] theParametricPlot command is applied so that the coordinates of the points on the cubicspline curves are generated using a third parameter. Input[5] also demonstrates howa matrix should be called in a looping situation.Notice that two plots for Output[4] and Output[5] are not presented here, forthe reason that Figure 9 already shows all of the needed details. However, it shouldbe mentioned that use of “X” as the character in Text for marking the five XYSpoints in Input[5] will enable it to be centered at the coordinates specified insteadof being upwardly offset as is the case of using the character “*” in Figure 9.2.6 PROBLEMSEXACT CURVE-FIT1. Modify the program ExactFit so that the given points (1,3), (3,8), and(4,23) can be exact-fitted by the equation y = c1x + c2x2 + c3x4.2.
Modify the program ExactFit so that the given points (0,0.2), (2,0.5),(5,–0.4), and (7,–0.2) can be exact-fitted by the equation y = c1sinx +c2sin2x + c3sin3x + c4sin4x.LEASTSQ11. Given five points (1,1), (2,3), (3,2), (4,5), and (5,4), calculate the coefficients c1 and c2 in the linear equation y = c1 + c2x which fits the five pointsby the least-squares method.2. For a given set of data (1,–2), (2,0), (3,1), and (4,3), two equations havebeen suggested to fit these points.
They are Y = X–2 and Y = (-X2 +7X–10)/2. Based on the least-squares criterion, which equation should bechosen to provide a better fit? Explain why?3. During a tensile-strength test of a metallic material the following data(Xi,Yi) for i = 1,2,…,7 where X and Y represent strain (extension per unitlength) and stress (force per unit area), respectively, have been collected:XY.2651.03© 2001 by CRC Press LLC.3951.41.6951.71.9552.091.352.422.052.762.453.01(x10–3)(x103)4.5.6.7.8.9.10.11.12.13.14.15.16.Fit these data by the least-squares method with an equation Y =b1XExp(b2X) and calculate the values of the coefficients b1 and b2.
(Note:ln(Y/X) = ln(b1) + b2X)Apply polyfit of MATLAB to the data given in Problem 1 and fit themlinearly. Compare the answer with that of Problem 1.Same as Problem 4 except the data are to be fitted by a quadratic equation.Same as Problem 4 except the data are to be fitted by a cubic equation.Same as Problem 4 except the data are to be fitted by a quartic equation.Use the results of Problems 4 through 7 and enter MATLAB statementsto compare their errors.Apply polyfit of MATLAB to the data given in Problem 3 and fit themlinearly.
Compare the answer with that of Problem 3.Same as Problem 9 except the data are to be fitted by a quadratic equation.Same as Problem 9 except the data are to be fitted by a cubic equation.Same as Problem 9 except the data are to be fitted by a quartic equation.Use the results of Problems 9 through 12 and enter MATLAB statementsto compare their errors.Save the results of Problems 4 through 7 and generating the four curvesdetermined by the least-squares method. Obtain a composite graph ofthese four curves superimposed with the given points marked using thecharacter * by application of plot.Same as Problem 14 but for the curves determined by Problems 9 through 12.Try Mathematica to obtain and compare results for the above problems.LEASTSQG1.