Eddy Orsi
Eddy Orsi

Reputation: 13

I am having trouble getting my function to provide its output in a matrix

I want to plot the log of a variable Dl which depends on a variable z with respect to log(z). I'm trying to do this for z = 1:100 but it returns only 1 number for Dl.

% Log(Dl) versus Log(Redshift)
m = 1;
d = 0;
z = linspace(1,100,1);
Dl = zeros(1,100);
for z = 1:100
    [Dl,Da] = Cosmological(m,d,z);
end
y = log(Dl);
x = log(1:100);
plot(x,y)

apologies for any silly or useless lines of code I'm very new to programming. The function cosmological that I called for is written as follows (but there are no errors so may not be necessary, I'm posting just in case):

function [Dl,Da] = Cosmological(m,d,z)
f = @(x)1./((1+x).*((m.*(1+z)-m+d.*((1+x).^(-2))-d+1).^(.5)));
q = integral(f,0,z);   % Integral part equations for Dl
if m+d==1             % flat universe condition
    Dl=c/H0.*(1+z)*q;
elseif m+d<1          %positive spatial curvature universe condition
    Dl=c/H0*(1-m-d)^(-1/2)*(1+z)*sinh((1-m-d)^.5).*q;
else                  % negative spatial curvature universe condition
    Dl=c/H0*(1-m-d)^(-1/2)*(1+z)*sin((1-m-d)^.5).*q;
end
Da = Dl/(1+z)^2;       %Angular diameter distance function
end

Upvotes: 0

Views: 33

Answers (1)

EBH
EBH

Reputation: 10450

First, these line is not needed, you assign z with 1:100 in the loop:

z = linspace(1,100,1);

You get only one value because your loop saves only the last value. You should index Dl with z like this (and probably also Da):

for z = 1:100
    [Dl(z),Da] = Cosmological(m,d,z);
end

Upvotes: 1

Related Questions