How to work with intervals values in julia

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

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.

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.

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. How to change a single value to a vector - JULIA - Stack Overflow

1 Like
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")

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)

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”)

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.

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?

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

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

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 ^.

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 - #10 by yuyichao

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.

@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.

5 Likes

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

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.

any ideas how to do that ? can you do that for me, as a favor ?

Which part is not clear? I’ve given you two options,

  1. If you want to get arrays of tuples

    Call rc0bc0gc0 with . syntax.

  2. If you want to get three arrays

    Change rc0bc0gc0 to use the . syntax.

In general, you need to add .

or in another word, when you have a function that works and only works on a scalar and you want to apply that function to all the elements. All what you need to do is to identify what function you want to work on scalar and which on vector and add use the . (i.e. broadcast) syntax.

No.

ok … thanks for your time … i appreciate that … i tried to put . in rc0bc0gc0 , but not working … but thanks … see you …

If you did some change and still get an error and still want help, you need to post what you’ve changed and what’s the error you get. It’s impossible to guess what you are doing.