/////////////////////////////////////////////////////////////////// programs ////////////////////////////////////////////////////////////////////////////
Setting parameters:
myfont=TextFont, mytick=TicksLabelFont, mywide, mymiddle, myslim are LineWidth.
For home use, myfont=15,mytick=10, for presentations myfont=30,mytick=15.
myfont:=[30]:mytick:=[15]:mywide:=0.9:mymiddle:=0.5:mywidth:=mywide:myslim:=0.3:
graphColor:=[RGB::Blue,RGB::LimeGreen,RGB::NavyBlue]:/*Colors of graphs*/
////////////////////////////////////program1. [parabola of osculation of n-th order]////////////////////////////
//////////////////////////////////////////Compare y=f(x)(x=xrange) and y=kx^n//////////////////////////
approx:=proc(n,f,xrange,center,krange)
local x1,x2,curve,curves;
begin
x1:=op(xrange,1);x2:=op(xrange,2);
curve:=plot::Group2d(
plot::Curve2d([x,f(x)],x=x1..(center-0.5),Color=RGB::NavyBlue,TimeRange=0..10),
plot::Curve2d([x,f(x)],x=(center-0.5)..(center+0.5),UMesh=500,Color=RGB::NavyBlue,TimeRange=0..10),
plot::Curve2d([x,f(x)],x=(center+0.5)..x2,Color=RGB::NavyBlue,TimeRange=0..10)
);
curves:=plot::Group2d(
plot::Function2d(k*(x-center)^n,x=x1..(center-0.5),k=krange,
AffectViewingBox=FALSE,Color=RGB::Black,Color=RGB::CobaltGreen),
plot::Function2d(k*(x-center)^n,x=(center-0.5)..(center+0.5),XMesh=500,k=krange,
AffectViewingBox=FALSE,Color=RGB::Black,Color=RGB::CobaltGreen),
plot::Function2d(k*(x-center)^n,x=(center+0.5)..x2,k=krange,
AffectViewingBox=FALSE,Color=RGB::Black,Color=RGB::CobaltGreen)
);
return(curve,curves,LineWidth=mywide,VisibleBeforeBegin=FALSE,VisibleAfterEnd=FALSE);
end_proc:
///////////////////////////////////////program2. [Substracte g(x) from f(x) by animation]//////////////////////////////////////////////////////
mover:=proc(f,g,xrange)
local x1,x2,origin1,origin2,origin3,origin4,vecs,curves,up_down,vecColor1,vecColor2;
begin
x1:=op(xrange,1): x2:=op(xrange,2):
vecColor1:=x->piecewise([x>0,RGB::IndianRed],[x<=0,RGB::ManganeseBlue]);/*original arrows*/
vecColor2:=x->piecewise([x>0,RGB::EnglishRed],[x<=0,RGB::SteelBlue]):/*moving arrows*/
up_down:=proc(f,g,t,a,b,t1)//sub procedure
local x1,x2,vec_origin1,vec_origin2,vec_transfered,curve_transfered;
begin
vec_origin1:=plot::Arrow2d([t,f(t)],[t,g(t)],TipLength=2*unit::mm,Color=vecColor1(g(t)-f(t)),
TimeRange=0..t1,VisibleAfterEnd=FALSE);
vec_origin2:=plot::Arrow2d([t,f(t)],[t,g(t)],TipLength=2*unit::mm,Color=vecColor1(g(t)-f(t)),
LineWidth=0.3,TimeRange=t1..t1+2,VisibleBeforeBegin=FALSE);
vec_transfered:=plot::Arrow2d([t+0.01,f(t)-k*f(t)],[t+0.01,g(t)-k*f(t)],k=0..1,
TimeRange=t1..t1+2,TipLength=2*unit::mm,Color=vecColor2(g(t)-f(t)));
curve_transfered:=plot::Function2d(g(x)-f(x),x=a..t,Color=graphColor[3]);
return(vec_origin1,vec_origin2,vec_transfered);
end_proc;//end of sub procedure
origin1:=plot::Function2d(f(x),x=xrange,Color=graphColor[2],LineWidth=0.75,TimeRange=0..21.9,
VisibleAfterEnd=FALSE);
origin2:=plot::Function2d(g(x),x=xrange,Color=graphColor[1],LineWidth=0.75,TimeRange=0..21.9,
VisibleAfterEnd=FALSE);
origin3:=plot::Function2d(f(x),x=xrange,Color=graphColor[2],LineWidth=0.5,TimeRange=21.9..22);
origin4:=plot::Function2d(g(x),x=xrange,Color=graphColor[1],LineWidth=0.5,TimeRange=21.9..22);
vecs:=up_down(f,g,x1+m*(x2-x1)/20,x1,x2,m,20)$m=0..20;
curves:=plot::Function2d(g(x)-f(x),x=(x1+(m-1)*(x2-x1)/20)..(x1+m*(x2-x1)/20),
TimeRange=m+2..22,Color=graphColor[3],LineWidth=0.75)$m=1..20;
return(origin1,origin2,origin3,origin4,vecs,curves,VisibleBeforeBegin=FALSE,
VisibleAfterEnd=TRUE,LineWidth=0.52)
end_proc
![]()
////////////////////////////////////Program3. [comparison with y=1-cos x & y= ax^2]/////////////////////////////
approxOfcos:=proc()
option escape;
local c,parabola,str,text2,text3;
begin
str:=k->"a=-".expr2text(k);
c:=plot::Function2d(cos(x),x=-PI..PI,Color=RGB::NavyBlue);
parabola:=plot::Function2d(1-k*x^2,Color=RGB::SeaGreen,
AffectViewingBox=FALSE,x=-PI..PI,k=0..1);
text2:=plot::Text2d(k->"y=1-".stringlib::formatf(k,2)."*x^2",[1.1,1-(1)^2*k],
HorizontalAlignment=Left,TextFont=myfont,k=0..1);
text3:=plot::Text2d("y=cos x",[-PI,-1],
HorizontalAlignment=Left,VerticalAlignment=Top,TextFont=myfont,k=0..1);
return(c,parabola,text2,text3);
end_proc
![]()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////// MAIN START /////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////// 1.cos x ///////////////////////////////////////////////////////////////
1-1. Static way of finding coeffients: y=cos x & y=1-1/2x^2
plotfunc2d(cos(x),1-x^2/2,x=-PI..PI,LineWidth=0.7,Header="y=cos(x)&y=1-x^2/2",
XTicksNumber=None,XTicksAt=[-PI="-p",-PI/2="-p/2",0="0",PI/2="p/2",PI="p"],
TicksLabelFont=["symbol"].mytick,YTicksNumber=Low,LineWidth=mywide,
YRange=-2..2,Scaling=Constrained,Colors=[RGB::NavyBlue,RGB::SeaGreen])

