What program should I learn for physics?

In summary, while 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, and I want to learn how to use coding for my future physics class. Any advice will help.
  • #1
Galactium
12
0
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.
 
Technology news on Phys.org
  • #2
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/dp/0123943981/?tag=pfamazon01-20

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.

http://www.freemat.org

www.octave.org

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

Matlab:
# 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/dp/0596007612/?tag=pfamazon01-20

https://www.amazon.com/dp/1449340377/?tag=pfamazon01-20

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:
  • Like
Likes nrqed, S.G. Janssens and Galactium
  • #3
Galactium said:
My professor recommended me using MatLab for computations, but I'm not sure how to teach myself.
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...

Galactium said:
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.
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
ChrisVer said:
Fortran also exists in some parts, because old professors (them including) are bored of learning too lazy to learn new languages.
Fixed that for you... :oldbiggrin:
 
  • Like
Likes ChrisVer
  • #5
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
f95toli said:
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.

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.
 
  • Like
Likes FactChecker
  • #7
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.
 
  • Like
Likes ChrisJ
  • #8
Dr Transport said:
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.
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:
  • #9
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
cosmik debris said:
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.

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
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:
  • #12
FactChecker said:
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.
do you have an example for that advice? I am not saying it's wrong, I am just trying to understand it.
 
  • Like
Likes ChrisJ
  • #13
ChrisVer said:
do you have an example for that advice? I am not saying it's wrong, I am just trying to understand it.
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.
 
  • Like
Likes ChrisJ
  • #14
FactChecker said:
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.

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.
 
  • Like
Likes ChrisJ
  • #15
cosmik debris said:
I am not advocating Python as a learning substitute for Matlab but as an alternative.
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
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
Jarvis323 said:
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.

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
cosmik debris said:
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 :-)
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.
 

1. What are the most commonly used programs in physics?

The most commonly used programs in physics are MATLAB, Mathematica, Python, and C/C++. These programs are used for data analysis, simulations, and mathematical calculations.

2. Which program is the best for beginners in physics?

For beginners in physics, Python is often recommended as it has a relatively easy syntax and a large community for support and resources. It is also a versatile language that can be used for a variety of tasks in physics.

3. What program is used for advanced calculations in physics?

For advanced calculations in physics, Mathematica and MATLAB are often preferred due to their powerful mathematical capabilities and built-in functions specific to physics. These programs also offer advanced visualization tools.

4. Are there any free programs for physics?

Yes, there are free programs available for physics such as Python, Octave, and FreeFEM. These programs may not have all the features and capabilities of paid programs, but they can still be useful for basic calculations and simulations.

5. Do I need to learn programming for physics?

It is not necessary to learn programming for physics, but it can greatly benefit your understanding and work in the field. Programming allows for more efficient data analysis and simulations, and many employers in the field prefer candidates with programming skills.

Similar threads

  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
729
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
10
Views
3K
Back
Top