필터 지우기
필터 지우기

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

조회 수: 10 (최근 30일)
Sid Halder
Sid Halder 2019년 12월 18일
댓글: Sid Halder 2019년 12월 18일
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에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by