1-2. Interactive way of finding coeffients: y=1-ax^2&y=cos x
plot(approxOfcos(),XTicksNumber=None,XTicksAt=[-PI="-p",-PI/2="-p/2",0="0",PI/2="p/2",PI="p"],
TicksLabelFont=["symbol"].mytick,YTicksNumber=Low,Scaling=Constrained,LineWidth=mywide)

///////////////////////////////////////////////////////2. y=e^x /////////////////////////////////////////////////
2-1. first approximation of e^x
plotfunc2d(exp(x),1+x,x=-3..3,YRange=-1..5,
Colors=[RGB::NavyBlue,RGB::Green],
LineWidth=mywide,Scaling=Constrained,TicksNumber=Low,TicksLabelFont=mytick)

2-2. Substaract linear approximation from e^x. Let's make g(x)=e^x-(x+1).
plot(mover(x->(x+1),exp,-3..2),
plot::Text2d("y=e^x",[1.6,5],HorizontalAlignment=Right,TextFont=myfont),
plot::Text2d("y=x+1",[2,3],HorizontalAlignment=Left,TextFont=myfont),
plot::Text2d("y=e^x-(x+1)",[-3.5,2.2],HorizontalAlignment=Left,TextFont=myfont,TimeRange=23..23),
Scaling=Constrained,VisibleBeforeBegin=FALSE,TicksNumber=Low,TicksLabelFont=mytick)

2-3. Compare g(x)=e^x-(x+1) with y=ax^2. And check the result by limit.
plot(approx(2,x->exp(x)-(x+1),-2..2,0,0..1),
plot::Text2d("y=e^x-(x+1)",[1.9,4],HorizontalAlignment=Right,TextFont=myfont),
plot::Text2d(k->"y=".stringlib::formatf(float(k),2)." x^2",[-2,4*k^2],
HorizontalAlignment=Left,TextFont=myfont,k=0..1),
TicksNumber=Low,TicksLabelFont=mytick,Frames=100
)

limit((exp(x)-x-1)/x^2,x=0)
![]()
2-4. Now that the 2nd approximation of e^x= 1+x+x^2/2.
Let's make h(x)=e^x-(1+x+x^2/2). And compare h(x) with y=ax^2. And check the result by limit.
plot(mover(x->1+x+x^2/2,exp,-3..2),
plot::Text2d("y=e^x",[1.8,6],HorizontalAlignment=Right,TextFont=myfont),
plot::Text2d("y=1+x+x^2/2",[2,5],HorizontalAlignment=Left,TextFont=myfont),
plot::Text2d("y=e^x-(1+x+x^2/2)",[1.4,2.5],TimeRange=23..23,
HorizontalAlignment=Left,TextFont=myfont),
TicksNumber=Low,TicksLabelFont=mytick,Scaling=Constrained,VisibleBeforeBegin=FALSE)

2-5. Compare h(x)=e^x-(1+x+x^2/2) with y=ax^3. And check the result by limit.
plot(approx(3,x->exp(x)-1-x-x^2/2,-2..2,0,0..0.5),
plot::Text2d("y=e^x-(1+x+1/2*x^2)",[1.7,1.5],HorizontalAlignment=Right,TextFont=myfont),
plot::Text2d(k->"y=".stringlib::formatf(float(k),2)." x^3",[-1.2,-2*k],
HorizontalAlignment=Left,VerticalAlignment=Top,TextFont=myfont,k=0..0.5),
TicksNumber=Low,TicksLabelFont=mytick,Frames=100
)

Check the result by limit.
limit((exp(x)-1-x-x^2/2)/x^3,x=0)
![]()
2-6. Now that the 3rd approximation of e^x= 1+x+x^2/2!+x^3/3!.
Let's compare those approximations together.
plotfunc2d(exp(x),1+x,1+x+x^2/2,1+x+x^2/2+x^3/6,x=-3..3,YRange=-1..5,
LineWidth=mywide,Scaling=Constrained,TicksLabelFont=mytick,
Colors=[RGB::NavyBlue,RGB::Green,RGB::Blue,RGB::SeaGreen])
