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

1 回表示 (過去 30 日間)
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

採用された回答

fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 11 日
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 件のコメント
fred  ssemwogerere
fred ssemwogerere 2020 年 2 月 12 日
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';
Jonathan Pinko
Jonathan Pinko 2020 年 2 月 12 日
This was a lot of help.
Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by