/////////////////////////////////////////////////////////////////// 0. 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

math

 

////////////////////////////////////////// Program3. [Comparison of  first and second approximation of f(x)]///////////////////////////////////////////////////////////////

approx_1:=proc(f,a,b,k)/*connect segments*/

local t,curve,lines,myColors;

begin

myColors:=i->[RGB::Blue,RGB::Green,RGB::EnglishRed,RGB::BlueGrey,RGB::DarkGreen][_mod(i,5)+1]:

t:=m->a+(b-a)/k*m;

curve:=plot::Function2d(f(x),x=a..b,Color=RGB::Black,LineWidth=mymiddle);

lines:=plot::Function2d(f'(t(m))*(x-t(m))+f(t(m)),

      x=t(m-1/2)..t(m+1/2),Color=myColors(m),LineWidth=mywide)$m=0..k:

return(curve,lines);

end_proc:

 

approx_2:=proc(f,a,b,k)/*connect quadratic parabolas*/

local t,curve,parabolas,myColors;

begin

t:=m->a+(b-a)/k*m;

myColors:=i->[RGB::Blue,RGB::Green,RGB::EnglishRed,RGB::BlueGrey,RGB::DarkGreen][_mod(i,5)+1]:

curve:=plot::Function2d(f(x),x=a..b,Color=RGB::Black,LineWidth=mymiddle);

parabolas:=plot::Function2d(f''(t(m))/2*(x-(t(m)))^2+f'(t(m))*(x-t(m))+f(t(m)),

      x=t(m-1/2)..t(m+1/2),Color=myColors(m),LineWidth=mywide)$m=0..k:

return(curve,parabolas);

end_proc:

 

 

////////////////////////////////////Program4. [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

math

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////   MAIN START  /////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

////////////////////////////////////////////////////////////[Appendix]//////////////////////////////////////////////////////////

/////////////////////////////////////////////////// A.sin x ///////////////////////////////////////////////////////////////

A-1. Subtract parabola of 1st degree(in this case y=x) from f(x).   Make g(x)=sin(x)-x

plot(mover(x->x,sin,-PI..PI),

plot::Text2d("y=x",[2,2],HorizontalAlignment=Right,TextFont=myfont),

plot::Text2d("y=sin x",[1.5,1],VerticalAlignment=Top,TextFont=myfont),

plot::Text2d("y=sin(x)-x",[-2/3*PI,sin(-2/3*PI)+2/3*PI],VerticalAlignment=Bottom,

            HorizontalAlignment=Left,TextFont=myfont,TimeRange=23..23),

XTicksNumber=None,XTicksAt=[-PI="-p",-PI/2="-p/2",0="O",PI/2="p/2",PI="p"],YTicksNumber=Low,

TicksLabelFont=["symbol"].mytick,Scaling=Constrained,VisibleBeforeBegin=FALSE)

MuPAD graphics

 

 

A-2. Compare g(x) with y=a x^3 (parabola of 3rd degree)

plot(approx(3,x->sin(x)-x,-PI..PI,0,-1..0),

plot::Text2d(x->"y=".stringlib::formatf(float(x),2)." x^3",[-0.05,-0.05],

HorizontalAlignment=Right,VerticalAlignment=Top,TextFont=myfont,x=-1..0),

plot::Text2d("y=sin(x)-x",[-2.1,1],TextFont=myfont,HorizontalAlignment=Right),

XTicksNumber=None,XTicksAt=[-PI="-p",-PI/2="-p/2",0="0",PI/2="p/2",PI="p"],

TicksLabelFont=["symbol"].mytick,YTicksNumber=Low,Frames=100)

MuPAD graphics

 

A-3. Let's check the result.

limit((sin(x)-x)/x^3,x=0)

math

 

Let's check the graphs statistically.

plotfunc2d(sin(x),x-x^3/6,x, Scaling=Constrained,LineWidth=mywide,YRange=-3..3,x=-PI*2..PI*2,

XTicksNumber=None,XTicksAt=[-PI="-p",-PI/2="-p/2",0="0",PI/2="p/2",PI="p"],

TicksLabelFont=["symbol",Bold].mytick,YTicksNumber=Low,Colors=[RGB::Blue,RGB::SeaGreen,RGB::Green])

MuPAD graphics

 

/////////////////////////////////////////////////////////////////////////Appendix B.differentiablity and tangent//////////////////////////////////////////////////////////////////////////

 

"Tangent of f(x)=x^2 at the origin" is y=0. The more magnified the closer y=0 osculates f(x).

plot(plot::Function2d(x^2,x=-2..2,Mesh=1000),

plot::Function2d(0,x=-2..2,Color=RGB::IndianRed),

plot::Text2d("y=x^2",[1,0.7],HorizontalAlignment=Left,TextFont=myfont),

plot::Text2d("y=0",[1,0.1],TextFont=myfont),

LineWidth=mywide,Scaling=Constrained,TicksNumber=Low,TicksLabelFont=mytick)

 

   

MuPAD graphics

However  g(x)=|x| is not differentiable at the origin. Thus no matter how you magnify the graph, they keep separated.

Defferentiable means having a linear approximation.

plot(plot::Function2d(x,x=0..0.1,Mesh=100,Color=RGB::Blue),

plot::Function2d(-x,x=-0.1..0,Mesh=100,Color=RGB::Blue),

plot::Function2d(x,x=0.1..2,Color=RGB::Blue),

plot::Function2d(-x,x=-2..-0.1,Color=RGB::Blue),

plot::Function2d(0,x=-2..2,Color=RGB::IndianRed),

plot::Text2d("y=|x|",[1,0.7],TextFont=myfont),

plot::Text2d("y=0",[1,0.1],TextFont=myfont),

LineWidth=mywide,Scaling=Constrained,TicksNumber=Low,TicksLabelFont=mytick)

   

MuPAD graphics

 

/////////////////////////////////////////Appendix C. Comparison of 1st and 2nd approximations//////////////////////////////

C-1. [Tick=1,n=1] Tangents at x=-1,0,1,2 of f(x)=x^3-x^2+1.

 

f:=x->x^3-x^2+1:

plot(approx_1(f,-1,2,3),Scaling=Constrained,TicksNumber=Low,TicksLabelFont=mytick

     ,Footer="1st degree approximation. Ticks=1")

MuPAD graphics

C-2.[Tick=1, n=2] Quadratic palabolas at x=-1,0,1,2 of f(x)=x^3-x^2+1.

plot(approx_2(f,-1,2,3),Scaling=Constrained,TicksNumber=Low,TicksLabelFont=mytick

     ,Footer="2nd degree approximation. Ticks=1")

MuPAD graphics

C-3. [Tick=0.5,n=1] Tangents at x=0.5k(k=0,1,-1,2,-2...) of f(x)=x^3-x^2+1.

plot(approx_1(f,-1,2,6),Scaling=Constrained,

    TicksNumber=Low,TicksLabelFont=mytick

     ,Footer="1st degree approximation. Ticks=0.5")

MuPAD graphics

C-4. [Tick=0.5,n=2] Tangents at x=0.5k(k=0,1,-1,2,-2...) of f(x)=x^3-x^2+1.

plot(approx_2(f,-1,2,6),Scaling=Constrained,

     TicksNumber=Low,TicksLabelFont=mytick

     ,Footer="2nd degree approximation. Ticks=0.5")

MuPAD graphics

It is hard to distinguish from f(x) any more.