Programming Basics for a Physics Major

<p>I'm a sophomore physics major. I consider myself computer savvy but I've never had a reason to study programming until I became interested in physics. So I have some simple questions:
1) How can I learn how to program? What language should I start with? I have a Mac, if that matters.
2) What languages should I be familiar with by the time I graduate?
I know this isn't really on the topic of college admissions, but you're a good crowd to ask. Thanks!</p>

<p>C or C++ or matlab?I don't know..</p>

<p>In physics you really need to embrace programming. Most physics guys at my school ate CS minors.</p>

<p>I'm spending the summer at Fermilab and the all physicists here write computer programs do to particle identification, simulations, or some other calculation. From my experience here so far, it seems important to know at least C/C++ and shell scripting for this field.</p>

<p>Programming is programming. But there's two basic flavors. For most scientific programming, it will be done in routine/procedure based languages. These will be something like FORTRAN. </p>

<p>Then there's objected oriented programming. I haven't seen a lot of scientific applications with OOP languages, but if you want to be a good programmer, then it's probably something you should learn as well. Java is certainly one of the more popular OOP languages. </p>

<p>It's also important to learn some script languages. Perl is a good choice there. </p>

<p>And finally, for everyday scientific/math computing, MATLAB is a god-send.</p>

<p>So, I should specifically firstly learn FORTRAN 90/95, Java, Perl, and MATLAB in that respective order?</p>

<p>Sakky, also, what do you think about this? <a href="http://www.dspguru.com/sw/opendsp/mathclo2.htm%5B/url%5D"&gt;http://www.dspguru.com/sw/opendsp/mathclo2.htm&lt;/a&gt;&lt;/p>

<p>Well, in terms of order, I would recommend:</p>

<p>MATLAB, Fortran, Java or C++, and then Perl. </p>

<p>I would say those would be the most useful during undergraduate work. Throughout undergraduate and graduate school, I used MATLAB on a daily basis. I think that's going to be the most useful to engineering/physics majors. For actual industry work, I found Perl to be extremely helpful. It's abilities to script and post-process analysis codes was incredibly useful. Perl has a slightly intimidating syntax. However, it's also a great tool to learn regular expressions. It's available on most Unix/Linux machines (which makes it a useful/common skill) and you can easily download a Windows Perl package. </p>

<p>FORTRAN is also useful in industry work (if your industry uses legacy codes like FORTRAN). As far as version, I would say version F77 is much more common and prevalent than F90. The simple fact of the matter is that very few people took the time to upgrade to F90, and not many programs start in FORTRAN these days. However, as a procedural language, it's probably the most useful to learn. If you're already a programming expert, there's probably no reason to learn FORTRAN for it's own sake.. but to learn programming, if you can master FORTRAN, than most other procedural languages will be a breeze. </p>

<p>Learning a modern language like Java will probably be the least useful for someone during undergrad (in terms of daily usage), but it's a good skill to have. Once you understand object oriented programming, then it's easy to move to other languages.</p>

<p>With regards to that MATLAB clone, I don't think that's a great idea. You can buy the student edition of MATLAB and Simulink for about $100 from your school. While it won't have all of the fancy toolboxes, it will certainly be sufficient for everyday use. If you want to learn some of the functions in the expensive toolboxes, go use the computers at your department's lab, I'm sure they have fully loaded Matlab versions. </p>

<p>BTW, although I'm honored by the reference, I'm not Sakky. ... Unless your comment was just a random call for him to enter the conversation. :)</p>

<p>What the heck? I guess I conjured up "S[ak]ky" out of my peripheral vision. Haha. My bad. I'll look into learning MATLAB first and getting it somehow.</p>

<p>Thanks, I'll take a look at MATLAB. Is that preferred over Mathematica?<br>
I'd never heard of Fortran. C++ seems much more popular, but maybe that's because it has non-scientific uses.</p>

