Help Defining Variable Contained Within Summation Notation, and Using this Variable for Plot

1 view (last 30 days)
Hi all,
I am having trouble defining and using a variable in my code. In my code (shown below), I would like to establish a variable "t" that varies from 0 to 1e6, only containing integer values. Therefore, I would like my SumFe56 term to be a function that finds the sum from n=1 to n=1e7 for t=0, and then from n=1 to n=1e7 for t=1, and so on, until my code provides values for SumFe56 from n=1 to n=1e7 for t=1e6. I would then like to use the SumFe56 function in a larger function, BulkFe56, before finally making a plot that shows BulkFe56 values on the y-axis, and t on the x-axis, with the t values varying on this plot from t=0 to t=1e6. Can anyone please tell me the code that would be capable of doing this?
Here is my code, currently:
n = 1:1e7;
t = 0:1e6;
r = .002;
DFe56 = 1e-12;
SumFe56 = sum((1./n.^2).*exp(-n.^2*pi^2*DFe56*t/r^2));
BulkFe56 = (20*.9175)+((6*.9175*(9-20))/pi^2)*SumFe56;
Thanks,
Jonathan

Accepted Answer

fred  ssemwogerere
fred ssemwogerere on 11 Feb 2020
Hello, you could use nested for loop. But i envision this running into an "Out of memory" problem sheerly based on the length of "n" and "t".
  3 Comments
fred  ssemwogerere
fred ssemwogerere on 12 Feb 2020
Hello, by nested for loop, i meant something like this;
n = 1:1e7;
r = .002;
DFe56 = 1e-12;
t = 0:100000:1e6;
% pre-allocate a temporary variable to store output from the nested loop. Let me call this variable: "v"
v=zeros(length(t),length(n));
for i=1:numel(t)
for j=1:numel(n)
% storing output in variable "v"
v(i,j)=(1/n(j)^2)*exp(-n(j)^2*pi^2*DFe56*t(i)/r^2);
end
end
SumFe56=sum(v,2); % taking sum along the rows (this computes sum for each value of "t")
clear v % Since there is no further use for the temporary variable "v", it is best to clear it from memory with this line
BulkFe56=(20*.9175)+((6*.9175*(9-20))/pi^2).*SumFe56;
% plot your data
figure;handl=plot(t,BulkFe56);handl.Parent.XScale='log';

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!