Write a script which produces a table which approximates half-life decay. P(t) =
ID: 3801894 • Letter: W
Question
Write a script which produces a table which approximates half-life decay. P(t) = P_0(1/2)^t/t_1/2 Your program should take two inputs: (a) the initial population (P_0) (b) the half-life (t_1/2) in years. Your program should then produce a table (see format below) which gives the decaying population and corresponding time values until the population reaches 1. Your function should allow for repeated operation until a terminating condition (such as negative starting population) is specified. Enter the starting population (billions): 20 Enter the half-life of the material (years): 5Explanation / Answer
#save as decay.m
#prompt user to enter starting population
p0=input('Enter the starting population(billions):');
#prompt user to enter t 1/2
tHalf=input('Enter the half-life of the material(years):');
#set time,t=0
t=0;
#set repeat to true
repeat=true;
#calculate population
population=p0*(0.5^(t/tHalf));
#print heading
fprintf('%-15s %-15s ','Time(years)','Pop(billions)');
fprintf('%-15s %-15s ','=====','==========');
fprintf('%-15d %-15.2f ',t,population);
#repeat until the repeat condition is true
while repeat
#set time,t=t5
t=t+5;
#calculate population
population=p0*(0.5^(t/tHalf));
#check population is >1
if population>1
fprintf('%-15d %-15.2f ',t,population);
else
#otherwise set repeat=false
repeat=false;
end
#end while loop
end
------------------------------------------------------------------------------------------------
Sample Output
>>Enter the starting population(billions):20
Enter the half-life of the material(years):5
Time(years) Pop(billions)
===== ==========
0 20.00
5 10.00
10 5.00
15 2.50
20 1.25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.