What is each step of this Matlab program doing?

In summary: I=1:numberOfNuclei-1I don't understand what this does. It should be I=1:nuclei.If you want to track how many nuclei are present at each time, you need to initialize nuclei to some value before the loop starts.It is stepping through time, not through the number of nuclei. It is stepping through time in 100 steps and calculating the number of nuclei at each step. In general, there would be huge numbers of nuclei and they are not considered individually. You only care that a certain percentage decay in each time step and keep track...You want to track how many nuclei are present at
  • #1
grandpa2390
474
14
So here is the program. I've spent a few hour trying to follow it. I numbered each line and after the code will try to explain what I think is going on in each line. I'm completely new to Matlab, and programming is a great weakness to me. I need help.
Matlab:
01. nuclei=zeros(1,100);
02. t=nuclei;
03. nuclei(1)=input('Enter initial number of nuclei: ');
04. t(1)=0;
05. t_c=input('Enter time constant: ');
06. dt=input('Time step: ');
07. for i=1:99
08. nuclei(i+1)=nuclei(i)-(nuclei(i)/t_c)*dt;
09. t(i+1)=t(i)+dt;
10. end
11. plot(t,nuclei,'-ok','MarkerFaceColor','k')
12. xlabel('Number of Nuclei','FontWeight','bold')
13. ylabel('time(s)','FontWeight','bold')
14. title('Radioactive Decay Number of nuclei versus time')
15. str1=['Time Constant= ' num2str(t_c) ' s '];
16. str2=['Time Step= ' num2str(dt) ' s'];
17. text(t(50),nuclei(10),str1,'FontWeight','bold','FontSize',14)
18. text(t(50),nuclei(20),str2,'FontWeight','bold','FontSize',14)
01. I looked up the zeroes function, and it returns a matrix of that size (1,100) full of zeroes. My guess is that this is how we declare nuclei to be a matrix. the addition of this line is unnecessary except to make the program run better though... so I'm not sure.

02. I don't really know. perhaps we are defining a matrix t as well? Removing this line doesn't change the graph either. perhaps just for efficiency.

03. in the first position of the nuclei matrix, enter the number of nuclei.

04. in matrix t, position 1 has a value of 0? Not entirely sure though. I think this is another line that I commented out and it had no effect. the textbook mentions in its true basic sudo code that t(1)=0 as a convention for Euler's method?

05. entering the time constant

06. entering the time step

07. for i=1:99. I understand a for loop. but I think the code should be I=1:numberOfNuclei-1
I don't know why it is numberofNuclei-1, but the sudo code in book implies it. it says:
for i=1 to size(t)-1
I can't figure out what size(t) is from what is written in the book. I am thinking that it is possible that size is a function (or sudo function?) in true basic that returns the size of the array that is in parenthesis. so size(t) would return 100 since 100 is the size of the array, t?

if this is true, I don't understand it. because in both this code and the sudo code, t is given a permanent size of 100. It happens that 100 is the number of nuclei the author plugs into this function. but what if I wanted to do 200 nuclei? the size of the arrays: t and nuclei are still 100. and so the for loop will still only go from 1->99?
 
Physics news on Phys.org
  • #2
This is basic MATLAB stuff you have here.

The nuclei and the t are arrays of 100 elements with both initially set to zeros.

The t array is basically your x-axis on a plot where t(1) is ##t_0## ie the x-axis represents time

The t array gets initialized as 0, dt, 2*dt, 3*dt where dt is your time step

and similarly the nuclei i+1 element is getting initialized using the ith element in that expressionnuclei(i)-(nuclei(i)/t_c)*dt and the ; just hides printing of any output to your screen.

I think the size(t) means the size of your t array ie 100 elements.

What you are plotting is nuclear decay (nuclei(i) is the decay value at time t(i)) over time.
 
  • #3
Isn't this the same problem for which you had a True BASIC program? Did you give up on that?
 
  • #4
grandpa2390 said:
01. I looked up the zeroes function, and it returns a matrix of that size (1,100) full of zeroes. My guess is that this is how we declare nuclei to be a matrix. the addition of this line is unnecessary except to make the program run better though... so I'm not sure.
Right. It's not necessary to do this, but it makes the program a little faster to have the entire array defined in one step.
02. I don't really know. perhaps we are defining a matrix t as well? Removing this line doesn't change the graph either. perhaps just for efficiency.
Correct again.
03. in the first position of the nuclei matrix, enter the number of nuclei.

