MATH52808/24/2018

Lab01: An introduction to TeXmacs, Mathematica

1TeXmacs

TeXmacs is an editor especially well suited for scientific work:

2Mathematica

Mathematica is a computational platform that combines symbolic, numerical and graphical calculations. Sample computations:

In[3]:=

10!

3628800

In[5]:=

p4=Expand[(x+y)^4]

x4+4x3y+6x2y2+4xy3+y4

In[6]:=

Factor[p4]

(x+y)4

In[8]:=

Integrate[1/(1+x^2),x]

tan-1(x)

In[10]:=

LaplaceTransform[Sin[t],t,s]

1s2+1

In[13]:=

FourierTransform[Exp[-x^2],x,k]

e-k242

In[14]:=

3Euler's method

An analytical solution to an ODE may be impossible. This usually happens for non-linear, higher-order ODEs, e.g.,

In[7]:=

ODE = y”[x] + x y[x] y'[x] == 0

y''(x)+xy(x)y'(x)=0

In[8]:=

DSolve[ODE,y[x],x]

DSolve[y''(x)+xy(x)y'(x)=0,y(x),x]

Even if an analytical solution is obtainable, it might be too complicated to work with

In[24]:=

f[x_,y_] = (x+y)^5;

ODE = y'[x] == f[x,y[x]]

y'(x)=(y(x)+x)5

In[25]:=

DSolve[ODE,y[x],x]

Solve[120(4log(y(x)+x+1)+(5-1)log((y(x)+x)2+12(5-1)(y(x)+x)+1)-(1+5)log((y(x)+x)2-12(1+5)(y(x)+x)+1)+210-25tan-1(4y(x)+4x-5-110-25)+22(5+5)tan-1(4y(x)+4x+5-12(5+5))-20x)=c1,y(x)]

In[26]:=

In such cases an approximation may be obtained numerically. Software systems like Mathematica contain sophisticated procedures that combine multiple algorithms to find a solution

In[42]:=

IniCond = y[0]==0

y(0)=0

In[43]:=

Y[x_] = y[x] /. NDSolve[{ODE,IniCond},y[x],{x,0,1}][[1,1]];

YN = Table[{x,Y[x]},{x,0,1,0.2}]

( 0. 0. 0.2 0.000010759 0.4 0.00068596 0.6 0.00806314 0.8 0.0516308 1. 0.348729 )

In[44]:=

The simplest numerical algorithm to approximate an ODE is solution is Euler's method

Yi+1=Yi+hf(xi,Yi),

where xi=x0+ih, h is the step size, and Yiy(xi). Euler's method is applied repeatedly within a loop to obtain the approximation

In[29]:=

h=0.2; x0=0.; y0=0.; xfinal=1.; nsteps=Floor[(xfinal-x0)/h]

5

In[45]:=

YE = {y0,0,0,0,0,0};

For[i=1, i<=nsteps, i++,

YE[[i+1]] = YE[[i]] + h f[x0 + i h, YE[[i]]]

];

{YN,YE}

( {0.,0.} {0.2,0.000010759} {0.4,0.00068596} {0.6,0.00806314} {0.8,0.0516308} {1.,0.348729} 0. 0.000064 0.00211364 0.0179415 0.0911634 0.400534 )

In[46]:=

Note that the approximate values from Euler's method are not the same as those from the Mathematica built-in NDSolve. In this lab we'll investigate why this happens.