MATLAB: What’s going on about the code?? I used the same code the teammate used, but mine keep showing error

can anyone tell me what's wrong about my f function and could you explain why it happens? thank you

x0=0;x1=1/2;x2=1;
>> L0 = @(x)((x-x1).*(x-x2)/((x0-x1)*(x0-x2)))
L0 =
@(x)((x-x1).*(x-x2)/((x0-x1)*(x0-x2)))
>> L1 = @(x)((x-x0).*(x-x2)/((x1-x0)*(x1-x2)))
L1 =
@(x)((x-x0).*(x-x2)/((x1-x0)*(x1-x2)))
>> L2 = @(x)((x-x0).*(x-x1)/((x2-x0)*(x2-x1)))
L2 =
@(x)((x-x0).*(x-x1)/((x2-x0)*(x2-x1)))
>> f = @(x) sin(pi*x/2)./((1+x).^(1/2))
f =
@(x)sin(pi*x/2)./((1+x).^(1/2))
t=0:0.01:1
p = @(x)(f(x0)*L0+f(x1)*L1+f(x2)*L2)
p =
@(x)(f(x0)*L0+f(x1)*L1+f(x2)*L2)
>> p(t)
Undefined function 'mtimes' for input arguments of type 'function_handle'.
Error in @(x)(f(x0)*L0+f(x1)*L1+f(x2)*L2)

Best Answer

You defined ‘L0’, ‘L1’, and ‘L2’ as a functions here:
L0 = @(x)((x-x1).*(x-x2)/((x0-x1)*(x0-x2)))
L1 = @(x)((x-x0).*(x-x2)/((x1-x0)*(x1-x2)))
L2 = @(x)((x-x0).*(x-x1)/((x2-x0)*(x2-x1)))
but did not give any of them arguments here:
p = @(x)(f(x0)*L0+f(x1)*L1+f(x2)*L2)
I am not certain what you want to do, but if you include them as functions of ‘x’, it works.
Example:
p = @(x)(f(x0).*L0(x)+f(x1).*L1(x)+f(x2).*L2(x));