Learn Computing for Physics Degree - Best Language to Start

In summary, it has been recommended to have a basic knowledge of computing for a physics degree. The programming language C has been suggested as a good starting point, with a free compiler available for download. Other suggested languages include Python and C++, which offers extensions and more flexibility. It is also recommended to write lots of programs to improve programming skills. For mathematical usage, high-end tools like MatLab are recommended, as they can quickly solve equations and provide visual displays of results. C is considered a mid-level language, while C++ includes pre-written code for specific environments, such as Windows.
  • #1
dt19
47
0
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?
 
Technology news on Phys.org
  • #2
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
do u write ur own comp programs
 
  • #5
physicsmasta said:
do u write ur own comp programs
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.
 
  • #6
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.
 
  • #7
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
 
  • #8
Dr Transport said:
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.
.

how different is C from C++?

will i find it difficult to switch from python to, say, C (as some people have suggested)?
 
  • #9
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
 
  • #10
I Can recommend C++ Primer Plus as an introductory text, i found it very good.
 
  • #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:
  • #12
nmtim said:
Whatever you do, be sure to write lots of programs. It's a craft, not a spectator sport.

I agree with that statement, my programming skills degreade very quickly when I am not using them consistently.
 
  • #13
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.
 
  • #14
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.

dt19 said:
how different is C from C++?
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:
  • #15
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.
 
  • #16
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.
 
  • #17
how different is C from C++?

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
 
  • #18
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.

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.
 
  • #19
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.

Yes but you don't have to start from scratch with C or C++, you can use open libraries of maths functions. No doubt, you don't 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 doesn't really justify choosing MATLAB over C/C++, at least not to me.
 
  • #20
3trQN said:
Im not knocking matlab, just saying that the test you describe doesn't really justify choosing MATLAB over C/C++, at least not to me.

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.
 
  • #21
doesn't C/C++ have a longer learning path? as a student i think you should start with C because of this longer learning path...In most other languages many things are precoded for you. Though you can find the same packages for C?

nmtim: i thought OO design can also be done in C via function pointers and #defines. Though i don't think C can handle Inheritance/Polymorphism which i think would be the biggest difference between C/C++Dr.Transport: Curious to know if you do High performance computing on Supercomputers?
 
  • #22
neurocomp2003 said:
i thought OO design can also be done in C via function pointers and #defines.

Yes, in principle you can do that. But the C support is weak to non-existent--you'd have to implement almost everything yourself. Why do that when you can take advantage of built-in language support and 20 years of compiler dev?

As to learning path, you don't need to learn all of C++ to be a productive user of it. You can learn a very useful subset of C++, including basic OO, in not much more time than it takes to learn C. True, it takes takes much longer to become a guru with C++ than C.

I suspect it's easier to teach OO to a programming novice than to someone with 10 years of procedural programming (C, F77, Matlab, IDL) experience, as it involves a different set of intuitions about how a program works. Better to build that up from the beginning.
 
  • #23
neurocomp2003 said:
Dr.Transport: Curious to know if you do High performance computing on Supercomputers?

Used to, I would run my codes on my laptop using my F90 compiler and IMSL, debug and all that jazz, then FTP to a Cray 90 machine and tun the entire code in its full glory...Now I run everything on eithre a *nix box or PC.

I guess you could I used to do High Performance Computing, my advisor always told me to sweat the small stuff, it would bite you in the butt. I worried about the 6th decemal on my PC and the 20th on the Cray...
 
  • #24
i'm, being taught a bit of microsoft visual basic at my work experience, would i find it difficult to adjust to, say, C++ once I've done more in depth VB stuff?
 

FAQ: Learn Computing for Physics Degree - Best Language to Start

What is the best programming language to start with for a physics degree?

The best programming language to start with for a physics degree is typically Python. It is a versatile and easy-to-learn language that is widely used in scientific computing and data analysis. It also has a large and active community, making it easier to find support and resources.

Do I need to have prior programming experience to learn computing for a physics degree?

No, prior programming experience is not necessary. While some basic knowledge of coding concepts can be helpful, most introductory courses assume no prior programming experience and will teach you everything you need to know.

How will learning computing benefit me in a physics degree?

Learning computing will be extremely beneficial in a physics degree. It will allow you to perform complex calculations and simulations, analyze and visualize data, and automate repetitive tasks. These skills are essential in modern scientific research and will make you a more competitive and efficient physicist.

Are there any other programming languages that are useful for physics?

Yes, there are other programming languages that are useful for physics, such as MATLAB, Fortran, and C++. However, these languages are typically more specialized and may require more advanced programming skills. It is still recommended to start with Python as it can be more easily applied to a variety of scientific fields.

Can I learn computing for a physics degree on my own, or do I need to take a course?

It is possible to learn computing for a physics degree on your own, but taking a course can be more beneficial. A structured course will provide you with a solid foundation and guide you through the learning process. It will also offer opportunities for practice and feedback, which can be helpful in mastering the concepts and skills.

Back
Top