Question: Want to define piecewise function using some already computed function

Hi friends! Hope everything is fine here

I want to generate a piecewise function using some already computed functions. Like I compute B[0], B[1], B[2] and B[3] using some formula which are given as,

B[0] := (1/6)*x^3/h^3;
B[1] := (1/6)*(4*h^3-12*h^2*x+12*h*x^2-3*x^3)/h^3;
B[2] := -(1/6)*(44*h^3-60*h^2*x+24*h*x^2-3*x^3)/h^3;
B[3] := (1/6)*(64*h^3-48*h^2*x+12*h*x^2-x^3)/h^3;

Now, I want to define the corresponding piecewise function as

piecewise(x <= 0, 0, 0 < x and x <= h, B[0], `and`(h < x, x <= 2*h), B[1], `and`(2*h < x, x <= 3*h), B[2], `and`(3*h < x, x <= 4*h), (B[3], 0)

similarly for already computed functions B[0], B[1], B[2], B[3] and B[4] which are given as

B[0] := (1/24)*x^4/h^4;
B[1] := -(1/24)*(5*h^4-20*h^3*x+30*h^2*x^2-20*h*x^3+4*x^4)/h^4;
B[2] := (1/24)*(155*h^4-300*h^3*x+210*h^2*x^2-60*h*x^3+6*x^4)/h^4;
B[3] := -(1/24)*(655*h^4-780*h^3*x+330*h^2*x^2-60*h*x^3+4*x^4)/h^4;
B[4] := (1/24)*(625*h^4-500*h^3*x+150*h^2*x^2-20*h*x^3+x^4)/h^4;

I want to define the corresponding piecewise function as

piecewise(x <= 0, 0, 0 < x and x <= h, B[0], `and`(h < x, x <= 2*h), B[1], `and`(2*h < x, x <= 3*h), B[2], `and`(3*h < x, x <= 4*h), B[3], `and`(4*h < x, x <= 5*h), B[4], 0)

Can someone please let me know a general procedure (using seq command, loop etc.) to define piecewise function if B[0], B[1], B[2],...,B[M] are known? I shall be very thankful for your answer.

Please Wait...