Summation of function handles in cell array
6 views (last 30 days)
Show older comments
Hi. I have written the following code so far.
par.comp = 2;
par.NT = 9;
par.P = 1;
par.Asat=[4.92531 4.6543 ];
par.Bsat=[1432.526 1435.264];
par.Csat=[-61.819 -64.848];
ics = [ 5.0000 0.2545 0.2545 0.2545 0.2545 0.6295 0.6295 0.6295 5.0000
4.9810 0.2535 0.2476 0.2289 0.1834 0.3066 0.1780 0.0469 0.0238
0.0190 0.0010 0.0069 0.0256 0.0711 0.3229 0.4515 0.5826 4.9762]';
for i = 1:par.comp
Psat{i} = @(T) 10.^(par.Asat(i)-par.Bsat(i)./(par.Csat(i)+T));
y{i} = @(T,x) x*Psat{i}(T)/par.P;
end
xn = bsxfun(@rdivide,ics(:,2:end),ics(:,1));
for i = 1:par.NT
T(i) = fzero(@(T) y{1}(T,xn(i,1))+y{2}(T,xn(i,2))-1,300);
end
The code works as it is supposed to (I think :) ), but I need it to be expandable for a higher value of par.comp. If par. comp is for instance 3 (ics would be 4x9), I need the last for loop to be:
for i = 1:par.NT
T(i) = fzero(@(T) y{1}(T,xn(i,1))+y{2}(T,xn(i,2))+y{3}(T,xn(i,3)-1,300);
end
And so on. Is there any way to do this?
0 Comments
Accepted Answer
Guillaume
on 17 Mar 2016
To be honest you're probably better off creating an explicit function, but you can achieve the summation with recursive anonymous functions. I have no idea what the impact on performance would be:
%xn = ???? %not sure what it should be when par.comp > 2
assert(all([numel(par.Asat), numel(par.Bsat), numel(par.Csat) >= par.comp);
assert(size(xn, 2) >= par.comp && size(xn, 1) >= par.NT);
ysum = @(~, ~, ~) -1; %constant -1 for i = 0;
for i = 1:par.comp
Psat = @(T) 10.^(par.Asat(i)-par.Bsat(i)./(par.Csat(i)+T));
y = @(T,x) x*Psat(T)/par.P;
ysum = @(T, xn, j) y(T, xn(j, i)) + ysum(T, xn, j); %call the previously defined anonymous function
end
%at the end of the loop ysum is a massive recursive anonymous function
for i = 1:par.NT
T(i) = fzero(@(T) ysum(T, xn, i), 300);
end
3 Comments
Guillaume
on 17 Mar 2016
I meant use a proper m file instead of anonymous function, but actually it may be even more complicated, so forget about it
The j is to distinguish between your two i loops, with the recursive anonymous function you need to pass it the i from the par.NT loop, you can't call it i obviously since it's already used for the par.comp loop. With your solution it's equivalent to:
for i = 1:par.comp
Psat{i} = @(T) 10.^(par.Asat(i)-par.Bsat(i)./(par.Csat(i)+T));
y{i} = @(T,n) xn(n,i)*Psat{i}(T)/par.P;
end
for j = 1:par.NT
T(j) = fzero(@(T) sum(cellfun(@(g) g(T,j),y))-1,300)
end
Your solution and mine are pretty much equivalent in term of weirdness. If anything I'd say the recursive anonymous function is weirder than cellfun. The main difference between the two solutions is that your function does the sum expansion with an explicit loop (that's what cellfun is after all) whereas the sum expansion is already unrolled in my solution. So my solution may be slightly faster.
玉成
on 29 Jun 2022
你好,我使用循环得到m{149}和PP{150,149}关于a的函数表达式,然后在它们等于30时候的根,但是运行太慢,有没有什么好的解决办法。代码如下:
clear
clc
x=[0,0.0810795702470861,0.338893857750972,0.289608984180225,0.373884785059033,0.419305386647289,0.381255405793147,0.387805825669320,0.473950108456370,0.777482062955663,0.899607314986380,0.879732671835855,0.642150284769991,0.686654174145980,0.733665507297614,0.648550239659963,0.792221920178573,0.858663843400142,0.950901518893565,0.908403222771981,0.841901725388123,0.803776648251092,0.685298083103286,0.642201303788118,0.758140400904556,0.497859095240467,0.299720346945175,0.330083731903406,0.399635177673192,0.308616636693337,0.406471800425586,0.596726876476001,0.715214262626168,0.550458743758525,0.492048935650519,0.419049480913983,0.412200268366443,0.364288733652186,0.295831971015213,0.576419028947360,0.574301385466896,0.596020783630510,0.674243062495796,0.498132386268964,0.397725957361996,0.502159675610620,0.592607341674827,0.492748845200838,0.683134575176520,0.723692678801045,0.791615285736084,0.856438696079586,0.838872071129628,0.660231299794271,0.697676532404639,0.554721614799348,0.589892742846681,0.673120226900998,0.756160690500417,0.639809622049692,0.746181259265069,0.654068062590623,0.747902390546003,0.887201993210606,1.15338612214489,0.958977631327466,1.12987192182697,1.17636074235029,1.23483468030576,1.23269555488261,1.33231404505112,1.37060109996512,1.36433302688288,1.36920419311632,1.52115837829399,1.65834482953446,1.87356345706959,1.99647272427397,2.05601842308497,1.99102453480784,2.11084190188162,2.15306161675519,2.26308133401253,2.45670787883561,2.47836705949687,2.50236540231708,2.55557973733006,2.66947127745567,3.01512570503452,2.99371227198361,3.25320110604215,3.31695082167831,3.52252671263044,3.62307908694682,3.64950604277507,3.71171958790155,3.92900862767658,4.18624573833328,4.40580642424208,4.43349562449954,4.37047086316136,4.52217706074632,4.69096186724754,4.47614654427472,4.58321154245995,4.95328125690921,5.03544570887013,5.06096113419815,5.04148762518430,4.92141732190889,4.96950751489138,5.11863558218135,5.10618007179529,5.25124128844036,5.46491228721431,5.50486562885070,5.68312502453605,5.87165380633716,5.98423928608256,6.35071262361604,6.30979036073293,6.46558377293770,6.47035888195859,6.50206243202057,6.68184170578453,6.94508314353340,7.09259966782103,7.38465156007102,7.39499822256765,7.56677221290127,7.62961885234629,7.66481995486628,7.46395102011682,7.69310729809314,8.06425918178358,8.26245926894464,8.57255189453345,8.71701239391423,8.69937525807431,8.72801534449858,8.76648524557879,9.04692602569443,9.12816457617079,9.28273121221453,9.25809977088039,9.37994368599395,9.32028599442426,9.37843566395927,9.62250467915979];
t=[0:0.1:14.8];
r=148
u(1)=0.0001;%初始化量
p(1)=0.0001;
s(1)=0.0001;
Q(1)=0.25;
k=1
for i=2:r
O{i+1}=@(a)(t(i+1)).^a-(t(i)).^a;
O{2}=@(a)t(2).^a-t(1).^a;
m{1}=u(k);
m1{2,1}=m{1}
PP{1,1}=p(k);
PP{2,1}=PP{1,1}+Q(k)
K{2}=@(a)PP{2,1}*O{2}(a)*(((O{2}(a))^2)*PP{2,1}+s(k)*(t(2)-t(1)))^(-1);
m{2}=@(a)m{1}+K{2}(a)*(x(2)-x(1)-m{1}*O{2}(a));
PP{2,2}=@(a)PP{2,1}-O{2}(a)*K{2}(a)*PP{2,1};
PP{3,2}=@(a)PP{2,2}(a)+Q(k);
m1{i+1,i}=@(a)m{i}(a);
PP{i+1,i}=@(a)PP{i,i}(a)+Q(k);
K{i+1}=@(a)PP{i+1,i}(a)*O{i+1}(a)*(((O{i+1}(a))^2)*PP{i+1,i}(a)+s(k)*(t(i+1)-t(i)))^(-1);
m{i+1}=@(a)m{i}(a)+K{i+1}(a)*(x(i+1)-x(i)-m{i}(a)*O{i+1}(a));
PP{i+1,i+1}=@(a)PP{i+1,i}(a)-O{i+1}(a)*K{i+1}(a)*PP{i+1,i}(a);
PP{i+2,i+1}=@(a)PP{i+1,i+1}(a)+Q(k);
end
a=fzero(@(a)PP{150,149}(a)-30,10)
a=fzero(@(a)m{149}(a)-30,10)
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!