Thompson - Computing for Scientists and Engineers (523188), страница 11
Текст из файла (страница 11)
Indeed, we explore this possibility in discussing the Lorentzian resonances inSection 10.2.2.6 PROJECT 2: PROGRAM TO CONVERT BETWEEN COORDINATES2.645PROJECT 2: PROGRAM TO CONVERTBETWEEN COORDINATESThe program Cartesian & Polar Coordinate Interconversiondeveloped inthis project serves both to develop your understanding of the relations between thesetwo coordinate systems and to give you practice with writing programs in C.The conversion from plane-polar to Cartesian coordinates is straightforward andunambiguous.
Given r and one has immediately (as discussed in Section 2.2)(2.78)which can be programmed directly.Stepping into the correct quadrantThe transformation from Cartesian coordinates to polar coordinates is less direct thanthe inverse transformation just considered. The required formulas are(2.79)which is straightforward to compute, and, in terms of the atan or tan-l function,(2.80)which is ambiguous. This formula does not uniquely determine the quadrant inwhich lies because only the sign of the quotient in (2.80) is available after the division has been made. The relative signs of the circular functions in the four quadrants indicated in Figure 2.7 may be used to determine the angle uniquely from thesigns of x and y.In some programming languages, including C, two functions are available forthe inverse tangent.
In one, such as the atan (t) in C language (with t the argument of the function), the angle is usually returned in the range /2 to /2, and theuser of this function has to determine the appropriate quadrant by other means.In the second function for the inverse tangent, such as atan2 (y, x) in the Clanguage, the angle is located in the correct quadrant by the function itself.
If wewere to use at atan2 in the program, the conversion from Cartesian to polar representation would be very direct. For practice in C and to reinforce your understanding ofplane-polar coordinates we use the simpler function atan.46COMPLEX VARIABLESFIGURE 2.7 Signs of the circular functions in each quadrant of the Cartesian plane.If we begin with an angle calculated into the first quadrant by using the absolutevalue of y/x, then multiples of /2 have to be added or subtracted to get the angleinto the correct quadrant. Computationally, the best way to get at the accuracy ofyour computer system is to use the identity(2.8 1)This relation is used in this program and in many others throughout this book.Coding, testing, and using the programThe structure of program Cartesian & Polar Coordinate Interconversionis typical of the interactive programs listed in the index to computer programs at theend of the book.
The program is controlled by a whi1e loop that terminates whenboth x and y (going from Cartesian to polar) and also r (going from polar to Cartesian) have been input with values of zero. Within each execution of the loop there isa choice of input of either Cartesian coordinates (chosen by c or C ) or polar ( por P ) coordinates.The Cartesian-input option uses the function MakePolar to locate an angle inthe first quadrant and to use the signs of x and y to adjust the quadrant, as discussedabove. Some care is required for angles that coincide with the axes.
The polar-inputoption uses (2.78) directly in the function MakeCartesian and returns the valuesof the x and y variables. The program listing is as follows.2.6 PROJECT 2: PROGRAM TO CONVERT BETWEEN COORDINATESPROGRAM 2.3 Interconversion between Cartesian and polar coordinates.#include#include<stdio.h><math.h>main ()/* Cartesian & Polar Coordinate Interconversion */double pi,x,y,r,theta;char isCP;voidMakePolar(),MakeCartesian();pi = 4*atan(l); /* pi to machine accuracy */printf("Cartesian & Polar Coordinates\n");x = 1; y = 1; r = 1;while ( (( x != 0 ) && ( y != 0 )) || ( r != 0 ) ){printf("\nCartesian (C) or Polar (P) input?\n");scanf("%s",&isCP) ;if ( isCP == 'c' || isCP == 'C' ){printf("Input Cartesian values as x y\n");scanf( "%le %le" , &x,&y) ;if((x!=O)||(y!=O)){MakePolar(pi,x,y,&r,&theta);printf("Polar values: r=%le, theta=%le (rad)\n",r,theta);}}else{if ( isCP == 'p' || isCP == 'P' ){printf("Input Polar values as r theta (rad) \n");scanf("%le %le",&r,&theta);if ( r != 0 ){MakeCartesian(r,theta,&x,&y);printf(Cartesian values: x=%le, y=%le\n",x,y);}}}}printf("\nEnd Cartesian & Polar Coordinates"); &t(O);}4748COMPLEX VARIABLESvoidMakePolar(pi,x,y,r,theta)/* Make polar coordinates from Cartesian coordinates */double pi,x,y; /* are input variables */double *r,*theta; /* are calculated variables */double angle;*r = sqrt(x*x+y*y);if ( x == 0 )angle = pi/2;else angle = atan(fabs(y/x)if (x>=O && y>=O) *theta =if (x<=0 && y>=O) *theta =if (x<=0 && y<=O) *theta =if (x>=O && y<=O) *theta =void);angle; /* first quadrant */pi-angle; /* second quadrant */pi+angle; /*third quadrant */-angle; /* fourth quadrant */MakeCartesian(r,theta,x,y)/* Make Cartesian coordinates from polar coordinates */double r,theta; /* are input variables */double *x,y; /* are calculated variables */*x = r*cos(theta);*y = r*sin(theta);}Now that we have a source program for converting between coordinates, it isappropriate to test and use it.Exercise 2.23Testprogram Cartesian & Polar Coordinate Interconversion for thetwo options by using as input values combinations of x and y or of r andthat correspond to points in each of the four quadrants and along the boundariesbetween quadrants, a total of 16 combinations.
Also verify that the program terminates if all of x, y, and r are input as zero. nYou now have two functions that are useful in converting coordinates. Extensionsof these programs may be used to interconvert spherical-polar and three-dimensionalCartesian coordinates.REFERENCES ON COMPLEX NUMBERS49REFERENCES ON COMPLEX NUMBERSBaldock, G. R., and T. Bridgeman, Mathematical Theory of Wave Motion, EllisHorwood, Chichester, England, 198 1.Ingard, K. U., Fundamentals of Waves and Oscillations, Cambridge UniversityPress, Cambridge, England, 1988.Pippard, A. B., The Physics of Vibration, Cambridge University Press, Cambridge,England, 1988.Wolfram, S., Mathenmtica: A System for Doing Mathematics by Computer, Addison-Wesley, Redwood City, California, second edition, 1991.50Previous Home NextChapter 3POWER SERIES AND THEIR APPLICATIONSThe goal of this chapter is to understand the analysis, numerics, and applications ofpower series, which form the basis for many of the powerful methods used in computing.
The chapter begins with a discussion of the motivation for using series,summarizing some properties and applications of the geometric series, then enunciating and proving Taylor’s theorem on power series. This general theorem is thenused in Section 3.2 to develop expansions of useful functions-the exponential,cosine, sine, arcsine, logarithm, and the hyperbolic cosine and sine. In this sectionthere are preliminary numerical and computing studies in preparation for the projectat the end of the chapter.The binomial approximation and various of its applications are described in Section 3.3, followed by a diversion (Section 3.4) in which we discuss iteration, recurrence, and recursion in mathematics and computing.
The computing project forthis chapter is testing the numerical convergence of the series for the functions considered in Section 3.2. References on power series then complete the chapter.3.1MOTIVATION FOR USING SERIES: TAYLOR’S THEOREMIn this section we review the properties of power series and we explore their numerical properties, especially their convergence. Since the geometric series is easy tohandle algebraically but also exhibits many of the problems that power series mayexhibit, we consider it first. Then we consider the very powerful Taylor’s theorem,which allows differentiable functions to be characterized by power series. A discussion of the interpretation of Taylor series then prepares us for the several examplesin Section 3.2 of expansions of interesting and useful functions.We emphasize the numerical convergence of series, resting assured by suchmathematics texts as those by Taylor and Mann (Chapters 19 - 21) and by Protterand Morrey (Chapter 3) that all the series that we consider are analytically conver5152POWER SERIESgent for the range of variables that we consider.
After you have understood the materials in this chapter you will be ready to use powerful algebraic and numericalcomputing systems, such as the Mathematica system described in Chapter 3.6 ofWolfram’s book, to perform accurately and efficiently much of the work involved inmaking expansions in power series.The geometric seriesThe geometric series provides a good example of a power series that can be readilyinvestigated and that illustrates many properties of series. We quickly review its formal properties, then discuss its numerical convergence properties and its relation toother functions.The usual definition of the geometric series to n terms is the function(3.1)in which the parameter r determines the convergence of the series. Although the results that we derive generally hold for complex values of r, we will discuss examples only for r a real variable.