How to change a single value to a vector - JULIA

I have a program in Julia that calculates many values, include a result of a final integral. All values calculated in the program depends on sigma, at first, fix in sigma = 0.6. Now I have to do a table of values, and change sigma to sigma= 0.319 / R , for R in [0.5:0.5:10] ( 20 points ).

So for each R, I will have a final result in my program. The output of this program is an integral result, called omega.

So, how could I do that, with no big changes in the program? sigma is declared at the beginning of the program.

I tried to put R = [ 0.5:0.5 :10 ] and sigma = 0.319./R , but in the nexts lines the program could not calculate other values. I have this error message:

MethodError: no method matching *(::Array{Float64,1}, ::Array{Float64,1}) 



using  NLsolve

include("C:\\Users\\Lucas\\Desktop\\LUCAS\\Julia\\brent.jl")

R = 0.5:0.5:10
       sigma = 0.319./R

Φ(r) = 2/15*sigma^9*(1/(r-1)^9-1/(r+1)^9-9/8/r*(1/(r-1)^8-1/(r+1)^8))-
                   sigma^3*(1/(r-1)^3-1/(r+1)^3-3/2/r*(1/(r-1)^2-1/(r+1)^2))

function Φl(r)
     ((3*sigma^3)/(2*r^2*(1+r)^2)+(3*sigma^3)/(r*(1+r)^3)-(3*sigma^3)/(1+r)^4
                                 -(3*sigma^9)/(20*r^2*(1+r)^8)
                                 -(6*sigma^9)/(5*r*(1+r)^9)
                                 +(6*sigma^9)/(5*(1+r)^10)
                                 -(3*sigma^3)/((r-1)^3*r)
                                 +(6*sigma^9)/(5*(r-1)^9*r)
                                 -(3*sigma^3)/(2*(r-1)^2*r^2)
                                 +(3*sigma^9)/(20*(r-1)^8*r^2)
                                 +(3*sigma^3)/(r-1)^4-(6*sigma^9)/(5*(r-1)^10))
end

function Φll(r)
     ((-(3*sigma^3)/(r^3*(1+r)^2))-(6*sigma^3)/(r^2*(1+r)^3)
                             -(9*sigma^3)/(r*(1+r)^4)+(12*sigma^3)/(1+r)^5
                             +(3*sigma^9)/(10*r^3*(1+r)^8)
                             +(12*sigma^9)/(5*r^2*(1+r)^9)
                             +(54*sigma^9)/(5*r*(1+r)^10)
                             -(12*sigma^9)/(1+r)^11+(9*sigma^3)/((r-1)^4*r)
                             -(54*sigma^9)/(5*(r-1)^10*r)
                             +(6*sigma^3)/((r-1)^3*r^2)
                             -(12*sigma^9)/(5*(r-1)^9*r^2)
                             +(3*sigma^3)/((r-1)^2*r^3)
                             -(3*sigma^9)/(10*(r-1)^8*r^3)
                             -(12*sigma^3)/(r-1)^5+(12*sigma^9)/(r-1)^11)
end



Veff(r,b,g) = Φ(r)+b^2*g^2/r^2
Veffl(r,b,g) = Φl(r)-2b^2*g^2/r^3
Veffll(r,b,g) = Φll(r)+6b^2*g^2/r^4



function rc0bc0gc0( sigma )
    rc0 = brent(r->3Φl(r)+r*Φll(r),1.02,5)
    gc0 = sqrt(Φ(rc0)+rc0*Φl(rc0)/2)
    bc0 = rc0*sqrt(gc0^2-Φ(rc0))/gc0
    rc0, bc0, gc0
end

rc0, bc0, gc0 = rc0bc0gc0(sigma)    # the error is here !
println("rc0 = $rc0, bc0 = $bc0, gc0 =  $gc0")

Upvotes: 0

Views: 705

Answers (1)

mbauman
mbauman

Reputation: 31342

I highly recommend reading through Julia's noteworthy differences from MATLAB. In particular:

  • In Julia, [x,y,z] will always construct a 3-element array containing x, y and z.

  • In Julia, a:b and a:b:c construct Range objects. To construct a full vector like in MATLAB, use collect(a:b). Generally, there is no need to call collect though. Range will act like a normal array in most cases but is more efficient because it lazily computes its values.

Put these two pieces together, and you'll see that [0.5:0.5:10] is an array with just one element in it! It's an array that contains a range. So your fix is simple: you want R = 0.5:0.5:10 (without brackets).

julia> R = 0.5:0.5:10
       sigma = 0.319./R
20-element Array{Float64,1}:
 0.638
 0.319
 ⋮

Upvotes: 5

Related Questions