# How to work with intervals values in julia

**URL:** https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433
**Category:** General Usage
**Created:** [October 14, 2017, 1:48am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433 "2017-10-14T01:48:01Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 14, 2017, 1:48am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/1 "2017-10-14T01:48:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2017, 2:52am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/2 "2017-10-14T02:52:30Z")

</div>

> [@leaoshark](#):
>
> [0.5:0.5:10]

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.

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 14, 2017, 3:00am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/3 "2017-10-14T03:00:57Z")

</div>

i have this error

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2017, 3:18am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/4 "2017-10-14T03:18:57Z")

</div>

What did you do to get that error?

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 14, 2017, 3:22am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/5 "2017-10-14T03:22:42Z")

</div>

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

```julia
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

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [October 14, 2017, 3:42am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/6 "2017-10-14T03:42:52Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2017, 3:43am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/7 "2017-10-14T03:43:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 14, 2017, 4:20am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/8 "2017-10-14T04:20:37Z")

</div>

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](https://stackoverflow.com/questions/46740395/use-a-loop-in-julia-table-values)

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 14, 2017, 2:11pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/9 "2017-10-14T14:11:16Z")

</div>

```julia
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")

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2017, 4:15pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/10 "2017-10-14T16:15:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 14, 2017, 5:55pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/11 "2017-10-14T17:55:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 14, 2017, 6:24pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/12 "2017-10-14T18:24:05Z")

</div>

> [@leaoshark](#):
>
> So, in every sigma i will substitute for .sigma ??

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

> [@leaoshark](#):
>
> what is the best method for me

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

To repeat,

> [@yuyichao](#):
>
> 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.

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](http://commonmark.org/help/) 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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 14, 2017, 10:16pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/13 "2017-10-14T22:16:55Z")

</div>

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:

> **[How to create a Minimal, Reproducible Example - Help Center](https://stackoverflow.com/help/minimal-reproducible-example)**
>
> Stack Overflow | The World’s Largest Online Community for Developers

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?

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 15, 2017, 1:38am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/14 "2017-10-15T01:38:14Z")

</div>

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

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 15, 2017, 1:40am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/15 "2017-10-15T01:40:28Z")

</div>

@yuyichao send me an e-mail and i send all code to you , for you help me [lucas.fisica@hotmail.com](mailto:lucas.fisica@hotmail.com)

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [October 15, 2017, 2:32am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/16 "2017-10-15T02:32:06Z")

</div>

This could be the source of the error.

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 15, 2017, 3:00am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/17 "2017-10-15T03:00:30Z")

</div>

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](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/10)

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](https://docs.julialang.org/en/latest/manual/arrays/#Array-and-Vectorized-Operators-and-Functions-1) 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.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 15, 2017, 3:31am UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/18 "2017-10-15T03:31:56Z")

</div>

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

---

<div class="post-metadata">

### Author: ![leaoshark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leaoshark/32/11462_2.png) [@leaoshark](https://discourse.julialang.org/u/leaoshark)
#### Post date: [October 15, 2017, 9:00pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/19 "2017-10-15T21:00:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [October 15, 2017, 9:11pm UTC](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433/20 "2017-10-15T21:11:09Z")

</div>

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.

[Next page](https://discourse.julialang.org/t/how-to-work-with-intervals-values-in-julia/6433.md?page=2)
