(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.0' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 217255, 5488]*) (*NotebookOutlinePosition[ 272820, 6986]*) (* CellTagsIndexPosition[ 272776, 6982]*) (*WindowFrame->Normal*) Notebook[{ Cell[BoxData[ StyleBox[\(12.010\ HW\ 4 : \ Problem\ solving\ with\ Mathematica\), "Title", FontSize->24]], "Input"], Cell[CellGroupData[{ Cell[TextData[{ "Question 1: ", StyleBox["Write Mathematic program which generates a table of Legendre \ polynomials and associated functions for degrees and orders from 0 to 4 (l \ and m on mathworld site) and for arguments (x) between -1 and 1 in steps of \ 0.25 (see ", FontSize->12, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}], StyleBox["http://mathworld.wolfram.com/LegendrePolynomial.html", FontSize->12, FontWeight->"Plain", FontColor->RGBColor[0, 0, 0.996109], FontVariations->{"CompatibilityType"->0}], StyleBox[" ", FontFamily->"Lucida Grande", FontSize->12, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}], StyleBox["and HW 02 solution). Table should be no wider than 100 characters \ and should have headers explaining what the columns are. How would you \ change this program if 10 significant digits were required? Solution should \ be in Mathematica NoteBook or a Mathematica .m file ", FontSize->12, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}] }], "Section"], Cell[TextData[{ "This question is quite tricky for a number of reasons (1) Controling ", StyleBox["Mathematica", FontSlant->"Italic"], " output in terms of format is not straight forward (i.e. no equivalanent \ to %10.5f in C and Matlab) and (2) The TableForm output can be tricky because \ there are three arguments that are changing (l,m. and x). ie, the List \ genenerated by Table is three levels deep where as two levels is easier for a \ table. Also getting the values of l and m into the table is not easy with \ the standard Table call. The solution below gets around the problem with \ Table but directly constructing the List to output. The SetAccuarcy and N \ usage sets the number of signficant digits. \n" }], "Commentary"], Cell[CellGroupData[{ Cell[BoxData[{ \(\(Off[General::spell1];\)\ \ (*\ Stops\ message\ about\ head\ looking\ like\ a\ command*) \ \[IndentingNewLine]\), "\n", \(\(arg\ = \ Range[\(-1\), 1, .25];\)\), "\[IndentingNewLine]", \(\(header\ = \ Join[{"\", "\"}, arg];\)\), "\[IndentingNewLine]", \(cnt\ = \ 0; \ lall\ = \ {};\), "\[IndentingNewLine]", \(For[l = 0, l < 5\ , \(l++\), \[IndentingNewLine]For[m = 0, m \[LessEqual] l, \(m++\), \[IndentingNewLine]{l1\ = \ Join[{l, m}, N[SetAccuracy[LegendreP[l, m, arg], 5], 5]], \[IndentingNewLine]lall\ = \ Append[lall, l1]}\[IndentingNewLine]]\[IndentingNewLine]]\), "\ \[IndentingNewLine]", \(\(full = Insert[lall, header, 1];\)\), "\n", \(Print["\"]\), "\[IndentingNewLine]", \(Print[TableForm[full, TableAlignments \[Rule] Right]]\)}], "Input"], Cell[BoxData[ \("Table of Legendre Functions\n----------------------------"\)], "Print"], Cell[BoxData[ TagBox[GridBox[{ {"\<\"l\"\>", "\<\"m\\\\arg\"\>", \(-1\), \(-0.75`\), \(-0.5`\), \ \(-0.25`\), "0.`", "0.25`", "0.5`", "0.75`", "1.`"}, {"0", "0", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002", "1.`5.000000000000002"}, {"1", "0", \(-1.`5.000000000000002\), \(-0.75`4.875061263391701\), \ \(-0.4999999999999999445`4.6989700043360205\), \ \(-0.2499999999999999722`4.397940008672039\), "0``5.", "0.2499999999999999722`4.397940008672039", "0.4999999999999999445`4.6989700043360205", "0.75`4.875061263391701", "1.`5.000000000000002"}, {"1", "1", "0``5.", \(-0.661437827766147568`4.820489028679167\), \ \(-0.8660254037844385966`4.937530631695853\), \ \(-0.9682458365518542553`4.985985638199879\), \(-1.`5.000000000000002\), \ \(-0.9682458365518542553`4.985985638199879\), \ \(-0.8660254037844385966`4.937530631695853\), \ \(-0.661437827766147679`4.820489028679167\), "0``5."}, {"2", "0", "1.`5.000000000000002", "0.34375`4.53624270683832", \(-0.125`4.096910013008058\), \ \(-0.40625`4.608793373986932\), \(-0.5`4.6989700043360205\), \ \(-0.40625`4.608793373986932\), \(-0.125`4.096910013008058\), "0.34375`4.53624270683832", "1.`5.000000000000002"}, {"2", "1", "0``5.", "1.4882351124738320003`5.000000000000002", "1.2990381056766580059`5.000000000000002", "0.726184377413890747`4.861046901591579", "0``5.", \(-0.726184377413890636`4.861046901591579\), \ \(-1.2990381056766580059`5.000000000000002\), \ \(-1.4882351124738322223`5.000000000000002\), "0``5."}, {"2", "2", "0``5.", "1.3125`5.000000000000002", "2.25`5.000000000000002", "2.8125`5.000000000000002", "3.`5.000000000000002", "2.8125`5.000000000000002", "2.25`5.000000000000002", "1.3125`5.000000000000002", "0``5."}, {"3", "0", \(-1.`5.000000000000002\), "0.0703124999999999861`3.847032539791457", "0.4375`4.640978057358334", "0.3359375`4.526258485931719", "0``5.", \(-0.3359375`4.526258485931719\), \ \(-0.4375`4.640978057358334\), \(-0.0703124999999999861`3.847032539791457\), "1.`5.000000000000002"}, {"3", "1", "0``5.", \(-1.7982840942392137595`5.000000000000002\), \ \(-0.3247595264191645015`4.511561899423571\), "0.9985035189440997216`4.9993495997578625", "1.5`5.000000000000002", "0.9985035189440996106`4.9993495997578625", \ \(-0.3247595264191645015`4.511561899423571\), \ \(-1.7982840942392139816`5.000000000000002\), "0``5."}, {"3", "2", "0``5.", \(-4.921875`5.000000000000002\), \ \(-5.625`5.000000000000002\), \(-3.515625`5.000000000000002\), "0``5.", "3.515625`5.000000000000002", "5.625`5.000000000000002", "4.921875`5.000000000000002", "0``5."}, {"3", "3", "0``5.", \(-4.3406857447153441854`5.000000000000002\), \ \(-9.7427857925749332679`5.000000000000002\), \ \(-13.6159570765104493972`5.000000000000002\), \(-15.`5.000000000000002\), \ \(-13.6159570765104493972`5.000000000000002\), \ \(-9.7427857925749350443`5.000000000000002\), \ \(-4.3406857447153441854`5.000000000000002\), "0``5."}, {"4", "0", "1.`5.000000000000002", \(-0.35009765625`4.544189203364008\), \ \(-0.2890625`4.460991754419129\), "0.15771484375`4.197872570027312", "0.375`4.57403126772772", "0.15771484375`4.197872570027312", \(-0.2890625`4.460991754419129\ \), \(-0.35009765625`4.544189203364008\), "1.`5.000000000000002"}, {"4", "1", "0``5.", "1.1626836816201813196`5.000000000000002", \ \(-1.3531646934131853488`5.000000000000002\), \ \(-1.5507062226025791762`5.000000000000002\), "0``5.", "1.5507062226025789542`5.000000000000002", "1.3531646934131853488`5.000000000000002", \ \(-1.1626836816201815417`5.000000000000002\), "0``5."}, {"4", "2", "0``5.", "9.638671875`5.000000000000002", "4.21875`5.000000000000002", \(-3.955078125`5.000000000000002\), \ \(-7.5`5.000000000000002\), \(-3.955078125`5.000000000000002\), "4.21875`5.000000000000002", "9.638671875`5.000000000000002", "0``5."}, {"4", "3", "0``5.", "22.7886001597555534204`5.000000000000002", "34.0997502740122655496`5.000000000000002", "23.8279248838932851129`5.000000000000002", "0``5.", \(-23.8279248838932886656`5.000000000000002\), \ \(-34.099750274012272655`5.000000000000002\), \ \(-22.7886001597555569731`5.000000000000002\), "0``5."}, {"4", "4", "0``5.", "20.09765625`5.000000000000002", "59.0625`5.000000000000002", "92.28515625`5.000000000000002", "105.`5.000000000000002", "92.2851562500000142109`5.000000000000002", "59.0625`5.000000000000002", "20.09765625`5.000000000000002", "0``5."} }, RowSpacings->1, ColumnSpacings->3, RowAlignments->Baseline, ColumnAlignments->{Right}], Function[ BoxForm`e$, TableForm[ BoxForm`e$, TableAlignments -> Right]]]], "Print"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Question 2:", StyleBox["Write a program that reads your name in the form \ and outputs the last name first and adds a comma \ after the name, the first name, and initial of your middle name with a period \ after the middle initial. If the names start with lower case letters, then \ these should be capitalized. The program should not be specific to the \ lengths of your name (ie., the program should work with anyone\ \[CloseCurlyQuote]s name.\nAs an example. An input of\nthomas abram herring \n\ would generate:\nHerring, Thomas A. \n(The solution can be in the same \ Notebook as Question 1)", FontSize->12, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}] }], "Section"], Cell[TextData[{ "This problem is not too bad to solve. This solution works in 5.0 ", StyleBox["Mathematica", FontSlant->"Italic"], " and does explicitly some things such as splitting a stringe apart that \ are now ", StyleBox["Mathematica", FontSlant->"Italic"], " commands (StringSplit). \n" }], "Commentary"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ StyleBox[\( (*\ Define\ a\ function\ that\ will\ convert\ chararacter\ of\ a\ \ string\ to\ upper\ case\ *) \), "Commentary"], StyleBox["\[IndentingNewLine]", "Commentary"], RowBox[{\(confirst[a_]\ := \ StringReplacePart[a, ToUpperCase[StringTake[a, {1}]], {1, 1}];\), StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[\( (*\ Get\ the\ name\ from\ the\ user*) \), "Commentary"], "\[IndentingNewLine]", \(inname\ = \ InputString["\"];\), " ", "\[IndentingNewLine]", StyleBox[\( (*\ Convert\ whole\ string\ to\ lower\ case*) \), "Commentary"], "\[IndentingNewLine]", \(fullname\ = \ ToLowerCase[inname];\), "\[IndentingNewLine]", StyleBox[\( (*\ Now\ get\ the\ list\ of\ blanks\ in\ the\ string*) \), "Commentary"], "\[IndentingNewLine]", \(posblanks\ = \ StringPosition[fullname, "\< \>"];\), "\[IndentingNewLine]", StyleBox[\( (*\ Get\ the\ position\ of\ first\ blank*) \), "Commentary"], "\[IndentingNewLine]", \(posfirst\ = \ Extract[Extract[posblanks, 1], 1];\), "\[IndentingNewLine]", \(firstname\ = \ StringTake[fullname, posfirst - 1];\), "\[IndentingNewLine]", RowBox[{\(firstname\ = \ confirst[firstname]\), ";", " ", \( (*\ Use\ our\ confirst\ routine\ *) \), "\[IndentingNewLine]", StyleBox[\( (*\ Get\ Middle\ Name\ *) \), "Commentary"], "\[IndentingNewLine]", \(posmid\ = \ Extract[Extract[posblanks, 2], 1]\), ";"}], "\[IndentingNewLine]", \(midinit\ = \ StringTake[fullname, {posfirst + 1, \ posfirst + 1}];\), "\[IndentingNewLine]", \(midinit\ = \ confirst[midinit];\), "\[IndentingNewLine]", StyleBox[\( (*\ Get\ Last\ Name\ *) \), "Commentary"], "\[IndentingNewLine]", \(lastname\ = \ StringTake[fullname, {posmid + 1, StringLength[fullname]}];\), "\[IndentingNewLine]", \(lastname\ = \ confirst[lastname];\), "\[IndentingNewLine]", \( (*\ Output\ the\ string\ *) \), "\[IndentingNewLine]", \(finalname\ = \ lastname\ <> \ "\<, \>"\ <> \ firstname\ <> \ "\< \>"\ <> \ midinit\ <> \ "\<.\>";\), "\[IndentingNewLine]", \(outline\ = \ "\"\ <> \ inname\ <> \ "\< converted to \>"\ <> \ finalname;\), StyleBox["\[IndentingNewLine]", "Commentary"], StyleBox[\( (*\ Output\ the\ results*) \), "Commentary"], "\[IndentingNewLine]", \(Print[outline]\), "\[IndentingNewLine]"}]}]], "Input"], Cell[BoxData[ \("Input name thomas abram herring converted to Herring, Thomas A."\)], \ "Print"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Question 3: ", StyleBox["Write a Mathematica program that will compute the trajectory of a \ paper plane given an initial height and vector velocity (i.e., a speed and \ direction for the launch.) The program should run until the height of the \ plane is zero. \nAs a test of your code, compute the trajectory of a paper \ plane weighing 3 grams launched from a height of 2 meters at a speed of 3.5 \ meters/sec at angle of -10 degrees from level. Repeat the calculation with a \ 10.0 m/sec initial velocity. The wing area should be 0.017 square-meters. The \ lift and drag coefficients are 0.22 and 0.04 m2. Air density of 1.225 kg/m3. \ Gravity is 9.8 m/sec2. Use the force equations from the solution to \ homework 1.\nYour answer to this question should include:\n(a)\tThe \ algorithms used and the design of your program (can be noted in Notebook)\n\ (b)\tThe Mathematica program.\n(c)\tThe results from the two test cases \ above. \nInclude in your Notebook a graphic showing the trajectories.\nHint: \ Look at the examples in the NDSolve descriptions\n", FontSize->12, FontWeight->"Plain", FontVariations->{"CompatibilityType"->0}] }], "Section"], Cell["\<\ The solution to this problem is in a number of cells so that \ default values can be set in one cell. Another cell allows these values to \ updated with user inout, and the cells below that do the calculation. The first cell sets the defaults values and defines some of the functions \ need for drag and lift calculations. Notice at t, x and z are cleared \ because if these were previously set to numerical values the code will not \ run correctly. The second cell allows user input. It does not need to be executed if the \ defaults are to be used. The third cell does the calculation. It basically just sets up the \ differential equations and the initial conditions (position and velocity) and \ set a time interval over which to solve the problem. The funtions hx ans hz \ are just easy ways to referr to the solution. The FindRoot finds the last \ root in the interval specified (In this case there is just the one root). \ The t /. FindRoot constructions returns the value of t from the list \ generated by FindRoot. The fouth cell does the ParametricPlot of the results. The fifth cell is a repeat of code that allows the 3.5 m/s case to be \ evalated. The solutions to the two test cases are: Vinit 10 m/s \[Rule]Time to reach ground \[InvisibleSpace]5.48108\ \[InvisibleSpace] sec, Traveled \[InvisibleSpace]15.9547\[InvisibleSpace] m Vinit 3.5 m/s \[Rule] Time to reach ground \[InvisibleSpace]3.12124\ \[InvisibleSpace] sec , Traveled \[InvisibleSpace]10.9052\[InvisibleSpace] m \ \>", "Commentary"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ StyleBox[\( (*\ Set\ up\ constants*) \), "Commentary"], "\[IndentingNewLine]", RowBox[{\(Off[General::spell1];\), StyleBox["\[IndentingNewLine]", "MR"], \(Clear[t, x, z, \ xd, \ zd, \ hx, \ hz\ ];\), StyleBox["\[IndentingNewLine]", "MR"], \(cd\ = \ 0.04; \ cl\ = \ 0.22;\), "\[IndentingNewLine]", RowBox[{\(area\ = \ 0.017\), ";", " ", StyleBox[\( (*\ m^2\ *) \), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(mass\ = \ 0.003\), ";", " ", StyleBox[\( (*\ kg\ *) \), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(lht\ = \ 2.0\), ";", " ", StyleBox[\( (*\ meters\ *) \), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(lthetadeg\ = \ \(-10\)\), ";", " ", StyleBox[\( (*\ degrees\ *) \), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(lvel\ = \ 10.0\), ";", " ", StyleBox[\( (*\ Initial\ velocity\ m/sec\ *) \), FontColor->RGBColor[0, 0, 1]], "\[IndentingNewLine]", \(ltheta\ := \ lthetadeg*Pi/180\), ";"}], " ", "\n", StyleBox[\( (*\ Define\ the\ acceleration\ functions\ we\ will\ need*) \), "Commentary"], "\[IndentingNewLine]", \(grav[z_]\ = \ \(-9.8\)\ ; \ rhoair\ = \ 1.225;\), " ", "\[IndentingNewLine]", \(dragx[xd_, zd_]\ := \ \(-rhoair\)* Sqrt[xd^2 + zd^2]*area*\((cd*xd + cl*zd)\)/\((2*mass)\);\), "\[IndentingNewLine]", \(dragz[xd_, zd_]\ := \ rhoair*Sqrt[xd^2 + zd^2]* area*\((\(-cd\)*zd + cl*xd)\)/\((2*mass)\);\), "\[IndentingNewLine]", \(Print["\"]\)}]}]], \ "Input"], Cell[BoxData[ \("Default Values Set"\)], "Print"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{ StyleBox[\( (*\ Get\ User\ \(inputs : \ Cell\ maybe\ skipped\ if\ defaults\ to\ be\ used\)*) \), "Commentary"], "\[IndentingNewLine]", \(\(lht\ = \ Input["\"]*1000;\)\[IndentingNewLine] \(lvel\ = \ Input["\"];\)\[IndentingNewLine] \(ltheta\ = \ Input["\"]*Pi/180;\)\[IndentingNewLine] \(mass\ = \ Input["\"]/1000;\)\[IndentingNewLine] \(area\ = \ Input["\"];\)\[IndentingNewLine] \(cl\ = \ Input["\"];\)\[IndentingNewLine] \(cd\ = \ Input["\"];\)\[IndentingNewLine] Print["\"]\[IndentingNewLine] \)}]], "Input"], Cell[BoxData[ \("User Values Set"\)], "Print"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(\(\( (*\ Set\ up\ differential\ equations\ to\ be\ be\ solved, \ x[t]\ is\ horizontal\ position, \ z[t]\ is\ height\ of\ object\ *) \)\(\[IndentingNewLine]\)\(vz\ = \ lvel*Sin[ltheta]; \ vx\ = \ lvel*Cos[ltheta];\[IndentingNewLine] \(solution\ = \ NDSolve[{\(z''\)[t] \[Equal] grav[z[t]] + dragz[\(x'\)[t], \(z'\)[t]], \[IndentingNewLine]\(x''\)[ t]\ \[Equal] \ dragx[\(x'\)[t], \(z'\)[t]], \[IndentingNewLine]x[ 0]\ \[Equal] \ 0, \ z[0]\ \[Equal] \ lht, \ \[IndentingNewLine]\(x'\)[0]\ \[Equal] \ vx, \ \(z'\)[0]\ \[Equal] \ vz}, {x, z}, {t, 0, 100}];\)\[IndentingNewLine] \(hz[t_] := \ Evaluate[z[t] /. solution];\)\[IndentingNewLine] \(hx[t_] := \ Evaluate[x[t] /. solution];\)\[IndentingNewLine] (*\ Compute\ the\ error\ in\ the\ distance\ *) \[IndentingNewLine] \(zerot\ \ = \ t\ /. \ FindRoot[ hz[t] \[Equal] 0\ , {t, 1, 50}];\)\[IndentingNewLine] Print["\