# What program should I learn for physics?

Tags:
1. Nov 17, 2016

### Galactium

I am currently a freshman majoring in physics. My professor recommended me using MatLab for computations, but I'm not sure how to teach myself. Next quarter I will be taking Computer science, I think they will be learning C++ ( I do have a bit of experience with C++ and python). I want to learn how to use coding for my future physics class, I want to use it to do data analysis, simulations, calculations etc. Any advice will help.

While I'm at this, any recommended books/websites/sources to teach myself how to learn these programs? I have a few apps for C++ and python.

2. Nov 17, 2016

### Staff: Mentor

Okay RosettaCode has a collection of programming tasks written in a large variety of languages for comparison. It can help you learn by example with the understanding that any example may not be the best example for that language.

www.rosettacode.org

For MATLAB, I like the book Essential Matlab by Hahn and Valentine. It has good coverage of the language and its toolkits. Students can get a student edition of MATLAB for around \$100 with access to its many toolkits.'

https://www.mathworks.com/support/books/book79218.html

https://www.amazon.com/Essential-MATLAB-Engineers-Scientists-Fifth/dp/0123943981

Alternatively, there is Freemat and Octave which provide many MATLAB features for free but are basically core MATLAB with no toolkits. I prefer Freemat as its much simpler to use and is a single executable. Octave is a fuller open source version of MATLAB but suffers from performance issues that probably won't affect you but limit its use in professional environments.

www.freemat.org

www.octave.org

Programming in MATLAB is a lot like programming in Basic with the added vector/matrix features.

Code (Matlab M):

# PLOT a Parabola
x=[0:100}
y=x.*x
plot(x,y)

For C/C++, there are several good websites, books and tutorials to choose from and others here will tell you what worked for them.

My preference in any language are the cookbooks where I can find the solution/recipe to doing a given task. For C++ and Python:

https://www.amazon.com/Cookbook-Solutions-Examples-Programmers-Cookbooks/dp/0596007612

https://www.amazon.com/Python-Cookbook-Third-David-Beazley/dp/1449340377

Alternatively, you could also look at the Processing language which is basically java with some convenience features. The processing website has many tutorials and its fun to write interactive graphics applications. It also has a Python mode (uses java based jython under the covers) to do graphical based python.

www.processing.org

Lastly, another alternative language to consider is Julia which destined to become the goto language for numerical computing. I can't say enough good things about it. It is faster than MATLAB and yet it interoperates with Fortran, Python, R and C/C++ meaning it can be integrated into legacy systems that need the powerful features of vector/matrix distributed programming.

www.julialang.org

Last edited by a moderator: May 8, 2017
3. Nov 20, 2016

### ChrisVer

there are always books/guides for use of MatLab... also some institutes choose to go with MatLab or Mathematica and which you choose/are recommended depends on which enterprise your institution supports and so you can download it from your university id...

For particle physics the following is useful:
For those kind of things, physicists already have their languages... C++ is quiet known for processing a large amount of data because it's a fast... again there is no reason to say why they chose C++ and not Java or any other language... It's what people chose to go with at some point.
Fortran also exists in some parts, because (as some of my teachers said) old professors (them including) are bored of learning new languages.
Python is also used in many cases after the large data processing, because it is easier (more user-friendly) to write a code than C++...So the analyses codes (eg plotting histograms, accessing the events, getting limits etc) are written or have written implementations in python code.

In general, I believe from my experience that most of the famous object oriented languages are more or less similar...so learning one makes it easy to learn the other (after getting used to the new syntax).

4. Nov 20, 2016

### Staff: Mentor

Fixed that for you...

5. Nov 22, 2016

### f95toli

Matlab is not really a programming language as such (although it can be used as one) and is certainly not a replacement for C/C++/Python/whatever.
Hence, if your goal is to learn how to use Matlab you should learn Matlab since knowing e.g. C++ is not really going to help much and vice versa.

Note that you can do quite a lot of work in Matlab without understanding anything more than the mere basics of programming (most of the thing a for loop is as advanced as it gets); many of the skills are Matlab specific (essentially knowing which command to use where).
You do need to be comfortable with the basics of vectors, matrices and arrays; but this is stuff they teach you in a course in linear algebra.

You rarely need to used any advanced CS concepts when running numerical simulations, unless you of course end up specializing in running very large simulations on clusters or similar.

6. Nov 22, 2016

### Dr Transport

I have to disagree somewhat, if you know C++, Matlab can be learned very quickly, it has many of the object oriented constructs in other languages, sure, there are specific commands and some different syntax that needs to be learned, but they are things that can't be done in C++ in a line or two. I typically use Matlab to rough out algorithms quickly then translate them into C++.

