Document Sample
ME Powered By Docstoc
					Find a root for projectile funct using bisection method in range [60, 70] */
                                            double xleft, fleft;      /* x and f(x) on the left of the root */
/2.0; <stdio.h>
            /* mid point */                                        xmid = (xleft+xright)/2.0;      /* mid point */
                                            double xright;             /* x on the right of the root */
clude <math.h>         /* for fabs() */
            /* f() in the mid point */                             fmid = func(xmid);              /* f() in the mid point *
                                            double xmid, fmid;         /* x and f(x) on the mid of xleft and xright */

          // C
clude <float.h>       /* criterion to terminate loop */
lerance) {/* convergence for FLT_EPSILON */                        while(fabs(fmid) > tolerance) {/* convergence criterion t
                                            double tolerance;          /* convergence tolerance */
fine determine the updating of left or acceleration */
) to M_G   9.81       /* gravitational right point constant g */       /* f(xleft)*f(mid) to determine the updating of left
                                            double x0 = 60.0, xf = 70.0; /* initial end points */
.0) {      /* mid point becomes the right point */                     if(fleft*fmid <= 0.0) {    /* mid point becomes the r

           /* update the right point projectifle */
function for the y-coordinate of the */
;                                                                          xright = xmid;          /* update the right point
                                           /* initialize xleft and xright with initial end points */

          // for the
ble func(double x) {                                                  }
                                           xleft = x0;
double theta0,        /* initial velocity */
           /* mid point becomes the left point */                     else {                       /* mid point becomes the
                                           xright = xf;
                      /* projection angle */
       v0, /* update the left point */                                     xleft = xmid;           /* update the left point
                      /* y coordinate */
       y; /* update f(xleft) */
                                                                           What do you teach if the ME
                                           /* use machine epsilon for float type as convergence tolerance */
                                                                           fleft = fmid;          /* update f(xleft) */
theta0 = 15*M_PI/180; /* set the projection angle in radian */        }
                                                                           curriculum allows only 10

          // Course
v0 = 35.0;             /* set the initial velocity */
ght)/2.0; /* update the mid point */
                                                                           weeks to devote to computer
                                                                        xmid = (xleft+xright)/2.0; /* update the mid                   point *
y          /* f() - the mid point */                                                                /* f() in UC
                                                                           programming? This is why the mid
                                            /* The signs of f(xleft) and f(xright) must be different
; = tan(theta0)*x in M_G*x*x/(2*v0*v0*cos(theta0)*cos(theta0));         fmid = func(xmid);                                             point *
return y;                                                                  Davis made its choice.
                                                to guarantee there is a root */
                                            fleft = func(xleft);            /* calculate f(xleft) */
c(x) = %g\n”, xmid, fmid);                                                 By %f m, H. Cheng
                                                                    printf(“x =Harryfunc(x) = %g\n”, xmid, fmid);
                                                  if(fleft * func(xright) > 0.0) {      /* invalid x0 and xf */
                                                                          return 0;
                                  riting computer programs to solve invalid end points\n”);
                                                                      } Fortran in the mechanical engineering curriculum.
                                  complicated engineering problems
                                                     return -1;           /* four years, it was apparent that and xright */
                                                                         Afterreturn -1 for invalid xleft our students’ over-
                                and to control mechanical devices
                                                  }                        all programming skills had declined significantly in com-
                                is a basic skill all engineers must        parison with previous students who had learned Fortran.
                                master, so most colleges require all       Our new graduates could not handle some of the senior
                                undergraduate engineering students         design projects sponsored by our industrial partners or
          to take some computer programming courses.                       other real-world programming tasks.
            Yet the number and type of courses they take vary                For example, I supervised a senior project sponsored by
          widely. For example, my school, University of Califor-           Lockheed Martin to design a prototype robotic system to
          nia, Davis, requires electrical engineering students to          gather rock and soil samples on Mars. The sponsor speci-
          take four courses related to computer programming:               fied an Atmel 8-bit microcontroller, which can only be
          Assembly for hardware interface, C for structured pro-           programmed using either C or Assembly language. Of the
          gramming and hardware interface, Matlab for numerical            four students on the team, only one student who took a C
          computing, and C++ for object-oriented programming.              programming course offered to computer science students
            This is not the case for mechanical engineering stu-           could program the microcontroller for system integration
          dents. Because the curriculum is already bulging with            and testing.
          requirements, most universities mandate that mechanical            Our department also noticed that graduate students
          engineering students take only one introductory course           who learned only Matlab had difficulty handling research
          in problem-solving programming.                                  projects that involved serious programming. As a result,
            But which language should students learn? As more              in 2003, we redesigned our curriculum to offer a new
          and more mechanical devices add integrated electronic            course that combined C programming with an intro-
          controls, the choice of programming language becomes             duction to Matlab.
          more pressing than ever before. Which language is the
          best, or the most popular, or the most useful in a future         // Why C?
          career? The question evokes contentious, often emotion-          There were many reasons to go with C. We all believed
          al faculty debates of near-religious intensity.                  that an introductory course in programming for prob-
            UC Davis’s mechanical engineering department went              lem solving should use a non-proprietary programming
          through this kind of debate twice in the past decade,            language that adheres to an international standard. Stan-
          and we learned many painful lessons from the expe-               dard programming languages, such as C and Fortran,
          rience. In 1998, Matlab, a mathematical program-                 are supported and maintained by all major computing
          ming language with strong visualization capabilities,            industry companies.
          replaced Fortran, a more traditional language, as one              A standardized programming language is stable and its
          of the four required programming courses in the elec-            evolution is overseen by a technical standards committee
          trical engineering curriculum. In a move championed              made up of business, academic, and organizational repre-
          by an upper level administrator, Matlab also replaced            sentatives with a stake in the language. For example, the
                                                                           first C standard, C89, was released in 1989 by the ANSI
          Harry H. Cheng is a professor in the Department of Mechani-      X3J11 and ISO S22/WG14 C Standard Committees. The
          cal and Aerospace Engineering at the University of California,   second (and latest) C standard, C99, was ratified in 1999
          Davis, and an ASME Fellow                                        and added many new features, such as complex numbers,

          2 mechanical engineering | September 2009

