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

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

 采纳的回答

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 个评论

Hi Fred,
Thanks for your response. I'm pretty new to this, so could you provide more detail as to what the nested for loop would look like?
I've provided what I've changed below, choosing to modify the t values at intervals of 100000 at this step so the code takes less time to run. I've also provided the answers for BulkFe56 that I'm getting farther below. Here's the thing: I'm generating the correct values of BulkFe56, although when I try to plot them, there is nothing that is appearing on my plots. I'm assuming that my data is overwriting itself with every new point I get for BulkFe56, and this is why there are no lines appearing on my plot.
Thus, what would my code look like for creating a nested loop so that my data does not overwrite itself, and I can plot the values of t vs. BulkFe56?
n = 1:1e7;
r = .002;
DFe56 = 1e-12;
for t = 0:100000:1e6
SumFe56 = sum((1./n.^2).*exp(-n.^2*pi^2*DFe56*t/r^2));
BulkFe56 = (20*.9175)+((6*.9175*(9-20))/pi^2)*SumFe56;
end
plot(t,BulkFe56)
set(gca,'XScale','log')
BulkFe56 =
8.2575
BulkFe56 =
12.9024
BulkFe56 =
14.3830
BulkFe56 =
15.3430
BulkFe56 =
16.0336
BulkFe56 =
16.5522
BulkFe56 =
16.9498
BulkFe56 =
17.2577
BulkFe56 =
17.4971
BulkFe56 =
17.6839
BulkFe56 =
17.8296
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';

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by