how to save calculating result ( form two variable) in 2-D matrix ?

1 view (last 30 days)
let's me explaining my question :
for example in 1-D
using for loop:
z=[ ];
for Eg=.5:.05:1.3;
a = quad(@(x) (x.^2)./(exp(x/0.516)-1) , Eg , 100 );
z = [z a]
end
then i use "arrayfun" saving result in 1-D matrix
z=[ ];
Eg=0.5:0.05:1.3;
a = arrayfun(@(x1)quad(@(x)x.^2./(exp(x/0.516)-1),x1,100),Eg)
Now i want to this process with 2 variables change to 2-d matrix that Eg is on row and Ei is on column.
(here is my code)
z=[ ];
for Eg=.5:.05:1.3;
for Ei=0:0.05:Eg-0.05
aa= quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ei , Eg);
z = [z a]
end
end
thanks :)

Accepted Answer

Sargondjani
Sargondjani on 8 May 2012
the problem is that the length of Ei will differ for the values of Eg. To solve this with for loop i preallocate 'aa' with NaN everywhere
%with for loop
Eg=.5:.05:1.3;
aa=zeros(17,26)+NaN;
for ig=1:length(Eg);
Ei=0:0.05:Eg(ig)-0.05;
for ii=1:length(Ei);
aa(ig,ii)= quad( @(E)(E.^2) ./ (exp(E./0.5172)-1), Ei(ii) , Eg(ig));
end
end
clear ig ii
without the for loop: i create a 'meshgrid' and then i cut of every value for Ei: if EI>=EG then i set EI=EG-0.05
%without for loop
Eg=.5:.05:1.3;
Ei=0:0.05:Eg(end)-0.05;
[EI,EG]=meshgrid(Ei,Eg);
EI(EI>=EG)=EG(EI>=EG)-0.05;
bb = arrayfun(@(E1,E2)quad(@(x)x.^2./(exp(x/0.5172)-1),E1,E2),EI,EG);

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!