Physics 230 -- Lab 1 (Introduction to Mathematica)

Fall 2015 - Transtrum

Labs by John S. Colton, BYU Physics & Astronomy, based on previous labs by Branton Cambell and Steve Turley.

Mathematica is a cross between a souped-up calculator and a full-blown programming language. You will mostly be typing and entering commands. 

To enter a command: Move the pointer/cursor to a spot between two “cells” (the smallest unit grouped by the brackets on the right), identifiable because the mouse cursor turns into a horizontal indicator, and type what you want Mathematica to do. Then press “shift-enter” (or just “enter” if you use the enter key on the numeric keypad) to have your command executed. Regular “enter” will not work--that’s for adding line breaks inside a command, for example if you want to run two commands at the same time.  If “shift-enter” doesn’t execute the command, then select the new cell (click on the vertical bracket on the right), then choose menu option Format→Style→Input and try again.

Probably the most helpful advice you will get this semester: If you are typing Mathematica commands and things are not working the way you expect, go to Evaluation→Quit Kernel→Local. That clears the memory and will restart the Mathematica engine the next time you enter a command. Just as the way to solve most computer problems is to turn it off and on again, the way to solve most Mathematica problems is to turn off the kernel (the processing engine) and then turn it on again. Evaluation→Abort can also be quite useful, to stop the execution of the current command if things are taking much longer than expected. 

To expand/contract the selections below: Double-click the vertical bars on the right that look like they have half of a downward-pointing arrow. To contract them, double-click the same bar (now potentially quite long). Or, go to  Edit→Preferences→Interface  and check the box that says “Show open/close icons for cell groups” (it may already be checked). That will give you carat-shaped icons on the left which will toggle the sections between expanded and contracted.

To expand/contract all groups at once: Here are two potentially useful shortcuts. To expand all groups at once, type Control-A (to select everything in the notebook), then choose the menu option Cell→Grouping→Open all subgroups. Similarly, to contract all groups, type Control-A then use menu option Cell→Grouping→Close all subgroups. Or, to close some but not all of the groups, just select the ones you want to close instead of using Control-A.

The Most Useful Commands

How to use Mathematica as a calculator

Calculator commands: Basic calculator-like commands such as +, -, *, /, and ^ all work pretty much like you’d expect. Regular parentheses can be used to group terms. Click on each of the equations below, and then press shift-enter to run these commands. When you do so, the equations will become labeled as input lines and output lines with the solution will appear. To experiment on your own, feel free to edit an equation on an input line and press shift-enter again. Or start typing in between cells, as mentioned above, to create your own input. Note, a “Suggestion Bar” will appear the first time that you execute a command. It can be closed with the “x” icon on the right.

1+1

4-5

6*5

6^5

1+(2-3)/5

Scientific notation can be indicated two ways. In the second method, there is no space between the number and the * character.

6.022 * 10^23

6.022*^23

You can either specify “times” by the * character, or more simply by leaving a space between two numbers. Mathematica will add the x character automatically. That’s what I did here:

6 5

Tip: Shift-enter(keypad) not only executes the command like shift-enter and enter(keypad) do, it also positions the cursor at the next available input field. You may find that to be a helpful shortcut when evaluating a series of commands in a row.

Another Tip: To evaluate all of the commands in a notebook, use Control-A to select all, then shift-enter. I wouldn’t necessarily advise doing that here, but it will be very useful in your own notebooks.

Built-in Functions: Giving Mathematica a function as a command is termed “calling the function”, or issuing a “function call”. The parameters that you use when calling the function are called the “arguments”. There are a ton of built-in functions. For example:

Log[1]

Log10[100]

Sqrt[10]

N[%]

N[Sqrt[10],50]

Cos[1]

N[%]

Things to observe:

1. Function calls such as Log and Sqrt must use square brackets. 

2. Pre-defined functions such as Log and Sqrt must be capitalized. 

3. The “%” symbol gets interpreted as “the result of the previously- executed command”. %% means “the result of two commands ago” (and so on as you add more % signs). % followed by a number means “the result of line <number>”

