Saving output values from a function into an array (to plot later)

24 次查看(过去 30 天)
So I have written a loop that runs a function for a given m to give a specific time_hours value (for that given m). The program works and I get all the values I need, but how would I save these values in an array to plot against m later on? I'm not sure how this would work as I have used fzero to calculate the time_hours values on each iteration and fzero values cannot be indexed? Thanks for any help, here is my code:
alpha = 0.7e-7; rho = 0.1 .^(1/3); %m = [4:0.1:8];
for m = 4:0.1:8
[term, scarysum, summation_t, f, time_seconds, time_hours] = tcalc(alpha, rho, m);
end
function [term, scarysum, summation_t, f, time_seconds, time_hours] = tcalc(alpha, rho, m)
scarysum = 0;
for k = 1:30
syms t
term = ((-1).^(k-1))./k .* sin(k.*pi.*rho) .* exp((-1 .* k.^2 .* pi.^2 .*alpha.*(1360.*pi).^(2/3).*t)./m.^(2/3));
scarysum = scarysum + term;
end
summation_t = matlabFunction(scarysum);
f = @(t) (2./(pi*rho) .* (summation_t(t))) - 0.7125;
time_seconds = fzero(f,14400);
time_hours = time_seconds ./ 3600;
disp(time_hours)
end

采纳的回答

Allen
Allen 2019-12-18
编辑:Allen 2019-12-18
If your function is returning scalar values that are being overwritten during each iteration of your for-loop, try the following as a replacement to your for-loop.
[term, scarysum, summation_t, f, time_seconds, time_hours] = arrayfun(@(m) tcalc(alpha, rho, m), 4:0.1:8);
% This is essentially the same as rewriting the for-loop to something like this, which changes the size
% of the output variables each iteration and appends the new value to the end of each variable.
rng = 4:0.1:8;
for m = length(rng)
[term(m), scarysum(m), summation_t(m), f(m), time_seconds(m), time_hours(m)] = tcalc(alpha, rho, rng(m));
end
  1 个评论
Sid Halder
Sid Halder 2019-12-18
Thank you so much! It's honestly so convenient that arrayfun exists, perfect function to use in my case. Ran into a few problems with arrayfun as my output variables contained exponential terms (which aren't scalar) so MATLAB had issues creating the new arrays but I think I've sorted it all out. Thanks once again.

请先登录,再进行评论。

更多回答(1 个)

Cameron B
Cameron B 2019-12-18
options = optimset('PlotFcns',@optimplotfval);
scarysum = 0;
for k = 1:30
syms t
term = ((-1).^(k-1))./k .* sin(k.*pi.*rho) .* exp((-1 .* k.^2 .* pi.^2 .*alpha.*(1360.*pi).^(2/3).*t)./m.^(2/3));
scarysum = scarysum + term;
end
summation_t = matlabFunction(scarysum);
f = @(t) (2./(pi*rho) .* (summation_t(t))) - 0.7125;
[time_seconds,fval,exitflag,output] = fzero(f,14400,options);
time_hours = time_seconds ./ 3600;
disp(time_hours)
fig = gcf;
dataObjs = findobj(fig,'-property','YData');
y1 = dataObjs(1).YData;
x1 = dataObjs(1).XData;
You'll have to save y1 and x1 each time, but that's how you'd extract the data.

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by