Download as odp, pdf, or txt
Download as odp, pdf, or txt
You are on page 1of 16

Introduction to Mathematica

Saroj Dhakal CDP, TU June27, 2013

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.

Basic Rules Of Mathematica syntax


Every word of in built function begin with capital letter. Eg: Pi, Sin,... Some, like the inverse cosine, ArcCos may even have many capital letters The argument of functions are given in brackets[..]whereas ( ) and{ } are used for algebraic expressions, and lists, respectively, and only for these purposes. You can use almost anything as a variable except that the variable name cannot start with the number. X2 can be used but not 2x. Moreover the words or letter that have already meaning in Mathematica cannot be used. E cannot be used as variable name because it stand for the base of natural logarithm. Multiplication is represented by * or the space between the characters. The delimiters (* and *) are used to enclose comments. Anything that appears between the delimiters is ignored by Mathematica when the cell is evaluated. Use of semicolon suppress the output. Also, we can write both commands on the same line with the use of semicolon. Mathematica follows the operation exactly.

Simple numerical calculations


Simple mathematical operations are performed in natural way in Mathematica a plus b is entered as a+b, a minus bis entered as a-b, a times b is entered as a b or a*b, a divided b is entered as a/b, a raise to power b is entered as a^b.

Some Built-in functions


Simple built in functions are the exponential function, Exp[x]; the natural logarithm, Log[x]; the absolute value function, Abs[x]; the trigonometric functions Sin[x], Cos[x], Tan[x], Sec[x], Csc[x], and Cot[x]; the inverse trigonometric functions ArcSin[x], ArcCos[x], ArcTan[x], ArcSec[x], ArcCsc[x], and ArcCot[x]; the hyperbolic trigonometric functions Sinh[x], Cosh[x], and Tanh[x]; and their inverses ArcSinh[x], ArcCosh[x], and ArcTanh[x].

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.

Besides these there are

some useful functions in built in

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.

Similarly f[x_,Y_] = expression in x and y.

?f

show the definition of f

Clear[f]

clear all definitions for 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

x = t + sin 2 t y = t + sin 3 t ; -2 \[CapitalPi] <= t <= 2 \[CapitalPi

Define a function And them Plot it

To graph the polar equation use PolarPlot[r[theta],{theta,alpha,beta}].

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

You might also like