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

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 :

And here is my code:

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)

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

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

1 Like

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:

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]) ```

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

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
with integer values for n and k you can use binomial from Julia built-in

2 Likes

I see. Thankyou so much.

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