How do I translate this program to matlab?

In summary: That's how we're being graded. if it is off by a little. if the colors are wrong, if the labels are wrong, if the labels are upside down, if the axes are wrong, if the lines are wrong, then it's not going to be good enough.In summary, the author's code is missing details about what the program does, and it is written in a language that looks like FORTRAN, but is not actually FORTRAN.
  • #1
grandpa2390
474
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
    16.6 KB · Views: 496
  • ?temp_hash=0bcf7fc392a44401675df74c76f4584e.png
    ?temp_hash=0bcf7fc392a44401675df74c76f4584e.png
    16.6 KB · Views: 703
Physics news on Phys.org
  • #2
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
FactChecker said:
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
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
FactChecker said:
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
grandpa2390 said:
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
FactChecker said:
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
grandpa2390 said:
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
FactChecker said:
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
    35.9 KB · Views: 395
  • Screen Shot 2018-02-13 at 10.53.24 AM.png
    Screen Shot 2018-02-13 at 10.53.24 AM.png
    18 KB · Views: 409
  • ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    35.9 KB · Views: 425
  • ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    ?temp_hash=e8802ab02fa01bfd7c93eb5022a2c8c2.png
    18 KB · Views: 360
  • #10
@FactCheckeredit:
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
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
FactChecker said:
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
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
FactChecker said:
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
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
    35.9 KB · Views: 769
  • Like
Likes grandpa2390 and FactChecker
  • #16
Dr.D said:
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
My understanding is that True BASIC will run with no modifications on either Windows or a Mac. Check the website 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
Dr.D said:
My understanding is that True BASIC will run with no modifications on either Windows or a Mac. Check the website 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
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
Dr.D said:
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 into 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
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 to How do I translate this program to matlab?

1. How do I open and run a program in Matlab?

To open a program in Matlab, go to the "File" menu and select "Open". Then, navigate to the location of your program and select it. To run the program, click on the "Run" button or press the F5 key on your keyboard.

2. How do I convert code from another programming language to Matlab?

The easiest way to convert code from another programming language to Matlab is to use the "Code Compatibility Report" tool. This tool will analyze your code and provide suggestions on how to convert it to work in Matlab. You can access this tool by going to the "Code" menu and selecting "Generate Code Compatibility Report".

3. How do I troubleshoot errors when translating a program to Matlab?

If you encounter errors while translating a program to Matlab, the first step is to carefully read the error message and try to understand what the issue is. Then, you can use the "Debugger" tool in Matlab to step through your code and identify the source of the error. You can access the debugger by going to the "Debug" menu and selecting "Debug".

4. How do I import data into Matlab from another programming language?

To import data into Matlab from another programming language, you can use the "Import Data" tool. This tool allows you to import data from various file formats, such as text files, spreadsheets, and images. You can access this tool by going to the "File" menu and selecting "Import Data".

5. How do I save my translated program in Matlab?

To save your translated program in Matlab, go to the "File" menu and select "Save". You can also use the shortcut Ctrl+S on Windows or Command+S on Mac. Make sure to save your program in the correct file format, such as .m for Matlab scripts or .fig for Matlab figures.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
866
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
959
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Back
Top