Answer on Question #44630 - Math - Other
Let be defined on . For each positive integer , let be the Lagrange interpolation polynomial of at the equidistant points in . Given a positive error , write a function that computes the smallest value of so that . For the test case, you can take .
As and usually during programming we don't consider functions as equivalence classes, we can assume that Thus, below is written a script which implements required function
Listing 1 (from MatLab): Q44630.m
1 clc
2 clear all
3 %enter error
4 delta=0.0001
5 n=3;
6
7 syms x
8 f=exp(x);
9 err=1;
10 while err>delta
11 X=-1:1/(n-2):1;
12 Y=exp(X);
13
14 %Lagrange
15
16 N=length(X);
17 W=1;
18 for j=1:N
19 W-W*(x-X(j));
20 end;
21 W1=diff(W,x,1);
22 L=0;
23 for i=1:N
24 T=subs(W1,x,X(i));
25 L=L+W.*Y(i)./(T.*(x-X(i)));
26 end;
27 Lnew=simplify(L);
28 Lnew;
29 W=exp(x)-Lnew;
30 W=abs(W);
31 g=matlabFunction(W);
32 m=fminbnd(g,-1,1);
33 err=double(subs(abs(W),x,m))
34
35 n=n+1;
36 end;
37 err
38 n-1www.AssignmentExpert.com