I have writed a function to get integral from interpolation Lagrange polynom.
function fxn = lagrange()clc; clear;k = [0 1 2 3 4 5 6 7 8 9 10];xpoints = 0.3 * k;ypoints = 1 - exp(-xpoints);L = @(x) 0;for i = 1 : 11 Li = @(x) 1; Ld = @(x) 1; for j = 1 : 11 if i ~= j Li = @(x) Li(x) * (x - xpoints(j)); Ld = @(x) Ld(x) * (xpoints(i) - xpoints(j)); end end L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);endfprintf("integral from L(x): %f",integral(L, 0, 3));fxn = @(x) L(x);end
But when i run this code I have this message appears:
Error using * Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. Toperform elementwise multiplication, use '.*'.Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)Li(x)*(x-xpoints(j)) (line 12) Li = @(x) Li(x) * (x - xpoints(j));Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in lagrange>@(x)L(x)+(Li(x)/Ld(x))*ypoints(i) (line 16) L = @(x) L(x) + (Li(x)/Ld(x))*ypoints(i);Error in integralCalc/iterateScalarValued (line 314) fx = FUN(t);Error in integralCalc/vadapt (line 132) [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);Error in integralCalc (line 75) [q,errbnd] = vadapt(@AtoBInvTransform,interval);Error in integral (line 88)Q = integralCalc(fun,a,b,opstruct);Error in lagrange (line 23)fprintf("integral from L(x): %f",integral(L, 0, 3));
What i do wrong? How can i fix this?
Best Answer