Interpolate function using Spline and Lagrange interpolation and take definite integral from it

I have the function f(x) = 1 - exp(-x); xk = 0.3*k; k = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; integral from 0 to 3; I have found how take integral from f(x), spline function. I have not found lagrange function. How i can take integral from spline function and how i can interpolate with lagrange and then take integral from it.
when i take integral from spline i have this message in command windows
Error using integralCalc/finalInputChecks (line 522)
Input function must return 'double' or 'single' values. Found 'struct'.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
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 lab1 (line 19)
clc;
clear;
k = [0 1 2 3 4 5 6 7 8 9 10];
xpoints = 0.3 * k;
ypoints = 1 - exp(-xpoints);
%f(x) function and integral
f = @(x) 1 - exp(-x);
fplot(f, [0 3.0]);
grid on;
integralFx = integral(f, 0, 3);
fprintf("%f\n", integralFx);
%s(x)
f(xpoints);
s = @(x) spline(xpoints, ypoints);
integral (s, 0, 3);

 Respuesta aceptada

I'm not at all sure
f(xpoints);
s = @(x) spline(xpoints, ypoints);
integral (s, 0, 3);
what you think the line f(xpoints); does? All it does is evaluate the function f at the vector xpoints, then it dumps the result into the bit bucket, never to be seen again.
Next, the line where you create s? It does not create a function you can evaluate. You can do that as:
s = spline(xpoints,ypoints);
S = @(x) ppval(s,x);
integralSx = integral(S, 0, 3)
integralSx =
2.0497680394453

2 comentarios

thx, i think that is what i need because results is close with other programms results. Try to find function for lagrange interpolation and take integral from lagrange polynom by myself.
You were close. Now you need to write a function that does Lagrange interpolation. Use integral the same way.
Thus if I call the function S at a list of points,
S([.1 2.3])
ans =
0.0949948404628685 0.899741790030172
it gives me a result, just as if I had called the function f.
So you need to write that Lagrange interpolation function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Feb. de 2019

Comentada:

el 23 de Feb. de 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by