Nested loops and function output into a matrix inconsistency
Show older comments
I am attempting to fit a set of data simultaneously with two varying inputs: the temperature and h-constant. Both of these inputs are in single row, 400-element vector. The fundamental piece of my code is the function B that takes two inputs (scalars x and h) and outputs a column vector of 43 elements -- the 43 elements are based off of the "nu" vector (frequency) input. From my understanding, to achieve the best fit of the .txt file I implemented, I have to consider the initial temperature evaluated with ALL 400 h-constants and repeat that for the other 399 temperatures. As such, it seems like I need 160,000 outputted column vectors. So that's why I have a 43x160000 matrix.
The plan is to take each temperature and cycle through each h-constant before continuing on with the next T(i) value. Each column of my "md" matrix will be the output of the B function. The problems I'm encountering are that: the columns repeat themselves and that the output in the matrix doesn't match the numbers (example like B(T(1),H(1)) in the command window --> this should be the first column in "md" but it's not). I think the crux of the problem lies in the loop statement. Where did I misstep? How can I ensure the output replaces each column in the large matrix, accounting for every single T and h? I've attached the .txt file if the need arises. Thank you.
Dprime=readtable('FIRASdata.txt');
spints=Dprime.Var2; invwav=Dprime.Var1; % extracting the two columns
c=2.9979*10^(8); k=1.3806504*10^(-23) ; J=2/c^2;% constants
nu=c./(invwav.^(-1)*0.01); % CONVERSION; frequency of FIRAS data Hz^-1
T=linspace(1,5,400); % range of temperatures
H=linspace(2*10^(-34),1*10^(-33),400); %range of h-constants
B= @(x,h) 10^(20)*J*h*(nu.^3./(exp((h*nu)./(k*x))-1)); % anonymous function to return MJy/sr intensities
%%
md=zeros(43,160000);
for q=1:400
for r=1:400
for w=1:160000
md(:,w)=B(T(q),H(r));
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!