<p>MATLAB I think is more prevalent and preferred in engineering. Mathematica probably has a better symbolic math interpreter. Mathematica also seems to make prettier plots and figures. It certainly has a nice interface, but I personally find that Mathematica has a steeper learning curve. MATLAB is widely used in academia and industry, I would guess by a larger margin than Mathematica. MATLAB programming is just much easier in my view. </p>

<p>FORTRAN was <em>the</em> technical computing language back in the day (1950s-1980s). A lot of programs were written in it, and there are still plenty of legacy programs (especially in fields like aerospace) that still use FORTRAN based programs. Since it still has a lot of applications, learning FORTRAN won't be bad idea. You can download a free FORTRAN compiler online (for example, <a href="http://www.gnu.org/software/fortran/fortran.html)%5B/url%5D"&gt;http://www.gnu.org/software/fortran/fortran.html)&lt;/a>. If you can write a program from scratch in FORTRAN, then you will have a good idea of how to program using a traditional procedural language. There are other languages from this era, like COBOL, but FORTRAN would be the best of the old-school languages to learn. </p>

<p>C++ is certainly more popular. I'd suggest either that or Java for learning OOP. Most scientific programming lends itself better to procedural languages rather than object oriented languages. Usually things like applications are better suited for OOP languages. </p>

<p>One of the things you can do is to find algorithms and implement them in either MATLAB or FORTRAN. Pick something like Newton's method or Golden Section Search and implement them on a simple function like Y=X^2 or something. The good thing about MATLAB is that it is extremely high level, and eliminates having to write subroutines to do stuff like finding the minimum of a function. However, you can also find existing FORTRAN math libraries out there that you can include in your programs. There are also dozens of numerical "cook books" about how to write these types of functions in FORTRAN.</p>

<p>I'm in chemical engineering the we were taught MATLAB. There's already a lot of built-in functions so it's quite useful.</p>

<p>Your first language shouldn't be your focus, because once you learn how to program, you can pick up other languages quite easily.</p>

<p>Steevee, that's what I was thinking, too, in the back of my mind. I need to learn how to program, the mechanics or it, and what not. How do I go about doing that?</p>

<p>As far as learning programming concepts, MATLAB is not that great of a tool. What you gain in terms of utility, you lose in generality. MATLAB is an interpreted language, and should be more appropriately called a script. It's also a proprietary language, so it has unique constructs that aren't useful in other languages. </p>

<p>It's also easy to get spoiled with how easy things are in MATLAB. But that makes it incredibly powerful. From the name, it's obviously great at matrix/vector based mathematics. From a simply A*B command in MATLAB, would have to be an entire subroutine (and a somewhat complicated one at that) to perform a matrix multiplication given two matrices of arbitrary size). [As a note, I think that would be a great exercise for you to learn if you want. Use FORTRAN to write a program that will do matrix multiplication on two matrices of arbitrary size. You will learn all about recursion, variable dimensioning, and other useful stuff.]</p>

<p>Personally, I think it's better to start at the foundations of programming. </p>

<p>Use FORTRAN to teach you things like how to define programs, write and call subroutines, declare variables, compile programs and some programming concepts like recursion. </p>

<p>Use C++ or Java to teach the basics of object oriented programming. It's significantly different in concept, so it's something worth learning. </p>

<p>Use a scripting language, like Perl or Python, to learn how scripts work. I think Perl is great for learning Regular Expressions. Once you learn regular expressions as a concept, it becomes much more powerful and useful to you. </p>

<p>MATLAB certainly has some of the elements from different languages, but in terms of learning programming basics, I don't think it's all that great. Certainly learn it because it will become a powerful tool for you (like a calculator can be). </p>

<p>As another suggestion, I know that many schools use Scheme to teach programming. It is based on the language Lisp. It's probably not very useful on it's own, but might be a good language to play with. <a href="http://www.drscheme.org/%5B/url%5D"&gt;http://www.drscheme.org/&lt;/a&gt;&lt;/p>

