Professional Documents
Culture Documents
Basic Mathematica Slide
Basic Mathematica Slide
Contents
1. Basic introduction............................................ 2. Some basic rules of Mathematica syntax........ 3. Numerical calculation and Built-In functions.... 4. Declaring a function.......................................... 5. Simple Plot....................................................... 6. Calculus............................................................ 7. Basic Programming.......................................... 8. Some interesting problems...............................
Basic Introduction
# Mathematica is a computational software program used in scientific, engineering, and mathematical fields and other areas of technical computing. It was conceived by Stephen Wolfram
# By the time he was 15 he began to research in applied quantum field theory and particle physics and publish his first scientific papers
# In order to simplify the mathematics portion he develop some sort of programming which eventually developed as Mathematica.
# you can just click the icon of Mathematica on your computer then you will get the Mathematica interface as shown below, where you can type the commands and start computations.
Generally, Mathematica tries to return an exact value unless otherwise specified with N. Example: Exp[-5] simply give e^(1/5), if we want the Numeric Value we should use In built constant 'N'; N[Exp[-5] ] returns the numeric value 0.00673795.
mathematica
TrigToexp reduces trigonometric to exponential ExpToTrig reduces exponential to trigonometric TrigExpand multiply or rewrites the function in argument of x. Example TrigExpand[Cos[3x]] returns Cos[x]^3- 3 Cos[x] Sin[x]^2 TrigReduce rewrites products and powers of trigonometric functions in terms of trigonometric functions With combine arguments; simply products to sum Example:TrigReduce[Sin[3x] Cos[4x]] returns (- Sin[x] + Sin[7 x])
Note: Mathematica has built-in definitions of many commonly used constants. In particular, e = 2.71828 is denoted by E, is denoted by Pi, and i : imaginary no is denoted by I. Example : N[Pi, 10] Returns the 10 digit approximation of pi 3.141592654
Declaring a Function
An elementary function of single variable y = f(x) is defined using the form f [x_] = expression in x.
?f
Clear[f]
Simple Plot
Plot [ Sin [ x ] , { x, 0, 2 Pi } ] (*Graph a function*)
We can simply plot more than one function at a time Plot[ {Sin[ x] , x^2 /10} , {x,0,2Pi}]
Plot can accept several really cool options for making our graphs look a lot better. 1. If we want the scales on each axes to appear to be the same we need to change the aspect ratio of the graph. We use the command AspectRatio Plot[ {Sin[ x] , x^2 /10} , {x,0,2Pi}, AspectRatio->Automatic]
We can control the exact region of the plot by using the option PlotRange {{xmin, xmax},{ymin, ymax}}. Using this option will force the plot to extend horizontally from xmin to xmax and vertically from ymin to ymax. Plot[E^x, {x, -1, 10}, PlotRange -> {{-1, 4}, {0, 10}}]
We can use PlotLabel option to label the graph Plot[E^x, {x, -1, 10}, PlotRange -> {{-1, 4}, {0, 10}}, PlotLabel->Name of the Graph]
Frame and FrameLabel options help to label the axes Plot[E^x, {x, -1, 10}, PlotRange -> {{-1, 4}, {0, 10}}, PlotLabel -> "G" , Frame -> True, FrameLabel -> {x label, "y label"}]
To graph the parametric equations x = x(t), y = y(t), a <= t <=b, use ParametricPlot[{x[t],y[t]},{t,a,b}]
PLOT
PLOT
r=cos(8/3)
Calculus
# Limit[f[x],xa] One-Sided Limits; Limit [f[x],xa,Direction-1] Limit[f[x],xa,Direction1] #Differentiation: D[f[x],x] or f'[x] f[x] D[f[x],{x,n}] #Integration: Integrate[f[x], {x xmin, xmax}] the range xmin to xmax.*) (*integrate f(x) with respect to x I /computes and returns f '(x) = d f / dx, /computes and returns f(x) = df / dx /computes and returns f(x) = df / dx (*computes right hand limit*) (computes left hand limit.*) (*find the limit of f(x) as x approaches a.*)
Solve[lhs=rhs,x] (*solve for x*) Nsolve[lhs=rhs,x] (*return numerical value of x*) Mathematica cannot Solve a simple equation such as cos x = x. It cannot even NSolve it. This is because the equation is not algebraic; it is a transcendental equation. There is, however, another command that can be used in such situations: FindRoot[lhs=rhs,{x,a}] (* attempts to numerically solve equation for x near xa. Dsolve[y'[t]==f[t,y[t]],y[t],t] (*solve differential equation of the type y'[t] = f[t,y] for y)
Basic Programming
If function The syntax for the If function is If[condition, t, f] When the If function is evaluated, the condition is evaluated to see if it is True or False. If it is True, then the expression t is returned, otherwise the expression f is returned. Do loop The Do loop is a definite loop. Definite loops are ones where the body of the loop is executed a definite number of times that is set before the program is executed.
While loop while loop is an indefinite loop. Indefinite loops are ones where the body is repeated an indefinite number of times, depending on conditions that are not necessarily known in advance. While loop has a syntax While[test,body] When the loop is executed, the test is evaluated. If it is True, the body of the loop is executed. The process is then repeated until test is False. If test never becomes False the loop will go on forever, what we call an infinite loop