my mathematica tutorial differential equations

30
Mathematica student guide Haris Javed The Purpose of this Guide is to inform the fellow students how to use mathematica for a Linear Algebra and Differential Equation Coarse: H* We Can Write Comments in mathematica by using H* Text*L command. Many of the Functions can H* Shift + enter should be used to Obtain a Function, just hitting enter will just take you to H* We can Assign Variables as a Function, For example typing n=2 will give n the value of 2, A H* You Can Also Use In@xD or Out@xD To bring or use old commands, just put the number in x, fo H* Pallettes Can Also be used instead of typing commands, you can use pallettes by clicking Pa H* Pi can be Written as pi without spaces or Π*L H* infinity can be Written as inf or ¥*L

Upload: haris-javed

Post on 15-Nov-2014

140 views

Category:

Documents


16 download

DESCRIPTION

This is what I made for my Calculus 4 class, might help some people.

TRANSCRIPT

Mathematica student guideHaris JavedThe Purpose of this Guide is to inform the fellow students how to use mathematica for a Linear Algebra andDifferential Equation Coarse:(+ We Can Write Comments in mathematica by using (+ Text+) command. Many of the Functions can(+ Shift+ enter should be used to Obtain a Function, just hitting enter will just take you to(+ We can Assign Variables as a Function, For example typing n=2 will give n the value of 2, Also(+ You Can Also Use In[x] or Out[x] To bring or use old commands, just put the number in x, for(+ Pallettes Can Also be used instead of typing commands, you can use pallettes by clicking Pallettes(+ Pi can be Written as pi without spaces or +)(+ infinity can be Written as inf or =+)1 SolvingSolving an Equation With MathematicaIn[2]:= Solve]4x-x2= 6,x| (+note that x2Requires Ctrl+6, and there are two equal signs= between the +)Out[2]= x-2 - f 2 , x-2 + f 2 In[21]:= Solve]5x2-18x-5=0,x|Out[21]= x-15|9 - 106 |, x-15|9 + 106 |In[22]:= Solve]-6+3x-2x2+4x3= 5 Sin(x),x|Out[22]= x-16-32 - 60 Sin12 22/3299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/3+299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/36 21/3,x-16+|1 + f 3 | 32 - 60 Sin)24 22/3299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/3-|1 - f 3 | 299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/312 21/3,x-16+|1 - f 3 | 32 - 60 Sin)24 22/3299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/3-|1 + f 3 | 299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2- 500 Sin31/312 21/32t e s t m at he m at ica. nbIn[23]:= (+by Adding A //N to the Function in input[26] We can change all the exact Value to ApproximateSolve]-6+3x-2x2+4x3= 5 Sin(x),x|//NOut[23]= x-0.166667 -0.0524967 32. - 60. Sin)299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3+0.132283 299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3,x-0.166667 +0.0262484 + 0.0454635 f) 32. - 60. Sin)299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3-0.0661417 - 0.114561 f) 299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3,x-0.166667 +0.0262484 - 0.0454635 f) 32. - 60. Sin)299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3-0.0661417 + 0.114561 f) 299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2- 500. Sin31/3In[24]:= (+ we Can Also Add a . in the Function itself to Create decimal/Approximate Values. Note the .Solve]-6.+3x-2x2+4x3= 5 Sin(x),x|Out[24]= x-0.166667 +0.0524967 - 0.090927 f) 32. - 60. Sin)2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3-0.0330709 + 0.0572804 f)2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3,x-0.166667 +0.0524967 + 0.090927 f) 32. - 60. Sin)2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3-0.0330709 - 0.0572804 f)2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3,x-0.166667 -0.104993 32. - 60. Sin)2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3+0.0661417 2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin21/3In[34]:= Solve[{7x-4y=-8,-1x+3y=18}]Out[34]= x-4817,y-11817t e s t m at he m at ica. nb3In[94]:= (+Multiple instructions can also be given for Solving a System+)Clear[equ];equ={9x+2y=-3,-2x+7y=11};Solve[equ]Solve[N[equ]]NSolve[equ]Out[96]= x--4367,y-9367Out[97]= {{x--0.641791,y-1.38806Out[98]= {{x--0.641791,y-1.38806In[102]:= Clear[equ];equ={2x-1y-3z=-4,-5x+2y-18z=2,4x+y+2z=-3};Solve[equ]Solve[N[equ]]NSolve[equ]Out[102]= x--162145,y-127145,z-43145Out[103]= {{x--1.11724,y-0.875862,z-0.296552Out[104]= {{x--1.11724,y-0.875862,z-0.296552In[117]:= Solve]x2+2x-7=0,x|Out[117]= x--1 - 2 2 , x--1 + 2 2 In[118]:= N[Out[117]]Out[118]= {{x--3.82843, {x-1.82843In[131]:= Clear[equ];equ={3 x-2 y-7 z==-4,-3 x+2 y-8 z==1,2 x+11 y+6 z==-3};Solve[equ]Solve[N[equ]]NSolve[equ]4t e s t m at he m at ica. nbIn[134]:= ContourPlot3D[{3 x-2 y-7 z==-4,-3 x+2 y-8 z==1,2 x+11 y+6 z==-3},{x,-5,5},{y,-5,5},{z,-5,5}]Out[134]=505505505t e s t m at he m at ica. nb52 RootsFinding Roots Using the FindRoot CommandIn[25]:= (+ mathematica is a Powerful tool which can be used for many operations, now lets see how we canFindRoot]4x3-5x2+3x-3=0,{x,0}|Out[25]= {x-1.16058(+ above is the Excat root, it does not include all the roots that are found by using the Solve +)Plot]]4x3-5x2+3x-3,{x,-10,10},AxesLabel- {x,y},PlotRange-{-10,10}|(+ below we can see the Root +)In[27]:= (+Another Root+)FindRoot]4x3+2x2+x-5=3Sin[x],{x,2}|Out[27]= {x-1.03301In[28]:= (+Visual Roots of Above Equation+)Plot]]4x3+2x2+x-5,3Sin[x],{x,-10,10},AxesLabel- {x,y},PlotRange-{-10,10}|Out[28]=10 5 5 10x105510yIn[135]:= FindRoot]2x5-5x4-2x3-x2+6x+1=3Sin[x]-4Cos[x],{x,3}|Out[135]= {x-2.813816t e s t m at he m at ica. nb3 GraphingPlotting A Basic EquationIn[3]:= (+We will Graph the above equation Eq1 here+)(+The above equation will be Defined as 2 functions [y=6] and ]y= 4x-x2| put together as follows |Out[3]=4 2 2 4x55yGraphing A Little More complex functionIn[4]:= (+ Lets Solve a Little More Complex Function+)Solve]5x2-18x-5=0,x|Out[4]= x-15|9 - 106 |, x-15|9 + 106 |t e s t m at he m at ica. nb7x-15|9 - 106 j, x-15|9 + 106 j(+Also Lets Plot This function and see how it looks like+)Plot]5x2-18x-5,{x,-3,3},AxesLabel- {x,y},PlotRange- {-8,8}|3 2 1 1 2 3x55yIn[5]:= (+ We will Plot another function just to get the hang of the method+)Plot]]2x4+x3-7x2+3x-8,{x,-20,20}, AxesLabel- {x,y},PlotRange- {-20,20}|(+ The image Below can be Shrinked, or Expanded with the use of the Mouse cursors. Click on the +)Out[5]=20 10 10 20x20101020y8t e s t m at he m at ica. nbIn[30]:= Plot]Sin[x]2,{x,0,2Pi},PlotLabel- Sin[x]2|Out[30]=1 2 3 4 5 60.20.40.60.81.0sin2xPlotting With Different Style (+ the Original Function Was23x-13==1-32x+) Plot]63x-23,1-12x,{x,-.5,1.5},PlotRange- {-.5,1.2}Out[6]=0.5 0.5 1.0 1.50.50.51.0In[7]:= Plot]4-x,(5-6x)2,{x,-2,4}| (+ Original function 4-x=(5-6x)2+)Out[7]=2 1 1 2 3 455Plotting a three Dimensional function , Graphing a system of Equations with x,y,z or x1, x2, x3t e s t m at he m at ica. nb9Plotting a three Dimensional function , Graphing a system of Equations with x,y,z or x1, x2, x3In[8]:= ContourPlot3D[{4x-8y-z=-4,-6x+3y-5z=1,6x+y+6z=-12},{x,-10,10},{y,-10,10},{z,-10,10}] (+ this +)Out[8]=10505101050510105051010t e s t m at he m at ica. nbIn[151]:= Plot3D](18x-2y+7)4,(-36+4y-1)1,(3+2x+y)-6,{x,-5,3},{y,-2,4},PlotRange- {-1,2},PlotStyle- {Red |Out[151]=420220241012t e s t m at he m at ica. nb113-D Plotting(+Mathematica will plot 3d Graphs With ease look at the Following Example+)Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2},Mesh- All](+This Plot can be moved/ Scaled by clicking on +)Out[18]=202210121.00.50.00.51.0In[19]:= Plot3D[Sin[x+y^2],{x,-2,2},{y,-2,2},PlotStyle-Directive[Orange,Specularity[White,20]]] (+with +)12t e s t m at he m at ica. nb21012 210121.00.50.00.51.0t e s t m at he m at ica. nb13In[20]:= (+Note that contour plot will plot a 3d function if we have more than 2 variables, if we onlyContourPlot[{8x+2y=-3,-8x+3y=11},{x,-6,3},{y,-2,4},Axes- True,Frame- False,Mesh- All]Out[20]=6 4 2 221123414t e s t m at he m at ica. nbIn[138]:= GraphPlot3D[Table[i -> Mod[i^2, 75], {i, 75}], SelfLoopStyle-> None,MultiedgeStyle-> None,EdgeRenderingFunction-> ({Green, Cylinder[, 0.1]} &),VertexRenderingFunction-> ({Black, Sphere[, 0.2]} &)]Out[138]=t e s t m at he m at ica. nb15In[174]:= p5 = ContourPlot ]y =(5x)2 + 2x2|12j, {x, -1, 15}, {y, 0, 1.5}, FrameTicks- True|Out[174]=0 5 10 150.00.20.40.60.81.01.21.416t e s t m at he m at ica. nb4 MatrixRowReduction,Determinant, Transpose, InverseIn[31]:= (+ For row reduction we will just use the Command Row Reduce+)RowReduce] 8 -5 -4 -4-5 2 -11 17 1 3 -3|Out[31]= 1,0,0,-4187, 0,1,0,-487, 0,0,1,1087In[32]:= RowReduce] 2 -5 -7 -4-3 2 -15 12 1 6 -3|//MatrixFormOut[32]//MatrixForm=1 0 0 -3521630 1 0 -791630 0 149163N]RowReduce] 4 3 22 3 4 ||(+ again the N gives Decimal Answers+)Out[33]= {{1.,0.,-1., {0.,1.,2.In[36]:= (+ We can always Define a Variable in mathematica as a function, look below+)n= 4 3 22 3 4Out[36]= {{4,3,2, {2,3,4(+ We have defined n As the Matrix above, be careful about lower and uppercase. A lot of Commands(+ We can Clear the above n by using the Clear[n] command+)Clear[n]In[61]:= (+ A Equation can Also be put as a linear system and be solved, {9x+2y=-3,-2x+7y=11} can be putlets solve them and see if the answers match+)Solve[equ]Out[61]= x--4367,y-9367t e s t m at he m at ica. nb17In[62]:= RowReduce] 9 2 -3-2 7 11 |Out[62]= 1,0,-4367, 0,1,9367(+ The Answers Match, as expected+)In[105]:= Clear[equ]In[109]:= Clear[m,u,v,w];m=2 -5 -7-1 8 -59 4 6;u={-8,1,-2};v={12,4,4};w={3,5,-8};f=LinearSolve[m]Out[111]= LinearSolveFunction|{3,3,>]In[112]:= (+ Transposing the Above matrix+)Transpose[{f[u],f[v],f[w]}]//MatrixFormOut[112]//MatrixForm=- 7048631148863- 434863353863- 100863122863533863- 1080863- 581863In[113]:= {u,v,w}//MatrixFormOut[113]//MatrixForm=-8 1 -212 4 43 5 -8In[114]:= Join[m,{u,v,w}]//MatrixFormOut[114]//MatrixForm=2 -5 -7-1 8 -59 4 6-8 1 -212 4 43 5 -8In[115]:= n=Transpose[Append[Append[Append[Transpose[m],u],v],w]]//MatrixFormOut[115]//MatrixForm=2 -5 -7 -8 12 3-1 8 -5 1 4 59 4 6 -2 4 -8In[116]:= RowReduce[Out[115]]//MatrixFormOut[116]//MatrixForm=1 0 0 -7048631148863-4348630 1 0353863-1008631228630 0 1533863-1080863-58186318t e s t m at he m at ica. nbIn[119]:= Clear[A,A1,b,c];A={{9,7,3},{8,5,1},{7,5,5}};b={-2,5,3};c={1,-1,0};A1=MatrixForm[{{9,7,3},{8,5 Out[119]= -12,-15,0, -12,15,13, {2,5,3, {Log|2],Log|5],Log|3],6In[121]:= (+ Dot Product Cross Product+){b c,b+c,bc,b.c,Dot[b,c],Cross[b,c],bc,bc,A.b,b.A,MatrixForm[b]}Out[121]= {-2,-5,0, {-2,-5,0, {-2,-5,0,-7,-7, {3,3,-3,{-2,5,3{1,-1,0, {3,3,-3, {26,12,26, {43,26,14,-253In[122]:= ]4+A,3+A2,Transpose[A],Transpose[A1],AT,A2,A12//MatrixFormOut[122]//MatrixForm={{13,11,7, {12,9,5, {11,9,93 + A2{{9,8,7, {7,5,5, {3,1,5Transpose|9 7 38 5 17 5 5|9T,7T,3T, 8T,5T,1, 7T,5T,5T{{81,49,9, {64,25,1, {49,25,259 7 38 5 17 5 52In[123]:= Clear[B];MatrixForm[B={{-3,5,19},{0,1,5},{6,-18,27}}]Out[123]//MatrixForm=-3 5 190 1 56 -18 27In[124]:= (+ Determinant+)Det[Out[123]]Out[124]= -315In[126]:= B+1//MatrixFormOut[126]//MatrixForm=-2 6 201 2 67 -17 28In[128]:= BT//MatrixFormOut[128]//MatrixForm=-3)T5T19T0T1 5T6T-18)T27Tt e s t m at he m at ica. nb19In[130]:= Transpose[B]//MatrixFormOut[130]//MatrixForm=-3 0 65 1 -1819 5 27In[216]:= Inverse[B]//MatrixFormOut[216]//MatrixForm=-13355335-2105-2211321-121210581051105In[218]:= LinearSolve[B]Out[218]= LinearSolveFunction|{3,3,>]In[219]:= NullSpace[B]Out[219]= {In[222]:= Eigenvalues[N[B]]//MatrixFormOut[222]//MatrixForm=27.5269-4.874472.34761In[226]:= Eigenvectors[N[B]]//Simplify//MatrixFormOut[226]//MatrixForm=0.54023 0.155871 0.8269560.986349 0.10673 -0.125397-0.877241 -0.46351 -0.12492620t e s t m at he m at ica. nbIn[147]:= Graphics3D[{AstronomicalData[, "OrbitPath"] & / AstronomicalData["Planet"],{Orange,Opacity[.5], (AstronomicalData[, "OrbitPath"] & /AstronomicalData["ApolloAsteroid"])}}, PlotRange-> {{-6, 6}, {-6, 6}, {-4, 4}}, Boxed-> False,ViewAngle-> /11]Out[147]=t e s t m at he m at ica. nb215 derivatives and integralsDifferentiation(+Remember to Always clear your Variables+)Clear[x];Clear[n]In[161]:= D[xn,x]Out[161]= n x-1+nIn[162]:= D[xn,x,{x,3}]Out[162]= -3 + n) -2 + n) -1 + n) n x-4+nIn[163]:= D]x33Tan[x],x|Out[163]= x2Cot|x] -13x3Csc|x]2Manipulate[D[2xn,x],{n,1,20,1}](+ This makes a nice Differential Calculator, which can Differentiate +)Out[167]=n2IntegrationIn[164]:= Integrate]x2,x|Out[164]=x33In[165]:= x2dxOut[165]=x3322t e s t m at he m at ica. nbManipulate[Integrate[2xn,x],{n,1,20,1}] (+ this makes a nice integral calculator, it will integrate )Out[166]=n2 x33In[168]:= Integrate]11-x3,x|Out[168]=ArcTan|1+2 x3|3-13Log|-1 + x] +16Log|1 + x + x2|In[170]:= Manipulate]Integrate]11-xn,x|,{n,1,20,1}|//TimingOut[170]= 1.63758 x 10-15,n1202 2 |5 + 5 | ArcTan|1 - 5 + 4 x2 |5 + 5 | | +2 10 - 2 5 ArcTan|1 + 5 + 4 x10 - 2 5| - 4 Log|-1 + x] -|-1 + 5 | Log|1 -12|-1 + 5 | x + x2| + |1 + 5 | Log|1 +12|1 + 5 | x + x2|In[171]:= 1x3-1dxOut[171]= -ArcTan|1+2 x3|3+13Log|-1 + x] -16Log|1 + x + x2|t e s t m at he m at ica. nb23In[172]:= Plot[,{x,0,2}]Out[172]=0.5 1.0 1.5 2.01.61.41.224t e s t m at he m at ica. nb6 vector fields and differential equationsPlotting Vector field Graphs (+ Note Mathematica 7 uses VectorPlot command instead of VectorFieldPlotIn[183]:= VectorPlot]]e-1/2xSin[2y], 2y Cos [5x], {x,-, }, {y, -, }, Axes - True, AxesLabel- {x,y}, |Out[183]=3 2 1 1 2 3x321123yt e s t m at he m at ica. nb25In[187]:= VectorPlot[{Sin[6y], -y Cos [6x]}, {x,-, }, {y, -, }, Axes - True, AxesLabel- {x,y}, Frame ]Out[187]=3 2 1 1 2 3x321123yIn[192]:= VectorPlot [{Sin[2x],Cos[y]},{x,-, },{y,-,},Axes- True,AxesLabel- {x,y}, Frame- True]Out[192]=3 2 1 0 1 2 33210123xy(+ Lets Solve some differential Equations now we will use DSolve Command which Solves 2 SimultaneousDSolve[{y[x]=-z'[x],z[x]=-y'[x]},{y,z},x](+ This equation involves no boundary conditions+)Out[190]= z-Function|{x,12c-x1 + c2 x| C|1] -12c-x-1 + c2 x| C|2]|,y-Function|{x,-12c-x-1 + c2 x| C|1] +12c-x1 + c2 x| C|2]|26t e s t m at he m at ica. nbIn[191]:= DSolve[{y'[x]= a y[x],y[0]= 1},y[x],x]Out[191]= {{y|x]-ca xIn[193]:= DSolve[2y[t]+2y'[t]+y''[t]==2t,y[t],t][[1]]Out[193]= y|t]--1 + t + c-tC|2] Cos|t] + c-tC|1] Sin|t]In[194]:= y[t]/. DSolve[2y[t]+2y'[t]+y''[t]==2t,y[t],t][[1]]Out[194]= -1 + t + c-tC|2] Cos|t] + c-tC|1] Sin|t]In[199]:= Clear[f]In[203]:= f[t_]:=t+c1 Cos[5t]e-t+c2 Sin[5t]e-5t-9f[t]/.{c1- 1,c2- 1}Out[204]= -9 + t + ctCos|5 t] + c5 tSin|5 t]In[205]:= Plot[Table[f[x],{c2,-2,2},{c1,-2,2}],{x,-3,3}]//Timing (+ Timing Gives the Seconds it took the +)Out[205]= 5.641,3 2 1 1 2 3403020101020t e s t m at he m at ica. nb27In[206]:= Plot[Evaluate[Table[f[x],{c2,-2,2},{c1,-2,2}]],{x,-2,3}]//TimingOut[206]= 0.765,2 1 1 2 3200100100200In[227]:= NDSolve[{x'[t]=-3(x[t]-y[t]),y'[t]=-x[t]z[t]+26.5x[t]-y[t],z'[t]= x[t]y[t]-z[t],x[0]= z[0] ]Out[227]= {{x-InterpolatingFunction|{{0.,200.,>],y-InterpolatingFunction|{{0.,200.,>],z-InterpolatingFunction|{{0.,200.,>]28t e s t m at he m at ica. nbIn[228]:= ParametricPlot3D[Evaluate[{x[t],y[t],z[t]}/.],{t,0,200},PlotPoints- 100000]//TimingOut[228]= 2.75,10 5 0 51010010010203040In[207]:= y[t]/.DSolve[2y[t]+2y'[t]+y''[t]==2Sin[t],y[t],t][[1]]//SimplifyOut[208]= -45+ c-tC|2] Cos|t] +25+ c-tC|1] Sin|t]t e s t m at he m at ica. nb29In[209]:= y[t]/.DSolve[2y[t]+2y'[t]+y''[t]==2Sin[t],y[t],t][[1]]//NOut[209]= 2.71828-1. tC|2] Cos|t] + 2.71828-1. tC|1] Sin|t] +0.2 -5. Cos|t] + Cos|t] Cos|2. t] - 2. Cos|2. t] Sin|t] + 2. Cos|t] Sin|2. t] + Sin|t] Sin|2. t])In[211]:= y[t]/.DSolve]y''[t]-4y'[t]+3y[t]=e(3t)-5Cos[3t],y[t],t|[[1]]//SimplifyOut[211]= ctC|1] + c3 tC|2] +1123 c3 t-1 + 2 t) + 2 Cos|3 t] + 4 Sin|3 t]|In[212]:= (+Initial Conditions+)DSolve]]y''[x]-4y'[x]+3y[x]=e(3x)-5Cos[3x],y[0]=1,y'[0]=1 ,y[x],x|//SimplifyOut[212]= y|x]-169 cx- 4 c3 x+ 3 c3 xx + Cos|3 x] + 2 Sin|3 x]|In[214]:= DSolve]x2y''[x]+y[x]= 0,y[x],x|Out[214]= y|x]- x C|1] Cos|123 Log|x]| + x C|2] Sin|123 Log|x]|30t e s t m at he m at ica. nb