to terminate loop */
            variable length arrays for numerical computing, and sup-         C excels as a model programming language. It does an
          port for 64-bit
 or right point */           computing. All variations of C that con-      excellent job of illustrating the underlying working prin-
right       form
        point */ to the C99 standard contain these features.               ciples of scientific computing and disciplined software
              As a language, C continues to evolve but remains back-       development. Students gain valuable knowledge of such
t */
            ward compatible. As long as it conforms to the C99 stan-       fundamental programming concepts as data types, com-
            dard, a compiler (which translates high-level C programs       piling, linking, optimization, and static and dynamically
 left       into
        point */ executable code) will work with any program writ-         shared libraries. Studying C provides a solid foundation
            ten in C89. And C is unlikely to disappear.                    for students who want to learn advanced programming
              Matlab, on the other hand, is a proprietary mathematical     skills, such as object-oriented programming in C++.
            programming language controlled by a single company,             Equally important, users can either compile or interpret
            MathWorks Inc. This makes it harder to share informa-          a C program. Compiling translates a C program into
            tion with colleagues not running the same software.            binary code and saves it as an executable file that runs on
              Academic institutions have a mission to teach technolo-      a specific processor. Interpreting executes C statements
*/          gies that are widely used in the real world so that students   one at a time. C interpreters let students execute a sin-
            have the skills that employers need. More than 90 percent      gle line of statement or a script file composed of several
            of the programs running on our desktops, from operat-          statements without compilation. This provides immedi-
            ing systems and e-mail clients to Web browsers and word        ate feedback, and makes them powerful tools for interac-
            processors, are written in C or its relative, C++.             tive classroom presentations and for learning basic pro-
              Just how widespread is C? There are many measures of         gramming concepts. Some C interpreters also contain
            programming language popularity.                               graphical plotting and advanced numerical computing
              The Programming Language Popularity Web site                 capabilities typically found in mathematical program-
            ( and Programming Community                    ming languages, for conveniently solving and visualizing
            Index ( are two of the best known and            engineering and science problems.
            most authoritative statistical sources.                          Granted, C has a reputation for being difficult to learn
              The Programming Community Index has been pub-                and even more difficult to master. Yet the language is small
            lished monthly since 2001 by Tiobe Co., a software con-        enough to teach in a one-term course. Once students have
            sultant. The June 2009 index indicates that Java, a lan-       learned C, they can pick up other languages and math-
            guage used to program Web sites, holds the top spot with       ematical programming languages without much difficulty
            a 20.1 percent share of the market. Next comes C at 16.8       because all other modern languages and mathematical pro-
            percent, followed by C++ with 10.6 percent. Matlab             gramming languages borrowed heavily from C. On the
            ranks 23rd, with a 0.5 percent share, and Fortran 24th,        other hand, students who have learned Fortran or Matlab
            with a 0.4 percent share. Since Java is based on simpli-       will still find it difficult to learn C by themselves, especially
            fied C language syntax, students with C programming            if they want to write serious software programs.
            experience can compete for a broad range of jobs. In the
            Programming Language Popularity Website, C tops the             // BilingUal aDvanTagEs
            list, while C++ is fourth. Fortran is No. 21 and Matlab        Teaching C by itself, however, may not be truly suffi-
            is nowhere to be seen.                                         cient for today’s mechanical engineers. Just as learning
              C, originally invented to write the Unix operating system,   foreign languages helps students understand their own
            runs on all platforms, from microcontrollers and hand-held     language better, learning C with other languages can
            devices to PCs and supercomputers. It is the language most     shed light on the fundamentals of computer program-
            often used to write operating systems and major applica-       ming. Besides, some languages are more suitable for cer-
            tions, as well as modify hardware and software. Users can      tain applications.
            also translate most other languages into C.                      At the University of California, Davis, we teach C with
              C is especially useful for mechanical engineers because      Matlab and provide optional introductory materials for
            it is the language of choice for hardware interfaces. Often,   Fortran so students can learn Fortran on their own.
            the only other alternative is tedious, low-level program-        Matlab is an interpreted language, with all the flexibil-
            ming using an assembly language. As a hardware interface       ity that implies. It finds many applications in scientific
            language, C is used commonly for data acquisition and          and engineering analysis. Like other commercial mathe-
            real-time control of robots and other mechatronic sys-         matical programming packages, such as Mathematica
            tems. Many hardware and software vendors even provide          and Maple, Matlab has graphical plotting and advanced
            libraries of C code for hardware interfaces. C is also the     numerical features that make it a powerful system for solv-
            most widely used language for programming embedded             ing and visualizing engineering and science problems.
            processors, which control everything from automobiles,           Matlab’s strength is its numerical computing and array
            industrial machinery, medical equipment, and household         processing capability. Most students, however, take intro-
            appliances to traffic lights, vending machines, cell phones,   ductory programming as freshmen, before they learn line-
            and toys. Of the 9 billion microprocessors manufactured        ar algebra, so they cannot fully appreciate the language’s
            in 2005, 8.8 billion were embedded into products.              numerical power. Still, we believe that if we expose them

                                                                                                   September 2009 | mechanical engineering 3
 ile(fabs(fmid) > tolerance) {/* convergence criterion to terminate loop */                fmid = func(xmid);                 /* f() in th
ate the mid point */                              xleft = xmid;           /* update the left point */
   /* f(xleft)*f(mid) to determine the updating of left or right point */                  while(fabs(fmid) > tolerance) {/* convergenc
 in the mid point */                              fleft = fmid;          /* update f(xleft) */
   if(fleft*fmid <= 0.0) {         /* mid point becomes the right point */                      /* f(xleft)*f(mid) to determine the upda
                                                                                                if(fleft*fmid <= 0.0) {      /* mid point
d, fmid); to Matlab or similar mathematical computing environ-            /* update the mid point features of Matlab by hav-
                                             xmid = (xleft+xright)/2.0; the strength and some unique */
                                                                                                     xright = xmid;           /* update th
            ments early, students will be able to apply the knowledge
                                             fmid = func(xmid);
                                                                                            to re-solve */
                                                                        ing students use it mid point many of the same problems
                                                                          /* f() in the
            to solve complicated engineering problems when they                                  in
                                                                        that they solved earlier} the course in C.
            take advanced math and engineering courses.                   We cover a lot of ground in 10 weeks. That means we
                                         printf(“x 1950s,      remains
             Although Fortran dates back to the = %f m,it func(x) =          must make fmid);
                                                                           %g\n”, xmid, compromises.       Due to the time constraints,
           one of the primary languages used by professionals in
                                         return 0;
                                                                             we teach students both C and Matlab, and give them
           both academia and industry, especially for such com-              handout materials to learn Fortran as a second program-
           putationally intensive programs as computational fluid            ming language on their own. Academic institutions that
           dynamics. It is one of the best candidates for mechanical         use a 15-week semester system should have enough time
           engineering students to compare with C.                           to introduce Fortran in class for comparison study with
             C99 has features, such as restricted pointers, that enable      C. They may also have time to cover some of the more
           it to be optimized as efficiently as the equivalent Fortran       difficult C/C++ features.
           programs. C99 also supports the complex numbers and                 Since 2004, I have been teaching freshmen how to
           variable length arrays found in Fortran that are so useful in     use C and Matlab to solve engineering problems. Our
           engineering and science applications. These changes have          department expects students who complete the 10-week
           led to growing use of C for numerical intensive super-            course not only to solve problems in C, but also to
           computing. The latest Fortran 2005 standard added many            achieve greater proficiency in Matlab than those students
           new features, some of them borrowed from C, though                who learned only Matlab. We also expect students who
           these broader capabilities make the language even more            would like to work on Fortran projects to pick up that
           difficult to cover in a single course.                            language quickly on their own.
                                                                               After five years teaching, we believe that we have achieved
            // TEaChing C anD MaTlaB                                         this goal. Students demonstrate proficiency by applying C
           An introductory computer programming course should                and Matlab to projects and homework in subsequent cours-
           focus on using computer programs to solve problems.               es. They are also able to port Fortran programs used in an
           Our goal is to teach students the fundamentals of doing           upper division applied aircraft aerodynamics course to C.
           this in C because it is widely used in engineering and              They also approach the programming and mechatron-
           because it provides an excellent foundation for solving           ics aspects of mechanical engineering differently. Many
           problems with other programming languages.                        developed a keen interest in the application of infor-
             Our introductory computer programming course at                 mation technology to mechanical devices. Others per-
           Davis runs for one quarter, or 10 weeks. The weekly               formed very well in the advanced C++ object-oriented
           schedule includes three one-hour lectures and a one-              programming course offered primarily to computer sci-
           hour discussion session. We offer three optional labora-          ence and electrical engineering students.
           tory hours with a teaching assistant in the lab to answer           C is one of the most popular programming languages
           students’ questions. Although we provide open lab hours           used in engineering and science. It is the only language
           (without a teaching assistant), most students do home-            for many programming tasks. C is one of the core foun-
           work assignments on their own computers using free C/             dations of information technology. As the pace of inte-
           C++ compilers and interpreters with graphical plotting            gration of electronic controls with mechanical devices
           and advanced numerical capabilities running in Win-               increases, it is more important than ever for mechanical
           dows, Mac OS X, or Linux.                                         engineers to master this fundamental technology.
             This demands a lot from students. In order to cover               With a solid foundation in C, mechanical engineering stu-
           adequate material, we rely heavily on information tech-           dents are well prepared for today’s projects, which increas-
           nology for teaching. We use a combination of Power-               ingly integrate mechanical hardware with control software.
           Point slides and live interactive code executed in a C/           They have the foundation to learn more advanced and
           C++ interpreter with graphical plotting and advanced              mathematical programming languages, as well as to take
           numerical capabilities for classroom presentations. Stu-          advantage of new and emerging computing paradigms. n
           dents learn the fundamentals of disciplined software
           development in C from examples that range from algo-
           rithm development, flow charts, different data types,
           and structured and modular programming to pseudo-                   To Learn More
           code, code reuse, coding style, on-line documentation,
           and debugging.
                                                                               For more detailed information about UC Davis’ intro-
             Matlab was implemented in C and borrowed heavily from
                                                                               ductory computer programming course and a review
           C. Like most modern languages, it shares similar opera-             of our teaching resources, visit our Web site at http://
           tors, expressions, repetition statements, control structures, The site includes a textbook,
           arrays, input and output, and functions. Because they start         detailed lecture notes, PowerPoint lecture and discus-
           with a solid foundation in C, students quickly learn to use         sion slides, homework and solutions, and handouts for
           Matlab to solve engineering problems. We demonstrate                learning Fortran and comparing it with C.

           4 mechanical engineering | September 2009

Shared By: