How do I translate this program to matlab?

  • #1
472
14

Homework Statement


?temp_hash=0bcf7fc392a44401675df74c76f4584e.png


Homework Equations




The Attempt at a Solution



I'm taking Computational Physics. And I am trying to learn how to program in matlab. But I don't understand the author's code and what it is in matlab code. Can someone help me?
 

Attachments

  • Screen Shot 2018-02-12 at 11.00.16 PM.png
    Screen Shot 2018-02-12 at 11.00.16 PM.png
    33.5 KB · Views: 387
  • ?temp_hash=0bcf7fc392a44401675df74c76f4584e.png
    ?temp_hash=0bcf7fc392a44401675df74c76f4584e.png
    33.5 KB · Views: 586

Answers and Replies

  • #2
FactChecker
Science Advisor
Gold Member
6,356
2,506
Do you know what language that is? It looks somewhat, but not quite, like FORTRAN to me. The code for the subroutines 'initialize', 'calculate', and 'display'. are missing. So there is practically no details there about what the program does. Are you talking about converting the entire thing to MATLAB or just this top level program that calls the subroutines?
 
  • Like
Likes grandpa2390
  • #3
472
14
Do you know what language that is? It looks somewhat, but not quite, like FORTRAN to me. The code for the subroutines 'initialize', 'calculate', and 'display'. are missing. So there is practically no details there about what the program does. Are you talking about converting the entire thing to MATLAB or just this top level program that calls the subroutines?
It is True BASIC

and the whole thing. I think if I can see how it these basic things translate, I'll be able to translate the rest of the author's code more easily.
 
  • #4
FactChecker
Science Advisor
Gold Member
6,356
2,506
First you need to determine which of the parameters of each function are inputs and which are outputs. Suppose that tau and dt are inputs of initialize and that n_uranium and t are output arrays. In that case, the call is:
Code:
[n_uranium, t] = initialize(tau, dt);
The variables tau and dt would need to be given values before initialize is called.

As it is, it looks like all the parameters of initialize are outputs. So that could be done with:
Code:
[n_uranium, t, tau, dt] = initialize();
 
  • #5
472
14
First you need to determine which of the parameters of each function are inputs and which are outputs. Suppose that tau and dt are inputs of initialize and that n_uranium and t are output arrays.
what are inputs of initialization?
 
  • #6
FactChecker
Science Advisor
Gold Member
6,356
2,506
what are inputs of initialization?
The input values of the function 'initialization'. You would need the code or some description of that function to decide what you want to input and what it should output.
 
  • #7
472
14
The input values of the function 'initialization'. You would need the code or some description of that function to decide what you want to input and what it should output.
are you saying initialization is some other program/function that is being called up?
 
  • #8
FactChecker
Science Advisor
Gold Member
6,356
2,506
are you saying initialization is some other program/function that is being called up?
Yes. All three of those called functions are specialized code just for that model. They are not general utilities that are part of the language. You will need to find the code for them and translate them to MATLAB also. You are probably only looking at the tip of the iceberg in the code you have posted.
 
  • #9
472
14
Yes. All three of those called functions are specialized code just for that model. They are not general utilities that are part of the language. You will need to find the code for them and translate them to MATLAB also. You are probably only looking at the tip of the iceberg in the code you have posted.

Wait I think I found them. Individually, but I think this puts them all in the same program???

?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png

?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
 

Attachments

  • Screen Shot 2018-02-13 at 10.53.17 AM.png
    Screen Shot 2018-02-13 at 10.53.17 AM.png
    69.2 KB · Views: 324
  • Screen Shot 2018-02-13 at 10.53.24 AM.png
    Screen Shot 2018-02-13 at 10.53.24 AM.png
    34.4 KB · Views: 341
  • ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    69.2 KB · Views: 260
  • ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    34.4 KB · Views: 210
  • #10
472
14
@FactChecker


edit:
I don't know. I have the option to use whatever program I want. the Professor likes MatLab, but if I wanted I could use C++, Mathematica, Excel, etc. Maybe I should just use True Basic so I can just copy and paste code?

edit: I assumed I could use True Basic on Mac because the author says the programs are written using libraries found on Mac... but apparently it isn't natively supported... weird.
 
Last edited:
  • #11
FactChecker
Science Advisor
Gold Member
6,356
2,506
Everything there is fairly easy to do in MATLAB unless you want to exactly mimic the graphing and display part. MATLAB has very good graphing capability of its own but it will not look exactly the same. MATLAB is very powerful and popular (within Physics and Engineering), so unless you already know other languages, it would be a good one to use. It's not free. Do you have MATLAB available to use?
 
  • #12
472
14
Everything there is fairly easy to do in MATLAB unless you want to exactly mimic the graphing and display part. MATLAB has very good graphing capability of its own but it will not look exactly the same. MATLAB is very powerful and popular (within Physics and Engineering), so unless you already know other languages, it would be a good one to use. It's not free. Do you have MATLAB available to use?

It has to look exactly like the graph in the book. That's how we're being graded. if it is off by a little. if the colors are wrong, if the title is wrong, the fonts, he position, the spacing, whatever. she'll give it back and tell us to try again.

I have access to matlab.
so are all the commands the same in matlab?
 
  • #13
FactChecker
Science Advisor
Gold Member
6,356
2,506
Discuss that with your teacher. If she would be happy with the MATLAB type of graph, then I recommend using MATLAB.
Otherwise, it is very difficult to use one utility to exactly mimic another utility. It could be a real effort for a professional programmer. If she really wants an exact match, then you would need to use the code you have. If the functions settitle, sethlabel, setvlabel, and datagraph in the display part are specialized code, you would need to find that and use it. If they are in a utility library, you would need to use that.
 
  • #14
472
14
Discuss that with your teacher. If she would be happy with the MATLAB type of graph, then I recommend using MATLAB.
Otherwise, it is very difficult to use one utility to exactly mimic another utility. It could be a real effort for a professional programmer. If she really wants an exact match, then you would need to use the code you have. If the functions settitle, sethlabel, setvlabel, and datagraph in the display part are specialized code, you would need to find that and use it. If they are in a utility library, you would need to use that.