<p>Or.. you could just take the intro to programming class at your school. :)</p>

<p>sky expressed in words for me the electrical signals going on through my head but couldn't quite translate it into English. This last post is definitely very good advice.</p>

<p>Sky's advice sounds good to me. </p>

<p>I should point out that engineers typically use MATLAB while non-astronomer physicists typically use Mathematica. Astronomers use Interactive Data Language (IDL), which is similar to MATLAB. Also, your physics lab course will probably make you learn LabVIEW, a sort of visual programming language.</p>

<p>So, learn the foundations of programming, then FORTRAN, then an OOP such as C++ or Java?</p>

<p>
[quote]
So, learn the foundations of programming, then FORTRAN, then an OOP such as C++ or Java?

[/quote]
</p>

<p>Yes, except I meant that you should learn the foundations of programming while using some of these languages. The languages you chose should be ones that you expect to use in your coursework or in industry. It might take a bit of research to see what these languages would be. I gave my suggestions based on my own experience, but it will vary. If you want to get into stuff like real-time computing, then perhaps you might want to learn a language like Ada. Each language has their own unique constructs and syntax, but as one of the previous posters said, once you know a few languages, picking up others will be really easy. </p>

<p>As I explained in my earlier post, each language will have some features that will teach you basic programming concepts. FORTRAN is useful, and simple enough where you can learn the basics, like variables, data structures, memory allocation, functions, subroutines, recursion, and how to actually create programs by compiling them and getting objects and executables. Any number of procedural languages can teach you the same concepts. You could also learn C or BASIC, they are generally in the same class of languages as FORTRAN. FORTRAN was optimized for technical computing, so that's why I recommended it. But you could learn C and get the same concepts. </p>

<p>It will take an OOP language to learn concepts like objects, classes, methods, inheritance, etc. I don't think you can separate the learning the foundation stuff from learning the language. Until you sit down and try to implement a program, all these terms won't really mean anything to you. </p>

<p>A script language is usually just a watered down version of a full programming language. It's not quite as powerful, or fast, but it offers very fast development time and usually just facilitates/automates a process. It's not compiled, so it needs an interpreter running on a machine to run it. MATLAB is a script language that interfaces with the MATLAB kernel. .. so all the MATLAB "program" is doing is calling MATLAB's built in functions with some typical programming constructs like loops and logical statements around them. </p>

<p>I'll also redirect you to the Dr.Scheme reference I posted earlier. I think that might be a good place to start. I'm not a huge fan of the language or its syntax, but it's easy to get past that. One of the reasons I recommend it is because it comes with a companion online book. <a href="http://www.htdp.org/%5B/url%5D"&gt;http://www.htdp.org/&lt;/a> That will be a quick way for you to get started and follow a somewhat more traditional 'introduction to programming' type course.</p>

<p>I sprang for Mathematica. It's provided free to faculty and installed on most of the computers at Amherst, so maybe I'll have more support with it. The equation solving and graphing is super. I'll attempt some programming tonight and will actually follow your advice next and try Fortran. Really appreciate your explanation of all the different options.</p>

<p>I know this isn't a very popular thing to say among the scientifically-inclined, but you're better off learning c/c++, java, mathematica, and other even higher-level interactive languages than bothering with FORTRAN. It's a holdover from simpler times - it's only still used because it executes a bit faster and there's legacy code, but most scientists (apart from computer scientists) <em>don't</em> understand that the time saved in execution is negligible compared to the time spent updating code and on maintenance... both of which can be orders of magnitude faster in more modern languages.
You save execution time in algorithms, not in languages. C++ is a good bet, as it allows both functional and OO programming within a single application. I use it, and I'm sutdying computer science and physics...</p>

<p>Hey, guys, let me know what you think of this website.</p>

<p><a href="http://www.freenetpages.co.uk/hp/alan.gauld/%5B/url%5D"&gt;http://www.freenetpages.co.uk/hp/alan.gauld/&lt;/a&gt;&lt;/p>