# Question to range( sin, cos)

**URL:** https://discourse.julialang.org/t/question-to-range-sin-cos/98896
**Category:** New to Julia
**Tags:** question, plotting
**Created:** [May 15, 2023, 7:21pm UTC](https://discourse.julialang.org/t/question-to-range-sin-cos/98896 "2023-05-15T19:21:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![msreo0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/msreo0/32/48038_2.png) [@msreo0](https://discourse.julialang.org/u/msreo0)
#### Post date: [May 15, 2023, 7:21pm UTC](https://discourse.julialang.org/t/question-to-range-sin-cos/98896/1 "2023-05-15T19:21:40Z")

</div>

hi guys, I just wanted to plot a graph with Julia, but don’t know how to set interval of the function. the function is (1-cos(x))/x, and x != 0 & x \<\<1. I tried to write

x = range(0.1, 0.2 , length=100)

y = (1-cos.(x))/x

using Plots  
plot(x, y)

but this doesn’t work. can somebody help me how to write x !=0 and x \<\<1 as an interval?

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [May 15, 2023, 7:51pm UTC](https://discourse.julialang.org/t/question-to-range-sin-cos/98896/2 "2023-05-15T19:51:21Z")

</div>

Use triple-backticks ``` to format code blocks nicely.

The issue you’re running into here is that you’re not broadcasting as many things as you need to be with `.`. Either of the below will work:

```julia
y = (1 .- cos.(x)) ./ x # add "." to every part manually
y = @. (1 - cos(x)) / x # "@." adds "." to every part automatically

```

I don’t know what plotting package you’re using so I can’t help with the `plot` part, but with the above fix it will probably work anyway.

I don’t understand the part about “x != 0 and x \<\< 1”, but maybe that isn’t a problem after the above changes. If you have questions about that, you’ll probably need to rephrase and ask it again.

---

<div class="post-metadata">

### Author: ![msreo0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/msreo0/32/48038_2.png) [@msreo0](https://discourse.julialang.org/u/msreo0)
#### Post date: [May 15, 2023, 8:03pm UTC](https://discourse.julialang.org/t/question-to-range-sin-cos/98896/3 "2023-05-15T20:03:15Z")

</div>

Thank you for ur answer first, my question was incomplete. so the function is (1-cos(x))/x, with x !=0 and  
|x|\<\<1, so there is no stated interval, if that makes sense. but I will try again with ur answer. Thank you again

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [May 16, 2023, 2:24pm UTC](https://discourse.julialang.org/t/question-to-range-sin-cos/98896/4 "2023-05-16T14:24:53Z")

</div>

Your approach still works.

```julia
x = range(-1, 1, 101) # (Type ?range in REPL to learn about the arguments.)
y = @. (1 - cos(x)) / x
plot(x, y)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/9/c91e8b1db62f0a3d1e7aa02a9d9973f1f301b0c9.png)

Note that zero is excluded from your range because `0/0` is undefined. In Julia (and most other languages) a `0/0` operation becomes `NaN`. [NaN](https://en.wikipedia.org/wiki/NaN) values are fine to store in your data. They just won’t be plotted. That is why there is a gap in the line plot around zero.

```julia
julia> y[51]
NaN

```

Alternatively, you could just avoid having 0 in your `x` vector. Then there won’t be any `0/0` calculations involved.

```julia
julia> x = range(-1, 1, 100);

julia> y = @. (1 - cos(x)) / x;

julia> x[50]
-0.010101010101010102

julia> x[51]
0.010101010101010102

julia> plot(x, y)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/4573cf6f318f9d1224e7e07c0172d0beba1bf64c.png)

Finally, note that the `plot` function also has a method that will take a function and a range, so you don’t have to bother creating an `x` vector and broadcasting it into `y`.

```julia
julia> f(x) = (1 - cos(x)) / x
f (generic function with 1 method)

julia> plot(f, -1, 1)

```
