How to work with intervals values in julia


#1

my program have a

sigma = 0.6

, but i must change to

sigma = 0.319 / R

, for R in [0.5:0.5:10]

and in the final line of the program i want to calculate an integral, and this integral depends of the R value … for each R value i have a integral value … and i have to print a table of these values …

how can i do that ?

i tried

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

but is not working


#2

This is not a interval. 0.5:0.5:10 is a range (a compact representation of an array) and if by interval you mean this array then you should just not put it in another array. i.e. R = 0.5:0.5:10. If by interval you mean something else than you need to be more specific.

Assuming you actually want the range 0.5:0.5:10 I’ll assume that you get the idea of using [0.5:0.5:10] from matlab. [...] in matlab corresponds to [...;] in julia which concatenate the array so [0.5:0.5:10;] in julia works too although it adds an unnecessary convertion.


#3

i have this error

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


#4

What did you do to get that error?


#5

for a constant sigma value, = 0.6 for example, i calculated many things that i used later

so, when i do R=0.5:0.5:10; the values that i calculate can not calculate for many sigma values … and give this

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 answers is here - three different values for a constant sigma ) 

when i use R table values, gives this error


#6

Please give a minimum working example, that is a piece of code that is short and complete that anybody can execute that shows the problem.


#7

When you post code, please post COMPLETE code, i.e. one can just copy-paste and run it. (The original one is good, the new one isn’t) Also please paste COMPLETE error and especially please READ and include backtrace. You were asking how to get sigma and now it’s pretty clear that your error is not at all from there anymore and in this case, please mention that you are asking questions in a different context now. Continuing in a single thread until you’ve fixed all problems in your code is certainly fine but you should explicitly state that or it is pretty confusing what you are actually asking about.

Note that AFAICT sigma isn’t being used in your function, (or it’s not called sigma I can’t tell). But if you are using a function that works with scalar input you should first figure out if it can accept vector input. Or you should use the rc0bc0gc0.(sigma) syntax to get an array of output corresponding to an array of input.


#8

Easy there, Yichao. Please try to give new folks gentler nudges in favor of complete verifiable examples and be a little more gracious about their problems. I wouldn’t be surprised if the error message here was indeed a downstream problem that was a direct result from 0.319 ./ [0.5:0.5:10] not doing what leaoshark expected.

At the same time, @leaoshark, please don’t post the same problem multiple places simultaneously. I posted an answer at Stack Overflow before seeing this thread here. It just ends up duplicating effort and splintering the conversation. https://stackoverflow.com/questions/46740395/use-a-loop-in-julia-table-values


#9
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 comes here !
println("rc0 = $rc0, bc0 = $bc0, gc0 =  $gc0")

#10

A few issues I can see,

  1. You are not actually using sigma in your function at all and is
    rather using it as a global variable so changing what you pass to the
    rc0bc0gc0 function won’t make any difference.

  2. You are using sigma (as well as other values) as scalar. As I’ve
    already said, you should figure out if you want to keep it that way
    (and apply rc0bc0gc0 on each of the scalar values individually) or if
    you want to “vectorize” rc0bc0gc0.

    The two should be almost equivalent though one will give you an
    array of 3-tuples (since that’s the return value of rc0bc0gc0) and the
    other will give you three arrays.
    After you fix the function parameter (i.e. 1), you can fix this by
    adding . to appropriate places (i.e. where you want to apply element-wise
    operation on the array)


#11

So, in every sigma i will substitute for .sigma ??

can you be more specific ? what is the best method for me ?

the error is

DimensionMismatch(“Cannot multiply two vectors”)


#12

No I mean https://docs.julialang.org/en/latest/manual/arrays/#Array-and-Vectorized-Operators-and-Functions-1

I don’t know what you want so I’m not sure what’s better.

To repeat,

Also, on a closer look, I think you are still not posting the complete code. It’s unclear what brent.jl is. And you should also quote your code as code so that the format isn’t messed up and there isn’t missing characters. It’s unclear if it’s caused by formatting or mistake in the code that it’s very hard to tell where each variables are defined.


#13

Yes, this is still far too hard for us to discern what’s going on here. Stack Overflow has some very nice advice on how to make it easier for folks to answer your question. Please take some time to read through this guide:

Am I understanding you correctly that this script works when sigma is a single number, but it fails when you change it into a vector of values?


#14

@mbauman correct ! if sigma is a single number, the program works well … when sigma is a vector, the program fails … any ideas ?


#15

@yuyichao send me an e-mail and i send all code to you , for you help me lucas.fisica@hotmail.com


#16

This could be the source of the error.

julia> (0.5:0.5:10)^3
ERROR: DimensionMismatch("Cannot multiply two vectors")

Try sigma.^3 instead if what you want is component-wise exponentiation, everywhere there is ^.


#17

The quoted code looks much better now. It looked really strange probably because some strange interference from unicode?

Anyway, it seems that the main issue is still what I mentioned in How to work with intervals values in julia

You should change Φ, Φl, Φll, Veff, Veffl, Veffll to all take sigma as parameter and pass it around. You still need to decide what you want to get in the end though. Do you want three arrays of numbers, or an array of 3-tuples. As I’ve said, that determines where you need to add the .s and the two options would be in rc0bc0gc0 or on rc0bc0gc0 (i.e. modifying the function to call other functions in it with the . syntax or calling the function itself with the . syntax). It seems you should only need to fix rc0bc0gc0 and can leave the rest of the functions along though, after adding the sigma parameter.


#18

@leaoshark This is homework, isn’t it? I just spotted the ( 20 points ) over at your Stack Overflow question.

Don’t try to get folks to do your homework for you.


#19

i tried to put sigma.^ in all … but still get error in multiplyng vector … can you test it in your computer?


#20

That’s exactly what I said not to do. The only thing you need to change is the last function or how you call it.