04. in matrix t, position 1 has a value of 0? Not entirely sure though. I think this is another line that I commented out and it had no effect. the textbook mentions in its true basic sudo code that t(1)=0 as a convention for Euler's method?
You want some initial time value. Starting a simulation at time=0 is very common.
05. entering the time constant

06. entering the time step

07. for i=1:99. I understand a for loop. but I think the code should be I=1:numberOfNuclei-1
No. It is stepping through time, not through the number of nuclei. It is stepping through time in 100 steps and calculating the number of nuclei at each step. In general, there would be huge numbers of nuclei and they are not considered individually. You only care that a certain percentage decay in each time step and keep track of the numbers of nuclei remaining, not the individual nuclei.
I don't know why it is numberofNuclei-1, but the sudo code in book implies it. it says:
I can't figure out what size(t) is from what is written in the book. I am thinking that it is possible that size is a function (or sudo function?) in true basic that returns the size of the array that is in parenthesis. so size(t) would return 100 since 100 is the size of the array, t?
if this is true, I don't understand it. because in both this code and the sudo code, t is given a permanent size of 100. It happens that 100 is the number of nuclei the author plugs into this function. but what if I wanted to do 200 nuclei? the size of the arrays: t and nuclei are still 100. and so the for loop will still only go from 1->99?
Right. It nice to use something like size() so that if you change the number of time steps (either to simulate a longer time or to take smaller steps in time.), then you don't have to look all through the code to find other things that have to change.
 
  • #5
FactChecker said:
Right. It's not necessary to do this, but it makes the program a little faster to have the entire array defined in one step.Correct again. You want some initial time value. Starting a simulation at time=0 is very common.No. It is stepping through time, not through the number of nuclei. It is stepping through time in 100 steps and calculating the number of nuclei at each step. In general, there would be huge numbers of nuclei and they are not considered individually. You only care that a certain percentage decay in each time step and keep track of the numbers of nuclei remaining, not the individual nuclei. Right. It nice to use something like size() so that if you change the number of time steps (either to simulate a longer time or to take smaller steps in time.), then you don't have to look all through the code to find other things that have to change.

ok, but the 100 for t is constant. I guess it makes sense to do it this way so that you only have to change the code in one spot, but wouldn't it make more sense to have the user input the value for the array: t? that way we don't have to constantly reprogram every time we want to change t?
 
  • #6
grandpa2390 said:
ok, but the 100 for t is constant. I guess it makes sense to do it this way so that you only have to change the code in one spot, but wouldn't it make more sense to have the user input the value for the array: t? that way we don't have to constantly reprogram every time we want to change t?
Good question. There is always a trade-off between what you program in and what you ask the user to input each time. It depends on how often it changes. A third alternative, which requires more programming, is to have a default and prompt the user to input a number if they want to use a different number.

PS. Please be careful about calling 100 a "value of t". It is the dimension of the t array, not any of the values in the t array.
 
  • Like
Likes grandpa2390

1. What is the purpose of this Matlab program?

The purpose of this Matlab program is to perform a specific set of calculations or operations on a given set of data. It can also be used to visualize data, create graphs, and solve complex mathematical problems.

2. What does each step of the Matlab program do?

Each step of the Matlab program is a specific command or function that is executed in sequence to achieve the overall goal of the program. These steps can include loading data, manipulating data, performing calculations, and displaying results.

3. How does the Matlab program work?

The Matlab program works by using a combination of built-in functions and user-defined commands to process data and perform calculations. It uses a high-level programming language that is optimized for scientific and engineering applications.

4. What is the role of variables in a Matlab program?

Variables in a Matlab program are used to store and manipulate data. They act as placeholders for values that can change or be modified during the execution of the program. By assigning values to variables, the program can perform operations on the data and produce desired outputs.

5. How can I learn to write and understand Matlab programs?

To learn how to write and understand Matlab programs, one can start by familiarizing themselves with the basic syntax and functions of the language. Online tutorials, textbooks, and practice exercises can also be helpful in developing programming skills. Additionally, seeking guidance from experienced programmers or taking a class can aid in understanding more complex concepts and techniques.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top