No it isn't a language per say and can't replace other languages, but it can be used to get things working quickly and get some results before going on to writing a full blown program.

7. Nov 24, 2016

### cosmik debris

Python is good, it has SciPy, NumPy, SymPy etc and other libraries to do most of what Matlab can do, and it is a much nicer language to use.

8. Nov 27, 2016

### FactChecker

I agree. An added feature of MATLAB is that you can develop models in the MATLAB language and then auto-generate production C code for the model. Many companies do that. Even if I did C code by hand, I liked to use a MATLAB code line as a comment in the equivalent C code.
Aero engineering students specializing in control laws have commented that a working knowledge of MATLAB (and Simulink) was a real advantage when job hunting. But that is engineering, not physics.

Last edited: Nov 28, 2016
9. Dec 3, 2016

### StatGuy2000

To the OP:

If you are interested in learning how to use MATLAB, in addition to the advice provided by others here on this thread, I would also suggest that you download Octave, which is the open-source version of MATLAB.

https://www.gnu.org/software/octave/

You can find many references on programming with MATLAB or Octave online.

10. Dec 4, 2016

### ChrisJ

If you are considering MATLAB, I would highly recommend Python with the MATplotlib, Numpy, Scipy and Sympy modules (seconding cosmik debris recomendation above)

The syntax is very similar, it can almost everything MATLAB can do, and since Python is a general purpose programming language too, it could possibly come in handy for non-physics programming stuff later in life (Jobs etc).

And the best thing about it, its FREE :) And there is a ton of resources available and help available on forums etc.

11. Dec 4, 2016

### FactChecker

I advise against using Python (or anything else) as a learning substitute for MATLAB. Unless you really can not afford the student license and can not get access to MATLAB, use the real thing.

Last edited: Dec 4, 2016
12. Dec 5, 2016

### ChrisVer

do you have an example for that advice? I am not saying it's wrong, I am just trying to understand it.

13. Dec 5, 2016

### FactChecker

If he wants to learn MATLAB, including all the options and extensions (Simulink, embedded C / Ada code generation, Code analyzer, source control integration, subject matter toolboxes, Simulink Model Adviser, building and integrating external C & Fortran libraries, etc.) , then he should use MATLAB. MATLAB and it's associated tools are very extensive. It is unlikely that he can learn another language and switch to MATLAB without a huge additional learning task.

14. Dec 5, 2016

### cosmik debris

I am not advocating Python as a learning substitute for Matlab but as an alternative. Matlab is Matlab, as someone else pointed out you can use Octave to get used to using that kind of programming technique. Python is just good for doing all sorts of general programming as well as having libraries for the kind of modelling that Matlab does. We use both, depending on preference. Sometimes people have developed a specific set of functions in Matlab that suit your particular project, why not use them? Sometimes there are Python libraries, ditto.

If you want more than just modelling some system then a general purpose language is just better to put your time into.

15. Dec 5, 2016

### FactChecker

His professor recommended MATLAB. I would take the professor at his word. I know groups that work almost exclusively in Python and others that work almost exclusively in MATLAB / Simulink. Both groups are very good at what they do and a person can not work with them without learning their language. Every language has its strengths and weaknesses.

PS. In the work that I did, I have seen Python over-sold by some.

16. Dec 14, 2016

### Jarvis323

Python has the advantage that, through SciKit Learn, it has perhaps the best machine learning support out of any language (better than Matlab I think). It is also quite a bit faster than Matlab.

Some people mentioned Fortran: as far as I know, its extensive use in physics for simulation is at least in part because it can be very efficient (more so than C or C++). Plus, many existing simulation codes are in Fortran, so knowing it could be beneficial in the long term if you want to do serious simulation work.

17. Dec 15, 2016

### cosmik debris

There is still a lot of computational library code written in FORTRAN, you don't need to program in FORTRAN to use these libraries of course, but care is needed with the bindings as there are subtleties like the way arrays are stored etc.

Power engineers here still use FORTRAN quite a bit, they are the butt of many jokes :-)

18. Dec 15, 2016

### FactChecker

FORTRAN always had some very nice features that are not in other languages. The namelist reads and writes allow a person to easily dump massive data files in ASCII format that can be directly read in again. It is easy for the user to modify the values or only specify a subset (particular array elements, etc.) in the file before it is read. For engineering and scientific work, that is handy. Programmers who are not very familiar with those features think that they make the same capability available in C or C++, but they can not. It would take an interpreted language or a pre-compile processing phase and would be difficult.