# Plotting a 2D graph using a self-defined Bernstein function

**URL:** https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671
**Category:** New to Julia
**Tags:** plotting
**Created:** [April 29, 2019, 9:59pm UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671 "2019-04-29T21:59:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [April 29, 2019, 9:59pm UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/1 "2019-04-29T21:59:08Z")

</div>

I am trying to draw a graph like the one shown below. Is there anyway to define the function better and draw the graph better too?  
Here is the graph :

 ![hope](https://global.discourse-cdn.com/julialang/original/3X/f/8/f83bbf6375751640fc6742b308485df08727bffb.png)

And here is my code:

```julia
using Plots
using PyPlot

T = range(0,stop=1,length=100)
#simple self-defined Berstein function
function Bt(n, i)
     [B(n, i, t) for t in T]
    return
end

fig = plt.figure()
ax = fig[:add_subplot](111, projection="2d")
n = 3
for i in range(0, stop=n+1, length=25)
plot(T,Bt, markersize=6, markevery=10, label=r'$B^{{{0}}}_{{{1}}}$)'.format(i, n)
```

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [April 29, 2019, 10:49pm UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/2 "2019-04-29T22:49:38Z")

</div>

Based on the information you provided, I believe this is approximately the code you want

```nohighlight
using PyPlot
n = 3
T = range(0,stop=1,length=100)
S = range(0,stop=n+1,length=25)
Bt(n, i) = [B(n, i, t) for t in T]
for i in S
    plot(T,Bt(n,i),marker="^",markersize=6, markevery=10,grid(true))
end
legend([latexstring("B^{$n}_{$i}") for i in S])

```

assuming you have the `B(n,i,t)` method defined

---

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [April 30, 2019, 8:21am UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/3 "2019-04-30T08:21:32Z")

</div>

Yes, i do have `B(n,i,t)` method defined. However am having a huge error which I know stems from my user defined function `comb` which uses factorial. I kept getting the error the factorial method only matches `Int128,BigFloat and uint128` hence the `n and k are int128` even though my maximum range for n is only 20.

Once again if there is an simpler way to this…kindly show me. Your help is appreciated.

Here is my entire code with comments on my traill of thought:

````julia
using PyPlot

#create&define combination function
function Comb!(n::Int128, k::Int128)
    #comb
    return factorial(n)/(factorial(n-k) * factorial(k))
end

#create&define a variable binom that will store all my combinations for k IN A RANGE OF N 
binom = ([Comb(n, k) for k in range(0,stop=n+1,length=100)] for n in range(0,stop=20,length=100))

#create&define function for a basic (simplest berstein)
function B(n, i, t)
    #B(n,i,t)
    return binom[n][i] * (t^i) * (1-t)^(n-i)
end

#define variables and draw polinomial berstein
n = 3
T = range(0,stop=1,length=100)
S = range(0,stop=n+1,length=25)

Bt(n, i) = [B(n, i, t) for t in T]
for i in S
    plot(T,Bt(n,i),marker="^",markersize=6, markevery=10,grid(true))
end
legend([latexstring("B^{$n}_{$i}") for i in S]) ```
````

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [April 30, 2019, 11:51am UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/4 "2019-04-30T11:51:10Z")

</div>

Julia already has a `binomial` coefficient function built-in; however, you need to use **integer** ranges

```plaintext
using PyPlot
n = 3 # or Int128(3)
T = range(0,stop=1,length=100)
B(n, i, t) = binomial(n,i) * (t^i) * (1-t)^(n-i)
Bt(n, i) = [B(n, i, t) for t in T]
for i in 0:n
    plot(T,Bt(n,i),marker="^",markersize=6, markevery=10)
end
legend([latexstring("B^{$n}_{$i}") for i in 0:n])
grid("on")

```

your code sample assumes you can define the `binomial(n,k)` for non-integer `k`, which is undefined

![bernstein_3](https://global.discourse-cdn.com/julialang/original/3X/6/b/6bd11628116dee6ed377ae8d3a6d1701e55b6e15.png)  
with integer values for `n` and `k` you can use `binomial` from Julia built-in

---

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [April 30, 2019, 3:23pm UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/5 "2019-04-30T15:23:16Z")

</div>

I see. Thankyou so much.

---

<div class="post-metadata">

### Author: ![mforets](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mforets/32/298_2.png) [@mforets](https://discourse.julialang.org/u/mforets)
#### Post date: [April 30, 2019, 8:47pm UTC](https://discourse.julialang.org/t/plotting-a-2d-graph-using-a-self-defined-bernstein-function/23671/6 "2019-04-30T20:47:01Z")

</div>

Hi Angela, are you working with Bernstein expansions? We got his half-baked package: [https://github.com/JuliaReach/BernsteinExpansions.jl](https://github.com/JuliaReach/BernsteinExpansions.jl) If you find anything interesting and want to contribute, external help is greatly appreciated 😉
