Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Computing for physics

  1. Jul 17, 2006 #1
    i'm going to apply to do a physics degree, and at several open days it's been mentioned that it would be a good idea to have at least a basic knowledge of computing. i've never done any computing before, so what would be a good language to start with, that would probably be used?
     
  2. jcsd
  3. Jul 17, 2006 #2

    berkeman

    User Avatar

    Staff: Mentor

    C. It's universal, and will teach you many useful concepts. A good book to buy is "The C Programming Language" by Kernighan and Ritche (whatever the latest edition is -- avoid the 1st edition). You can download free C compilers from many places, and start programming!
     
  4. Jul 17, 2006 #3

    berkeman

    User Avatar

    Staff: Mentor

  5. Jul 17, 2006 #4
    do u write ur own comp programs
     
  6. Jul 17, 2006 #5

    berkeman

    User Avatar

    Staff: Mentor

    Are you posting on a PDA? If not, could you please pose your question as a complete sentence with capitalization and all that stuff? Thanks.
     
  7. Jul 17, 2006 #6

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I'd avoid Kernighan and Ritchie for a first look thru on C, it is very terse. I'd suggest getting the Schaum's outline in C++, they are fairly inexpensive and full of examples.

    I work in industry and we spend a fair amount of time writing VBA's for Excel to solve most of our problems.
     
  8. Jul 17, 2006 #7

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    K&R is perhaps the classic C reference, but it's not easy to read.

    Here are some good web tutorials:

    http://www.cprogramming.com/tutorial.html

    If you've never done any programming before, I'd also recommend that you not start with C. Python is a much easier language to handle for a beginning programmer, but will teach you all the same concepts.

    You can find tutorials for Python here:

    http://www.python.org/

    - Warren
     
  9. Jul 17, 2006 #8
    how different is C from C++?

    will i find it difficult to switch from python to, say, C (as some people have suggested)?
     
  10. Jul 17, 2006 #9

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    C++ is basically a superset of C. You'll need to be a bit further along in your programming career before you'll really want or need those extensions (except such things as input/output -- C++'s I/O is much easier to follow than C's).

    The concepts of all programming languages are virtually the same. Loops, variables, etc. are common to all languages. The biggest difference between Python and C is that C is a "strongly-typed" language, meaning that every variable can only hold a specific type of information. In C, a integer is very different from digit-string representation of the same integer. In Python, however, you can effectively forget about variable types. When you use a variable like a number, it's a number. When you use it like a string, it's a string.

    If you get used to such flexibility, C's very strict typing rules may seem a bit difficult and annoying.

    - Warren
     
  11. Jul 17, 2006 #10
    I Can recommend C++ Primer Plus as an introductory text, i found it very good.
     
  12. Jul 17, 2006 #11
    I second C++ Primer Plus: it's written for beginners, but comprehensive enough that it's still often the first book off the shelf to look something up. A good second book is "C++ in Action"; it has a strong emphasis on quality, incremental development, and avoiding the design mistakes that hackers make. Work through the projects in that book, and you'll be a better programmer than 90+% of physicists. My first C++ book was by Schildt (title?), it was pretty good.

    Once upon a time, the usual way of learning things was C first, then C++. There's no point doing that: the books above do a great job of flattening the learning curve, and gradually exposing beginners to more stuff. C++ has a lot of things like i/o streams that make life so much easier than the corresponding C stdio functions.

    I wish I had known about Python when I was learning OO.

    Whatever you do, be sure to write lots of programs. It's a craft, not a spectator sport.
     
    Last edited: Jul 17, 2006
  13. Jul 18, 2006 #12

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I agree with that statement, my programming skills degreade very quickly when I am not using them consistently.
     
  14. Jul 18, 2006 #13

    PerennialII

    User Avatar
    Science Advisor
    Gold Member

    Would also recommend that once have learned something well (like C++), systematically extend to other languages, scripting within major math packages etc (whatever means will turn out to be important in your field). At some point the pieces 'click' with any luck & form a bigger picture, enabling you to add a new 'degree' of expertise.
     
  15. Jul 18, 2006 #14

    rcgldr

    User Avatar
    Homework Helper

    First of all, in your case, for the purpose of physics, your programming will be mostly mathematical, although some graphics may be involved. Although expensive, high end math programming languages (actually more of a high end "tool") like MatLab are much more suited for the type of mathematical usage you'll find. MathCad is cheaper, but not as powerful. MatLab is a very high level tool, that hides much of how computers are programmed. There may be a student version of MatLab that is relatively cheap.

    Matlab can be used interactively. You enter a 3rd order equation, and it will solve it for you, including complex number results, plot it for you as well, and/or show the 3 solutions on a circle on a graph with real and complex axis. To do the equivalent in C or C++ would take hours of programming.

    C is considered a mid-level language; programming in C is closer to programming a computer directly in it's native assembly language than a high level language like the classic high level languages Fortran (still used by NASA) or Cobol (still used for data processing environments like banks on main frames), and very high end tools like MatLab.

    C++ is a step up from C, but most of the gain is due to a lot of pre-written code, for example, in the case of Windows, C++ includes default code for dealing with the windows environment, and you program routines that supercede the default routines included in the C++ windows code.

    A C structure is an organized group of variables, but one variable type is a pointer to function (it's address). A C++ structure is called a class, and deals with functions more directly rather than as pointer to function variables. In C++, you can "supercede" the default functions in a class. In C to do the equivalent, you'd have to re-assign a new address in a pointer to function. C++ internally renames your functions to include the functions inputs and outputs, so you can have two functions with the apparent same name, like a function called "Add", one that would add integers, the other that would add floating point numbers.

    This is probably going beyond what you'll need as "basic understanding" of computers. Personally I wouldn't recommend using C or C++ for mathematical oriented code. C++ is good for windows programming, but even in this case, it's quite common for a lage application to only use C++ for the windows interface, and then use C for the heart (generic) of the application.

    If your goal is to learn how to program, then C or C++ is good. If your goal is to use a computer as a tool to help you solve mathematical problems, then MatLab is a much better choice.
     
    Last edited: Jul 18, 2006
  16. Jul 18, 2006 #15

    J77

    User Avatar

    I started with Fortran90 at Masters level.

    Then wrote code in C++ for my PhD.

    Now, I mainly use Matlab.

    Of course, C/C++ compilers are free and the skills you learn will be more general. However, you may not use it as a student - ime, mass classes are preferably taught with simething like Matlab.
     
  17. Jul 19, 2006 #16

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I worked witha guy who called MatLab "C Light", nearly the same syntax and much more powerful to work with. We did a test, wrote a piece of code in C than the corresponding code in MatLab, the MatLab code ran about 50% faster on the same computer because MatLab was written for use with matricies and we didn't have to explicitly code in all of the loop structures like you do for C. I ginned up some MatLab code last week to quantatatively (sp) show my boss that an issue with one of our vendors measurement apparatuses could indeed be a major problem for us. It took mew about an hour to program, check and plot the data and would have been a much longer project writting either Fortran or C.

    I work in industry with a multitude of codes, Fortran (77 and 90), C/C++, MatLab, when I get to a *nix box, I tend to use awk, sed and the other shell scripting languages like the Korn and Borne shells to manipulate tons of data. The nice thing about using a *nix box is that we can script thru a couple of days of range data in a couple of hours to make sure we are getting what we think we should be getting and in the best case scenario, we can run our plotting routines in conjunction with our prediction codes at the same time and complete most of our analysis on the fly.

    If you are looking for an easy learning curve, learn MatLab and you can be up and running in about a day getting some serious work done. There is a clone called OCTAVE for a Linux box if you have access, it is nearly as functional and coupled with another plotting program absolutely a joy to work with. Octave does not have all of the plotting capability that MatLab has but they are working it slowly. I just write the data to a file then use gnuplot to get it into a decent form to pass of to someone else.
     
  18. Jul 22, 2006 #17
    The most obvious difference is that C++ supports object-oriented (OO) programming. An object is a combination of code and data. Only the object is allowed to modify its data (called the object's state); other code has to use the object's interface functions to modify the data. This is called encapsulation.

    By giving the object control of its state, encapsulation tames one of the mad monsters of large code projects: data getting changed unexpectedly. A second pleasant effect is that other code need not know how the object does its work: the object's state is hidden behind the interface. This increases modularity, which means code is easier to test, reuse, replace with other methods, etc.

    These and other traits make OO very useful on larger code projects, including scientific and engineering projects. OO is not idiot-proof, but done well, it can make it much faster to write and test complex codes. That's why OO really took off in the 90's.

    Since most scientific programs spend most of their time in a relatively few lines of numerically intense code, OO apps can be as fast non-OO apps. Since this universe's boundary conditions allow bad programmers, OO apps can be significantly worse-performing than non-OO.

    Python also has decent object-oriented support, much cleaner than C++.

    There are lots of other differences between C and C++, but OO is the biggest. C++ was originally called "C plus classes". (An object's type is called its class.)

    Good luck,
    Tim
     
  19. Jul 22, 2006 #18
    I agree. All-in-one computing environments like Matlab are a good way to get up and running. Some of them have discounts for students.

    Mathematica is another example; its strength is symbolic computing and numerical approximations to integrals, diff-eq's, etc. It's probably a little harder to learn, as its core language is more abstract.
     
  20. Jul 22, 2006 #19
    Yes but you dont have to start from scratch with C or C++, you can use open libraries of maths functions. No doubt, you dont mention what algorithms you used in the test, you could use a C++ library that is as fast as matlab, but still be much more open (less restricted).

    Im not knocking matlab, just saying that the test you describe doesnt really justify choosing matlab over C/C++, at least not to me.
     
  21. Jul 22, 2006 #20

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    I'm not trying to get someone to choose one over the other, just that there are other choices out there. I cannot describe the test in detail because it is coprorate limited, i.e. it provides a competative advantage over our competitors, i.e. even my funding agents are unaware of the full extent of our testing.

    Amongst the people who were geting their PhD's at the same time I did, at my alma mater there was a 3 way tie betwen Fortran, C/C++ and MatLab to do the modelling for their dissertations.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?