how to save past outputs of a function
3 次查看(过去 30 天)
显示 更早的评论
I have the following code :
function NM
options = optimset('Display','iter','TolX',1e-10,'TolFun',1e-10);
[x,fval] = fminsearch(@(x)ban2(x),1,options);
function ban1 = ban2(x)
x1 = x
iono_layer_parms = [x(1), x(2), -1, -1, -1, -1];
[iono, iono_extra] = iri2012(lat, lon, x, UT,ht_start, ht_step,num_hts);
ionofreq = 0.008978663597663*sqrt((10.^(-6))*(iono(1,18:end)));
D6xb = interp1(alt3(101:697),D6xa(101:697),alt(26:175));
D6xc = 0.008978663597663*sqrt(D6xb);
ban = ionofreq(9:158) - D6xc;
ban1 = sum(ban.^2)
I want to save the variables x used in each iteration. I tried using persistent variables at the fourth line of the code but got error message :The PERSISTENT declaration must precede any use of the variable. x.
I also tried to use persistent variables at other places in function ban1 but got the same error message. Any help will be greatly appreciated.
0 个评论
采纳的回答
dpb
2015-3-9
Use the output function option to write the function to return the values. There's an example in the documentation that creates a history as you're looking for...
In the local doc for R2012b here there's an example w/ a history specifically like you're asking; that doesn't seem to show up so easily anyway w/ the online doc's...not sure what's changed if anything besides the documentation.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!