Using CAS Sessions in TEXMACS
Total Page:16
File Type:pdf, Size:1020Kb
Using CAS sessions in TEXMACS One important feature of TeXmacs is that users can "execute" external computer algebra system (CAS) within worksheet. Supported CAS’s include: maxima octave gnuplot yacas R qcl etc. The detailed menu will be listed in [Insert] [Session] according to exactly CAS’s installat- tions1 in your system. → 1. Octave When you choose from the [Octave] choice from [Insert] [Session] or from submenu in middle line, octave will be invoked and you can input the octave→ command after the prompt as follows: octave> A=[1 0 0 0;2 2 0 0;-1 0 2 0;0 -1 2 2] octave> A^2 octave> A.^2 octave> eig(A) octave> [U,V]=eig(A) octave> V.^1 octave> U.^1 octave> A’ octave> 1.1. Linear System Equations The following equation can be solved only by division: ax = b x = b/a if a 0 ⇒ The method for solving linear system equation is similar as follows: I I 1 I Am nXI n 1 = bm 1 Xn 1 = Am− nbm 1 × × × ⇒ × × × And octave support the operation for such solution: I 1 I I XI n 1 = Am− nbm 1 Xn 1 = Am n bm 1 × × × ⇒ × × × / Example 1. Solve the linear system of equations: x + x = 2 − 1 2 2x + 3x = 1 1 2 − This system of equations can be rewritten as: 1 1 x 2 1 = −2 3 x 1 2 − Now the solution by octave is: octave> A=[-1 1; 2 3];b=[2;-1] octave> A\b octave> Note the command is A b but neither A/b nor b/A. \ Exercise 1. Solve the following systems of equations: i. x + 2x + 3x = 2 − 1 2 3 4x1 5x2 + 6x3 = 1 7x − 8x + 9x = 1− 1 − 2 3 ii. 1.1 x + 2.2x + 3.3x = 2.2 − 1 2 3 4.4x1 5.5x2 + 6.6x3 = 1.1 7.7x − 8.8x + 9.9x = 1.1− 1 − 2 3 1.2. LU Decomposition Another method, UV decomposition, is usually used in large linear systems. Suppose that I AXI = b This equation can be solved as follows: I I I I PAX = LUX LyI = Pb = c ⇒ I Iy = L c ⇒ \ I X = U Iy ⇒ \ where L is a lower triangular matrix and U is an upper triangular matrix. The octave command about this LU decomposition is [ LUP] = lu( A) Example 2. Solve the same problem in Example 1 in LU decomposition. And compare the solutions. octave> A=[-1 1; 2 3];b=[2;-1] octave> [L U P]=lu(A) octave> y=L\(P*b) octave> x=U\y octave> x.^1 octave> Exercise 2. Solve the problems in Exercise 1 by LU decomposition. 1.3. Octave Programming Not only the fundamental mathematical functions octave supports, programmability is also sup- ported too. octave> A=[1 0 0 0;2 2 0 0;-1 0 2 0;0 -1 2 2] octave> A^2 octave> A.^2 octave> [u,v]=eig(A) octave> Q=[1 0 0 0;-2 2 1 0;1 1 0 0;-4 0 0 -1] octave> P=inv(Q) octave> P*A*Q The first octave program example is to plot graph of mathematical function, y = x + sin x. Unlike general alike software, Octave plot such graph piecewisely. In other words, we have to partition the parameter axis into several nonoverlapping subintervals firstly as following: x = linspace( xinit, xend, number of intervals) − − and plot the curve as: y = function( x) plot( x, y, ′′ ; title; ′′ ) A simple plotting example is given: 1. Partition [10,-10] into 1000 non-overlapping subintervals: octave> x=linspace(-10,10,1000); 2. define the function, y( x) = x + sin x octave> y=x+sin(x); 3. plot octave> plot(x,y,";Function y=x+sin(x);"); 10 Function y=x+sin(x) 8 6 4 2 0 -2 -4 -6 -8 -10 -10 -5 0 5 10 octave> z=x+cos(x);hold on octave> plot(x,z,";z=x+cos(x);"); 10 Function y=x+sin(x) z=x+cos(x) 5 0 -5 -10 -15 -10 -5 0 5 10 octave> octave> For more detailed explanation, we can use “set options” to modify the defaulted output, such as the tics, title and format etc. In the following example, we will use the octave command: gsplot(3d data) − to plot a 3D curve: ( x( t) , y( t) , z( t)) = ( cos2πt, sin2πt,t) Actually, the former ( x( t) , y( t)) pair is come from the Euler formulae, famous complex number: e 2πıt = cos2πt + ısin2πt where ı is √ 1 − octave> t=linspace(0,3,200); Define the particle position ( x( t) , y( t) , z( t)) in parametric form octave> x=cos(2*pi*t); octave> y=sin(2*pi*t); octave> z=t; octave> wave = [x’,y’,z’]; octave> grid Plot a parametric graph octave> gset parametric octave> gset ticslevel 0 set up format of the the output and save octave> gset term postscript enhanced eps color octave> gset out "/tmp/wave.eps" Plot the the graph with 3D data octave> gsplot wave octave> The example describe and the result is as follows: line 1 3 2.5 2 1.5 1 0.5 1 0.6 0.8 0 0.2 0.4 -1-0.8 0 -0.6-0.4-0.2 -0.4-0.2 0 0.2 0.4 -0.8-0.6 0.6 0.8 1 -1 Figure 1. Euler Representation Nowadays, this formulae is very useful to describe how the wireless network works! Exercise 3. Plot the graph of function, y = sin( x)/x, around x = 0. and gauss what the limit of y at x = 0 is. 1.4. ODE simulations One of the powers of Octave is modeling and simulations. The following example is to simulate the system of ODEs (Ordinary Differential Equation): dx ( t) 1 = 10( x ( t) x ( t)) dt − 1 − 2 dx ( t) 2 = 28x ( t) x ( t) x ( t) x ( t) dt 1 − 2 − 1 3 dx ( t) 8 3 = ( x ( t) x ( t) x ( t) ) dt 3 1 2 − 3 Initial condition: x1 (0) = 2, x2(0) = 5, x3(0) = 10 and output the resulted in postscript format: Initial condition: octave> x0=[2;5;10]; octave> t = linspace (0,10,800); The Differential Equations octave> function dx = butter (x ,t) dx(1) = -10.0*(x(1)-x(2)); dx(2) = 28.0*x(1)-x(2)-x(1)*x(3); dx(3) = 8.0/3.0*( x(1)*x(2) -x(3) ); end; Solve the DE numerically octave> y=lsode("butter",x0,t); octave> gset parametric; octave> gset term postscript enhanced color eps; octave> gset xtics 10;gset ytics 10; gset ztics 10; Output the result in eps format octave> gset out "/tmp/butterfly.eps"; octave> gsplot y title "Butterfly Effect" octave> At the last part of Octave code, we first save the 3D trajectory as butterfly.eps in /tmp directory. Then we can embed this EPS file into the worksheet as follows: Butterfly Effect 50 40 30 20 10 0 20 10 -20 0 -10 0 -10 10 20 -20 Figure 2. Butterfly Effect In the small octave codes, the first part shows the operation about linear algebra, multiplication, inverse and signalization of matrix. The second part shows the graph capacity of Octave, 2D and 3D graphs. 2D graphs can be automatically embedded into the worksheet but 3D graphs are not. You have to save the results somewhere and insert the results. You can download from http://octave.sf.net Exercise 4. (van der Pol Equation) Consider the following initial value problem: d2 x dx (1 x2 ) + x = 0 dt2 − − dt x(0) = 0 dx (0) = 1 dt a) Plot the graph for ( t, x( t)) within 10 unit time. b) Plot the parametric graph for ( x( t) , x ′( t)) . Hint: Consider the vector, Iy : x( t) Iy = x ( t) ′ and transform the original second order ODE of x( t) into first ODE of y( t) : y ( t) ′ y = 1 ′ y ( t) 2 x ( t) = ′ x ( t) ′′ y1′ ( t) = y2 ( t) 2 y′ ( t) = (1 y ( t) ) y ( t) y ( t) 2 − 1 2 − 1 Finally, the system of ODE’s becomes y ( t) y ( t) y = 1′ = 2 ′ y ( t) (1 y ( t) 2 ) y ( t) y ( t) 2′ ! − 1 2 − 1 Exercise 5. Challenge problem. 226 The differential equation for the famous Rutherford’s experiment, emitted α-particle toward Ra88 particle is as followed: 2 2 d x Zα ZAue x 2 = 3 dt 4πε0mα r 2 2 d y Zα ZAue y 2 = 3 dt 4πε0mα r where the velocity of α is v0 = 0.053[ c] . After all, the equations become: d2 x x = 0.0164 dt2 r3 d2 y y = 0.0164 dt2 r3 A simulation is as follows: Figure 3. Rutherford Scattering Simulation Derive the equations into first order system ODE’s for Ip ( t) = ( x( t); x ′( t); y( t); y′( t)) . 1.5. ODE Revisited 1.5.1. Newton Scheme Remember the great Newton’s linear approximation: f( xi +1 ) f( xi) + f ′( xi 1 ) ∆x ∼ − · where ∆x is the unit length for x partition. Consider the following mechanical problem: L θ1 L1 W1 L4 θ2 W2 W L2 3 θ θ3 4 L3 where Li , i = 1 , 2, 3, 4, is the length for each section, Wi , i = 1 , 2, 3, is the weight of the hang ball and θi , 1 , 2, 3, 4 is the angle between the horizontal level and the line section at each ball. The governed equations for this system are as follows : 4 Li cos θi L = 0 − i =1 X 4 Li sin θi = 0 i =1 X f( xi) f( xi ) cos θi = 0 ( where i = 1 , 2, 3) − +1 +1 f( xi) f( xi ) sin θi Wi = 0 ( where i = 1 , 2, 3) − +1 +1 − In this octave example, the values for the weight ( W2, 1 6 W2 6 3) and the total length, ( L, 1 6 L 6 4) have to be input by user.