MATLAB What is each step of this Matlab program doing?

  • Thread starter Thread starter grandpa2390
  • Start date Start date
  • Tags Tags
    Matlab Program
Click For Summary
The MATLAB program initializes two arrays, 'nuclei' and 't', both with 100 elements set to zero, to simulate radioactive decay. Users input the initial number of nuclei, a time constant, and a time step, which are used in a loop to calculate the decay over time. The loop iterates 99 times, updating the number of nuclei based on the decay formula and incrementing the time array. The program then plots the number of nuclei against time, labeling the axes and including text annotations for the time constant and time step. The discussion highlights the importance of defining array sizes and the potential for user input to enhance flexibility in the program.
grandpa2390
Messages
473
Reaction score
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
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.
 
Isn't this the same problem for which you had a True BASIC program? Did you give up on that?
 
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.
 
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?
 
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

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K