Im trying to author a maplet which executes numerical integration on a function for an interval. Here is what I have so far.
This is a the part of the assignment that didn't require maplet format. (This part I got to work)
restart;
a:= 1; b := 3;
f:=x->100/x^2*sin(10/x);
L:=[];# which is list of lists
ListOfEndPts:=[b];
found := false;
MaxNoPasses:=70;
tolerance:=1.0e-4;
simpAB:=evalf(student[simpson](f(x),x=a..b,2));app:=0;
for PassNo from 1 to MaxNoPasses while not found do
print("**********************************************");
middle := (a+b)/2.0:
print(`Pass number` = PassNo);
print(`A` = a);
print(`B` = b);
print(`simpAB ` =simpAB);
simpAM := evalf(student[simpson](f(x),x=a..middle,2));
simpMB := evalf(student[simpson](f(x),x=middle..b,2));
sum1:=simpAM + simpMB;
print(`simpAM `=simpAM);
print(`simpAM + simpMB `=sum1);
Test:=abs(simpAB - (simpAM + simpMB));
print(`Test `=Test);
if Test <> 0 then
tail:=L[nops(L)];
a:=tail[1]:
b:=tail[2]:
simpAB:=tail[3];
L:=subsop(nops(L)=NULL,L);
else found:=true;
end if;
else
print("Split. Append [a,middle,SimpAM] to the list.Reset a to equal middle. Leave b alone");
L:=[op(L),[a,middle,simpAM]];
a:=middle;
simpAB:=simpMB;
print(`List `=L);print(`New a `=a,`New b `=b);
print(`List of endpoints`=ListOfEndPts);
print(`Approximation to date `=app);
end if;
end do:
print(` List of SORTED end points `=ListOfEndPts);
print(`Number of end points `=nops(ListOfEndPts));
app;
L1:=evalf(map(f,ListOfEndPts));
L:=zip( (x,y)->[x,y],ListOfEndPts,L1);
p1:=plots[pointplot](L,axes=BOXED,title="f(x) = 100/x^2*sin(10/x) on [1,3]");
p2:=plot(f(x),x=1..3);
plots[display]([p1,p2]);
Now what I need to do is construct a maplet that performs the same operation for given inputs.
Here is the maplet.(Doesn't work)
with(Maplets[Elements]):
Numint:=proc()
local f1,digits,L1,p1,p2,a,b,f,PassNo,middle,simpAM,simpAB,app,simpMB,Test,ListOfEndPts,sum1,tail,L,found,MaxNoPasses,tolerance,pass;
L:=[];
ListOfEndPts:=[b];
found := false;
pass:=0;
digits:=Get(TFd::algebraic);
a:=Get(TFa::algebraic);
b:=Get(TFb::algebraic);
f1:=Get(TFf::algebraic);
f:=x->f1;
MaxNoPasses:=Get(TFp::algebraic);
tolerance:=Get(TFt::algebraic);
simpAB:=evalf(student[simpson](f(x),x=a..b,2));
app:=0;
for PassNo from 1 to MaxNoPasses while not found do
pass:=pass + 1;
middle := (a+b)/2.0:
simpAM := evalf(student[simpson](f(x),x=a..middle,2));
simpMB := evalf(student[simpson](f(x),x=middle..b,2));
sum1:=simpAM + simpMB;
Test:=abs(simpAB - (simpAM + simpMB));
if Test <> 0 then
tail:=L[nops(L)];
a:=tail[1]:
b:=tail[2]:
simpAB:=tail[3];
L:=subsop(nops(L)=NULL,L);
else found:=true;
end if;
else
L:=[op(L),[a,middle,simpAM]];
a:=middle;
simpAB:=simpMB;
end if;
end do:
L1:=evalf(map(f,ListOfEndPts));
L:=zip( (x,y)->[x,y],ListOfEndPts,L1);
p1:=plots[pointplot](L,axes=BOXED,title="f(x) = 100/x^2*sin(10/x) on [1,3]");
p2:=plot(f(x),x=1..3);
##plots[display]([p1,p2]);##set plotter value
print(L1);
print(L);
Maplets:-Tools:-Set('TFapp'=evalf[digits](app));
Maplets:-Tools:-Set('TFmapapp'=int);
Maplets:-Tools:-Set('TFpass'=pass);
end proc:
maplet1:=Maplet([[" f(x) = ",TextField['TFf'](value='100/x^2*sin(10/x)'),"Digits Accuracy =",TextField['TFd'](width=5,value='10'),"Tolerance =",TextField['TFt'](width=5,value='1.0e-4'),"Maximum number of iterations =",TextField['TFp'](width=5,value='100'),"a =",TextField['TFa'](width=5,value='1'),"b =",TextField['TFb'](width=5,value='3')],["Adaptive approximation of the integral =",TextField['TFapp'](value='0'),"Maple approximation of the integral =",TextField['TFmapapp'](value='0'),"Number of iterations =",TextField['TFpass'](width=5,value='0')],[Plotter['PL']()],[Button("Close", Shutdown(""))] ]):
Maplets[Display](maplet1);
Any advice or suggestions are greatly appreciated.
Thanks.