I should have just taken a more advanced Physics course. too late now :(
I wanted to go see her yesterday. then I remembered that today was Mardi Gras and classes were canceled :(
 
  • #15
2,358
683
I use True BASIC routinely, and that is indeed what this code is. It is a very powerful, flexible, and easy to use language. When my last employer required everything to be done in Matlab, I would first get it running in True BASIC, and then convert to Matlab, a great waste of effort, but easier for me than working in Matlab.

temp_hash-e8802ab02fa01bfd7c93eb5022a2c8c2-png.png

The main program is the first 10 lines, down to the END statement. There are three external subroutines called INITIALIZE, CALCULATE, AND DISPLAY. The calling arguments for each are shown in the main program, and they correspond one-to-one to the lists in the subroutine declarations, even through the names are different. Thus, the first calling argument in the call to INITIALIZE is n_uraniaum which was previously dimensioned 100 in the main program. In the subroutine, the first argument is NUCLEI() where the parenthese tell the code to exxpect a one dimensional array.

The easiest way to run this code is to download the free trial version of True BASIC Bronze edition from TrueBASIC.com.
 

Attachments

  • temp_hash-e8802ab02fa01bfd7c93eb5022a2c8c2-png.png
    temp_hash-e8802ab02fa01bfd7c93eb5022a2c8c2-png.png
    69.2 KB · Views: 567
  • Like
Likes grandpa2390 and FactChecker
  • #16
472
14
I use True BASIC routinely, and that is indeed what this code is. It is a very powerful, flexible, and easy to use language. When my last employer required everything to be done in Matlab, I would first get it running in True BASIC, and then convert to Matlab, a great waste of effort, but easier for me than working in Matlab.

View attachment 220271
The main program is the first 10 lines, down to the END statement. There are three external subroutines called INITIALIZE, CALCULATE, AND DISPLAY. The calling arguments for each are shown in the main program, and they correspond one-to-one to the lists in the subroutine declarations, even through the names are different. Thus, the first calling argument in the call to INITIALIZE is n_uraniaum which was previously dimensioned 100 in the main program. In the subroutine, the first argument is NUCLEI() where the parenthese tell the code to exxpect a one dimensional array.

The easiest way to run this code is to download the free trial version of True BASIC Bronze edition from TrueBASIC.com.

I guess I had better figure out how to run True BASIC Bronze addition on my mac. Maybe I can run it in WINE or Parallels or something? If I don't have to convert it to Matlab, it will probably be easier that way.
 
  • #17
2,358
683
My understanding is that True BASIC will run with no modifications on either Windows or a Mac. Check the web site to be sure. That was always one of their selling points. I've been using this software for over 30 years for teaching, consulting, and research, and I have nothing but praise for it. It is not fast enough to serve for robotics applications (real time control), but for everything else, I have found it to work great. I've written fairly large programs (a few thousand lines) and little short programs, and everything works just like I expect it to.
 
  • #18
472
14
My understanding is that True BASIC will run with no modifications on either Windows or a Mac. Check the web site to be sure. That was always one of their selling points. I've been using this software for over 30 years for teaching, consulting, and research, and I have nothing but praise for it. It is not fast enough to serve for robotics applications (real time control), but for everything else, I have found it to work great. I've written fairly large programs (a few thousand lines) and little short programs, and everything works just like I expect it to.

I got a windows computer, installed True Basic Bronze, and I copied that program in exactly.
I got this:
library sgfunc trouble using disk or printer
 
  • #19
2,358
683
The original program includes a (less than helpful) comment that says about sgfunc and sglib that they are found "here" wherever that is. Without them, you don't have the whole program.

You have two options: (1) go look for where "here" is and get those two routines, or (2) figure out what they do and write them yourself.
 
  • #20
35,115
6,856
The original program includes a (less than helpful) comment that says about sgfunc and sglib that they are found "here" wherever that is. Without them, you don't have the whole program.
In the code itself is the line
Code:
library "sgfunc*", "sglib*"
I assume that the asterisks mean all library code modules that start with these names. @grandpa2390, look in whatever directories that were installed with True Basic to see if you can find these files.

It's also possible that these files are present, but the True Basic compiler/interpreter can't find them. If that's the case you'll need to add the directory these library files are in to your path. Either that, or provide the full path in the library statement. The documentation for True Basic would have a section devoted to what you need to do before you can run a program.
 
  • #21
2,358
683
I vaguely recall seeing a reference to "sgfunc" in the past, but it is not anything I have ever used. There is also a call to a routine "datagraph" that it seems you do not have. You are probably going to have to write some code, but it is really easy to do. It appears that the real work of this program happens in "sub calculate." Get some examples of coding in TB, and follow them to write your own plotting routine.

As I think about it, I believe that "sgfunc" and "sglib" are both routines that are part of the Gold version (many more $$$ than the Bronze version), and probably not available in Bronze. You really do not need either of them. Remove the call to "uranium (....)" and write a short loop including a plot statement to draw the graph you need (FOR i=1 TO n // plot x(i),y(i); // next i) where // means a new line of code. The semicolon is required to connect the dots. You can plot the labels using the statement PLOT TEXT, at 12,15: "This is the text to appear." starting at the points with coordinates (12,15).
 

Related Threads on How do I translate this program to matlab?

  • Last Post
Replies
9
Views
3K
Replies
2
Views
5K
Replies
2
Views
1K
Replies
5
Views
7K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
10
Views
1K
Replies
1
Views
3K
  • Last Post
Replies
1
Views
2K
Top