4. The “N” command can be used to force a numerical decimal result. I use N[%] all the time when a command doesn’t give me a numerical result like I wanted.

5. Commas are used to separate additional arguments (parameters) for functions. The second argument to the N command, if used, specifies the number of significant figures you want Mathematica to supply.

6.  Trigonometric functions must use radians. The trig functions include Sin, Cos, Tan, ArcSin, ArcCos, ArcTan, and the equivalent hyperbolic trig functions Sinh, etc.

The second most helpful piece of advice you’ll get this semester:  A large part of this class will involve learning many of the built-in functions. Mathematica has outstanding online help. To find out more information about any function, click on the function name and press F1. Try that with some of the built-in functions above. You can also access help topics organized by category, and/or search for a particular topic by going to Help→Documentation Center. One cool feature is that you can actually edit examples inside the help documents and execute them with shift-enter.

Variables: You can assign names to store your results into the computer's memory as "variables".

a = 15

b = 2

a b

b = 35

a b

a^b

c = a-b

c

b1 = 42

thisNumber=999999999

y+z

y+z * thisNumber

Things to observe:

1. You can give the variables any name you want (with a few exceptions), then you can refer to them by that name later.

2. You can use numbers in the variable name, like “b1” above, but you can’t start the name with a number.

3. You can have capital letters in the name of a variable, but it’s best not to start the name with a capital letter since that’s the way all of Mathematica’s built-in functions start.

4. If you try to use variables that have not been defined as anything, Mathematica will leave the answer as a symbolic expression. You can use this to do algebra, for example. More on that later.

Built-in Constants: Some variable names are reserved, such as Pi, E, I, and Infinity:

Sin[Pi/2]

N[E,50]

(2 + 3 I) / (4 - 7 I)

2^(-Infinity)

E = 10

Things to observe:

1. Mathematica treats Pi and E just as you’d expect, as if you had defined them yourself to hundreds of decimal places.

2. A capital I is used to indicate complex numbers.

3. Infinity is kind of a strange constant, but it also works like you’d expect.

4. You cannot set the value of the built-in constants yourself. That’s why the last command gave an error messaage “Symbol e is protected”. This tends to be a problem with the letters “E” and “I” which are very tempting to use to stand for energy, electric field, or current. Remember when defining your own constants it’s best to start them with a lower case letter. That will prevent this problem with e or i.

Now try it out on your own: move the mouse directly below this paragraph (it's a break between cells) and when the mouse pointer turns into a horizontal bar, click to position the cursor there, and start typing. Calculate something that you might use a regular calculator for.

↑
Enter your command above the arrow. (Notice the break between cells is indicated on the right.) Remember if “shift-enter” doesn’t execute the command, then select the new cell (click on the vertical bracket on the right), then choose menu option Format→Style→Input and try again. That won’t generally be an issue when you create your own new notebooks, but it may be here.

Now open up a brand-new notebook file using File→New→Notebook (or Control-N) and type in some  commands there. The formatting won’t look as cool as this notebook, but we’ll cover formatting later.

CAUTION:  Mathematica "remembers" variable assignments between notebooks. That is, if you've defined a=15 in this notebook, when you open a new notebook, it will equal 15 there, too! This can often cause unintended (and undesired) consequences. The "quit kernel" tip from the top of this notebook can be especially helpful under those circumstances.

An aside: how to leave comments

The commands you’ve seen so far have been very simple and intuitive. That will change. In a complicated sequence of commands it's critically important to leave comments for yourself (and others), so that you (and they) will know what was going on in your head when you wrote the code that way. You do that in Mathematica via paired parentheses/asterisks:

hbar = 1.055*^-34   (* this is Plank's constant, divided by 2 Pi *)

There’s a nice shortcut for toggling comments on and off: select the text you want to comment, then type Alt-?. Try that, here:

comment this out with Alt-?

Assignment 0:  Basic computation (You should do this, but you don’t need to pass it off)

In a new notebook or in a newly created cell below this assignment, do these computations. It might be good to check your answer against your calculator.
(a)  (Sqrt[1+((2/(3-(Subscript[log, 10])4)))])  (give the answer to 10 sig figs)
(b) the answer to (a) + ln(2.39).

How to define a function

You can define your own functions, such as:

f[x_] = - x^4 + 3 x^3 +  4 Sin[x]

f[2]

N[%,30]

f[a]

N[%]

f[y]

f[x^2]

Things to observe:

1. Function definitions must use the underscore symbol after the variable (and square brackets!). Function calls do not use the underscore symbol.

2. You can evaluate your new function at a particular number such as 2 just the same way you would use a built-in function.

3. You can evaluate your function at a particular variable, or even at a function of a variable. In the examples above f[y] doesn’t get evaluated at a particular number because y is undefined. It just gets evaluated at the unknown variable “y”. And just as Sin[x^2] will compute sin of (x^2), f[x^2] will compute f of (x^2).

How to plot a function

Plot[f[x],{x,-2,4}]

Plot[f[x],{x,-2,4}, PlotRange->{-20,20}]

Plot[Sin[x]/x,{x,-20,20}]

Plot[Sin[x]/x,{x,-20,20}, PlotRange->All]

Things to observe: 

1. Commands such as plot are treated in much the same way as functions such as sin or cos. In fact, I’ll likely often call them functions. In particular, like all other commands/functions, “plot” must be capitalized. 

2. Again, all commands/functions must be called with square brackets. 

3. When a function argument contain multiple values rather than a single number, it is typically done via curly braces. In this case, the second argument of the plot command, “{x,-2,4}” specifies the variable to be plotted, the start of the plot range, and the end of the plot range. 

4. The right arrow is often used to specify function parameters. It is produced automatically when you type “-” followed by “>”.
 
5. There are various settings you can give to the Plot command via additional arguments; PlotRange is probably the most useful of them. It specifies the vertical range for occasions when the automatic vertical scale doesn’t give you the range you want to see. The two most common ways to do it are to specify the range yourself with PlotRange->{low,high}, or to force Mathematica to display all y-values using PlotRange->All.

The third most helpful advice you will get this semester: Be very VERY careful when choosing between parentheses (always used for arithmetic grouping), square brackets (always used in function calls/definitions), and braces (always used for lists of items). Mathematica is EXTREMELY picky about which is which, and will give you errors if you use them incorrectly. That's both good and bad--bad, because new users can easily get frustrated, but good, because once you get a little more experience the standardized notation protects you against doing things wrong accidentally.

Plotting two functions: You can plot two functions like this:

f1[x_]=Sin[x]

f2[x_]=Cos[x]

Plot[{f1[x],f2[x]},{x,0,10}]

or like this :

plot1=Plot[f1[x],{x,0,10}]

plot2=Plot[f2[x],{x,0,10}]

Show[plot1,plot2]

Things to observe: 

1. To plot two (or more) functions together, the easiest way is usually to group the functions with braces and use them together as the first argument of the Plot command.

2. You can give names to ANY output of Mathematica commands, not just numerical outputs. Notice how I named the two plots “plot1” and “plot2”. 

3. The Show command can be useful to merge plots together. Notice because it was merging plots together rather than creating a new plot with two functions, it didn’t plot the functions with two separate colors as the first combined plot did. It’s very helpful if you want to combine two different types of plots, like a plot of data points combined with a regular plot like those above (more on that in a later lab).

How to integrate a function

The ability to perform symbolic computations, such as integrating a function, is a large part of what makes Mathematica so useful. Rare is the physics professor who will make you perform integrals by hand any more.

How to integrate a function symbolically:

f[x_] = - x^4 + 3 x^3 +  4 Sin[x]; (* in case you didn't define it above *)
Integrate[f[x],x]

Integrate[(Sin[x])^2,x]

Integrate[f[x],{x,y,3 y}]

Things to observe: 

1. The second argument of the Integrate command ("x" vs "{x,y,3y}", in the examples above) specifies whether you want an indefinite or a definite integral, and (if the latter) what the limits of integration are.

2. You can either type the function itself into the Integrate command or, if the function has been user-defined, call the defined function (f[x] in this case). Or any sort of mixture of the two.

How to integrate a function numerically:

f[x_] = - x^4 + 3 x^3 +  4 Sin[x]; (* In case you didn't define this function above, I've repeated this command here. The semicolon supresses the output of the command *)Integrate[f[x],{x,0,Pi}]

N[%,50]

NIntegrate[f[x],{x,0,Pi}]

N[%,50]

Integrate[E^(-Sin[x]^2),{x,-1,1}]

NIntegrate[E^(-Sin[x]^2),{x,-1,1}]

NIntegrate[E^(-Sin[x]^2),{x,-1,1},WorkingPrecision->50]

Integrate[E^(-x^2),{x,-Infinity,Infinity}]

Things to observe: 

1. Both the Integrate and the NIntegrate commands can be used to numerically evaluate an integral. The differences are subtle: the first command first symbolically integrates the function, then plugs in the numbers afterwards. The second command does a numerical integration from the get-go, and can only handle definite integrals. Typically the first one is more useful because it will give you an exact answer if it can give you an answer at all. That’s why the first N[%,50] was able to display fifty decimal places but the second one was not. But sometimes Integrate fails, as in the “E^(-Sin[x]^2)” example, in which case NIntegrate will be the way to go. And if you need more precision than the default setting of NIntegrate, there are options such as WorkingPrecision which can give you that.

2. Mathematica is very sensitive to capitalization. Typing “Nintegrate” will not work at all. If you have any question about how the name of a function should be spelled/capitalized, type your best guess for the name, click on what you just typed, then press F1 to get help.

3. As mentioned above, Infinity is treated as a special constant. It’s often quite useful in integrals.

Assignment 1: Position is the integral of velocity

Define a function of time: v(t) = v0 - g t, where v0 is a variable defined to be 5 and g is a variable defined to be 9.8. Define x(t) to be the integral of that function. Plot both v(t) and x(t) on same graph with different colors, for t = 0 to 2.

How to differentiate a function

Finding derivatives is another very useful Mathematica feature.

fderiv[x_] =f'[x]

fsecondderiv[x_] = f''[x]

Things to observe: 

The tick mark  '  tells Mathematica to calculate the derivative. (It’s produced by the apostrophe key.)  Two tick marks gives you a second derivative. The “D” function can also be used for derivatives; see Mathematica’s online help for more details on that.

How to find when a function equals a specified number

FindRoot[f[x]==-10,{x,-1}]

FindRoot[f[x]==0,{x,3}]

Things to observe: 

1. That’s a double equals sign, ==, not a single equals sign, =.  The double equals sign means “test for equality”. In other words, the FindRoot command works by varying x many, many times and testing for equality each time, until it finds the value of x that makes the two sides equal.

2. The FindRoot command actually doesn’t find the roots. It finds when the left side of the double equals sign is equal to the right hand side. If you type in “0” for the right hand side, or if you leave off the right hand side completely, it will indeed find a root of the equation--but in general it is much more flexible than that. This is a very powerful/helpful command!!  Along with making plots and calculating integrals/derivatives, it’s probably the Mathematica command that I personally use the most.

3. The second argument of the FindRoot command, for example “{x,-1}”, specifies where you would like Mathematica to start looking. You can translate the first FindRoot command into English like this: “Find the place around x = -1 where f(x) equals -10.”

Tip: It’s often good to do a plot before you use the FindRoot command, so you can be clever about where you tell Mathematica to start looking.

Assignment 2. Solving an equation, numerically

For what value of x (in radians) does cos(x) = x?  
(a) Start by plotting cos(x) and x on the same graph. You can get an approximate answer by looking at where the two curves intersect: that’s where cos(x) equals x. 
(b) Then use FindRoot to get a precise answer.

How to use the Solve command

Solve[r x == s (3 x - 5), x]

Solve[3 x^2 == 4 (3 x - x^2) +5, x]

N[%]

Solve[ {x + 2 y == 3 , 4 x + 5 y ==6 },{x,y}]

Things to observe:

The Solve command is a lot like FindRoot, and is extremely useful when solving equations. It allows you to do a multitude of tasks: isolate a variable in an algebraic expression, solve a quadratic equation (or any polynomial, I believe), even solve simultaneous equations. Take note of the required syntax; among other things, it uses the "double equals" like FindRoot. Despite its power, however, it also has serious limitations--which is why I myself often turn first to FindRoot if all I want to do is solve numerically for a particular value of x in an equation. On the other hand, FindRoot will only find real numbers, whereas Solve can find complex number solutions.

How to find the maximum/minimum of a function

There are multiple ways to do this job. Here is one of my favorites:

FindRoot[f'[x]==0,{x,2}]

Things to observe: 

1. I’ve simply combined two functions in one: first the derivative function,  ' , to compute the derivative of the function f, and then the FindRoot command to find where the derivative equals zero. I gave it x=2 as a starting point because I knew from the original plot of f(x) above that x=2 was close to where f(x) had a maximum. Again, making a plot first is often a very useful thing to do.

2. The output of this command just tells you the value of x when f(x) is a maximum. To determine the value of the function itself, you’d have to additionally evaluate f(x) at that value of x.

Here's a built-in command to do the same thing:

FindMaximum[f[x],x]

Things to observe: 

1. This command gives you the maximum value of the function in addition to the value of x for which the function has that maximum.

2. There are a tremendous number of built-in functions. This happens to be one that I forgot about for a few years until I rediscovered it. Is it a valuable function? Yes, but since it’s so easy to mimic as I did with FindRoot using the derivative, it’s not nearly as valuable to me as the FindRoot function.  But sometimes it can be worth your while to type a command that you think might exist, then press F1 to see if it shows up in the help file.

Assignment 3: Diffraction from a slit

In Physics 123 you hopefully learned that the diffraction pattern produced by a single slit of non-negligible width is given by:  I(x) = (Subscript[I, 0])  (((((sin(πxa/λL))/(πxa/λL))))^2), where a is the slit width, L is the distance to the screen where the pattern is being observed, and x is the position on the screen.  (Disclaimer: this involves a small angle approximation.) Sometimes that equation is written in terms of the "sinc" function, where sinc(x)=((sin(x))/x). (Incidentally, Sinc is a pre-defined function in Mathematica, just like Sin itself.) In the equation, (Subscript[I, 0]) is the intensity at x = 0, i.e. the mid-point of the diffraction pattern; let's just define that as 1 for simplicity.
(a) Make a plot of this function for a few maxima/minima around the central maximum, for a 100 micron slit, a 633 nm laser, and a distance to screen = 10 m. Make two plots, actually: one with the default y-axis (meaning intensity)  range, and a second one where you use a PlotRange option to “zoom out” and fully see the central peak.
(b) Determine the position of the first maximum to the right of the central maximum. Side note: In Physics 123 you learned a simple formula for the minima, but there is no similar simple formula for the maxima. They are only approximately located equidistant between the minima.

How to perform a summation

Sum[1/n^2, {n,1,100}]

N[%, 20]

Sum[1/n^2,{n,1,Infinity}]

N[%]

Sum[ n x^n, {n,1,10}]

exptaylor[x_] = Sum[ x^n /n! , {n,0,1000}];
exptaylor[1.2]

E^1.2

Things to observe:

1. The Sum command can be very useful to quickly compute numerical summations. The first summation above would be read as “Sum 1/n^2, with n ranging from 1 to 100”. The second argument (which must be in braces) indicates the variable to sum over as well as the summation range, just as in the Integrate command.

2. Sum can be used to determine the value of an infinite summation.

3. Sum can also be used to define functions which include symbolic summations, and in function definitions. I’ve defined exptaylor[x_] to be the sum of the first 1000 terms of the Taylor series for e^x.

4. As mentioned above, a semicolon can be used at the end of a command to suppress the output. Try the exptaylor[x_] function definition command without the semicolon.

5. As you might expect, the “!” symbol can be used to do factorials.

Assignment 4. Two summations

(a) Calculate the value of  (∑_(n=1)^∞)(1/(n^6)), in both symbolic and numerical form. 
(b) Calculate the value of 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...

How to simplify results

Simplify[1+4 x+6 (x^2)+4 (x^3)+(x^4)]

complicatedexpression =1/(3 (1+x))-(-1 + 2x)/(6 (1-x +x^2))+2/(3(1 + (1/3) (-1 +2 x)^2))

Simplify[complicatedexpression]

complicatedexpression[2]

Things to observe:
  
1. A lot of time symbolic integration or differentiation will give output that looks like the complicated function above. I've gotten into the habit of typing "Simplify[%]" whenever I obtain some output that looks at all like it might be simplifyable.

2. You don’t always have to define a function using the bracket & underscore notation. However, if you don’t you have to be really careful because the results might not be what you expect. In the example above, it might look like I have defined “complicatedexpression” as a function of x. However, what I have actually done is defined “complicatedexpression” as a variable whose value is equal to the three terms involving x. That’s fine here, since I was using the term “complicatedexpression” strictly in place of where I would have to type the expression itself. But if you try typing, for example, “complicatedexpression[2]” to evaluate the function at x=2, Mathematica won’t know what to do. There are ways to treat expressions like that as functions; we’ll get into that in the next lab.

A bit more algebra

Mathematica has many advanced algebraic functions. “Solve” and “Simplify” are two examples; here are a few more examples of algebraic-related functions that can be helpful. Figure out what each one is doing.

Expand[(1+x)^4]

Together[1/x+1/y]

Apart[(x+y)/(xy)]

longexpression = 1+4 r+6 (r^2)+4 (r^3)+(r^4)+4 s+12 r s+12 (r^2) s+4 (r^3) s+6 (s^2)+12 r (s^2)+6 (r^2) (s^2)+4 (s^3)+4 r (s^3)+(s^4)

Collect[longexpression,s]

Collect[longexpression,s, Simplify]

Things to observe:

Collect is one of the subtlest functions of that bunch. It attempts to group the expression in powers of the specified variable. The “Simplify” option is a nice/helpful touch.

Cosmetics

Here are various ways you can clean up the look of your notebooks. You don’t have to try them all out, but do look them over so that when the need arises you can say to yourself, “Wasn’t there something about this in Lab1?”

How to make input cells look pretty

I personally prefer to type equations/expressions exactly the way you’ve seen in this notebook so far, typing them out using function names/parentheses/etc. However, some prefer using the math input “palette” to help you type them in. You can open that palette via Palettes→Basic Math Assistant. Explore the options there if you’d like.That palette has a host of formatting capabilities. One of those is the ability to add Greek letters (they are under “Typesetting”)--but if that’s all you need, my own preference to get Greek letters is via the Insert→Special Character menu item. Or even better, type Esc, then the name of the letter, then Esc again. 

You can also accomplish many formatting tasks using keyboard shortcuts. See for example the documentation on Two-Dimensional Input and Special Characters.

Changing the “style” of a cell

The most helpful thing under the Format menu is setting the style of a cell, under Format → Style → < various styles > . That allows you to change a cell to Text style, for example, so that it won’t be evaluated as input, or to create section titles, bullet points, and so forth. Try changing this cell to a different style to see what happens, resetting it to “Text” when you are done. Be sure to select the whole cell as you do this by first clicking on the relevant vertical bar on the right.

Changing the appearance of the whole notebook

The Format→Stylesheet submenu changes the look and feel of the whole notebook by altering the fonts and colors associated with each style.  The stylesheet used here is the Report→StandardReport stylesheet. Try changing this just for fun. You can edit existing stylesheets or even create new ones.

Also, Control-Mousewheel is a nice trick for making the text of the whole document get bigger or smaller, just as in most web browsers.

Changing the appearance of individual words

As in most word processors, Control-B and Control-I produce bolded and/or italicized text.  The Format menu options can also be used to alter the font/face/size/color of a specific word or phrase, though I myself don’t often bother with that capability.

Splitting/merging cells

The Cell→Divide Cell and Cell→Merge Cells options can sometimes be quite useful.  In the input cell below, place your cursor at the beginning of the second expression and split the cell into two separate cells.  Then select both cells and merge them back together again.  When the cells split or merge, notice that the cell brackets at the right also split and merge.

1+1
2/2

How to “clean up” output cells

The Cell→Delete All Output option deletes every output cell in the entire notebook.  This can be useful when cleaning up your notebook at the end of a session, but it can't be undone (although, as mentioned above, you can do a Control-A followed by shift-enter to reevaluate everything from scratch).

A glipse behind the curtain

Functions that are behind the shorthand

All Mathematica expressions down to something as simple as “2 + 2” can be written as function calls. Here is what Mathematica really is “thinking” when you type in some simple things.

3+4+5  
Plus[3,4,5]

x y z
Times[x,y,z]

x^2
Power[x,2]

q=4
Set[q,4]

What Mathematica actually "sees"

Mathematica not only interprets shorthand notation as functions, it actually interprets nearly everything as functions. Cell→Show Expression shows you how Mathematica  views a cell. Try it on this very simple text cell, for example. This feature can be toggled on and off -- so don't panic and accidentally delete the whole cell to escape the garbage on the screen!

Additional Assignments

Solve the following problems. Feel free to pass several of them off at once. If you get stuck, you can ask a TA or a nearby student for help.

Assignment 5. The plot thickens

Make a plot of (x^4) + 2(x^3) +3 (x^2)+ 4 x+5, from x = -3 to x = 3. Experiment with different plot styles as options, such as PlotStyle->Thick,  or PlotStyle->Red (or Blue, or Green, or Magenta, etc.), or PlotStyle->Dashed  (or Dotted, or DotDashed), or PlotStyle->Opacity[0.5] (or Opacity[0.1], or Opacity[0.8], etc.). After you have experimented a bit with these options, choose your favorite plot styles and combine them together via a list enclosed in braces: PlotStyle->{option1, option2, option3, etc.}.

Assignment 6. Polynomial roots

As you should have just seen from the plot, the polynomial x^4 + 2 x^3 + 3 x^2 + 4 x + 5 has no real roots. (It doesn' t cross zero anywhere.) It does, however, have four complex roots. What are they? Find them in decimal form.

Assignment 7: Taylor’s series of (e^x)

The Taylor’s series of (e^x) evaluated at x = 1 is 1 + 1 + 1/2 + 1/3! + 1/4!  + ... .  Show that this series gets closer and closer to the true value of e  (=(e^1)) as you include more and more terms. Try 5 terms (as shown), 10 terms, and 100 terms; give your results to 50 decimal places. Also display e to 50 decimal places for comparison.

Assignment 8. A basic derivative

Define the function f8(x) = (((sin^2)(x))/(1+cos(x))). Take its derivative, and simplify the result.

Assignment 9. A basic integral

Determine  ∫(e^(-x))sin(x)dx, integrated from 0 to infinity.

Assignment 10. A basic complex number calculation

Do a search in Mathematica's help files to find the "Complex numbers tutorial". Pay special attention to the Abs and Arg commands. Use them to calculate the magnitude and angle (in radians) of:  (4 + 5i) ÷ (6 - 7i).  Give your answers in decimal form to 8 sig figs.

Wrap-up

Click on any of the terms in blue to bring up the help file for more information.

The following functions were discussed in today’s lab, or are very closely related:
Operators:  +, -, *, /, ^, !
Basic Functions: Log, Log10, Sqrt
Trig Functions: Sin, Cos, Tan, ArcSin, ArcCos, ArcTan, Sinc
Forcing numerical output: N
Referring to previous results: %, %% (and so on), %[number]  (all related to Out function)
Built-in constants: Pi, E, I, Infinity
Functions: f[x_] notation to define your own function, f[x] to refer to function, Plot (and PlotRange option) to display function
Calculus-related functions: Integrate, NIntegrate, derivatives via ‘, ‘’ (and so on)  (all related to D function)
Misc: FindRoot, FindMaximum, FindMinimum, Sum, Solve
Complex Numbers: Abs,  Arg, Re, Im, Conjugate 

Potentially useful summary/tutorial pages:
Operators
Some mathematical functions 
Mathematical Constants 
Defining functions
Solving Equations
Comments (*  *)
Complex numbers tutorial