Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To R

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

INTRODUCTION TO R

R is a programming language developed by Ross Ihaka and Robert Gentleman in 1993. R possesses
an extensive catalog of statistical and graphical methods. It includes machine learning algorithm,
linear regression, time series, statistical inference to name a few. Most of the R libraries are written in
R, but for heavy computational task, C, C++ and Fortran codes are preferred.

R is not only entrusted by academic, but many large companies also use R programming language,
including Uber, Google, Airbnb, Facebook and so on.

Data analysis with R is done in a series of steps; programming, transforming, discovering, modeling
and communicate the results

 Program: R is a clear and accessible programming tool


 Transform: R is made up of a collection of libraries designed specifically for data science
 Discover: Investigate the data, refine your hypothesis and analyze them
 Model: R provides a wide array of tools to capture the right model for your data
 Communicate: Integrate codes, graphs, and outputs to a report with R Markdown or build
Shiny apps to share with the world

Use Of R As A Calculator
Function What It Does

abs(x) Takes the absolute value of x

log(x,base=y) Takes the logarithm of x with base y; if base


is not specified, returns the natural logarithm

exp(x) Returns the exponential of x

sqrt(x) Returns the square root of x

factorial(x) Returns the factorial of x (x!)

choose(x,y) Returns the number of possible combinations when drawing


y elements at a time from x possibilities

Although the major purpose of R is to work with statistical data, it can also serve as
a calculator. It is not as powerful as a full computer algebra system, having no provision
for imaginary numbers or purely algebraic computations. It does know basic constants like
π, the usual trigonometric functions and inverses (with angles in radians), as well as the

natural exponential and logarithm function, so mathematicians will feel at home.

Example. We use R as a simple calculator.

> pi

[1] 3.141593

> sin[pi/2]

[1] 1

> sqrt[ 3^2 + 4^2 ] + log(1)*exp(0)

[1] 5

Note. The [1] that occurs at the left of the output technically means that the output of

each calculation is a list. That does not mean much in our simple examples, because each

output is a list with only one item. If the output were a list with enough items to wrap to

the next line, subsequent lines would get (higher) numbers indicating how far through the

list we had gone. That is, the sixth item would be preceded by [6], and so on.

R has an extensive help system containing information (with examples) about all the

built-in functions and constants.

Example. We learn from the help system how to do logarithms in other bases.

> help(log)

> log(64, base=4)

[1] 3

Variables in R are created with the assignment operator, which historically has been the

left arrow (<-). Modern versions of R also accept equals (=) for assignment, and you may

find reference documents that use either symbol.

Example . We define and use a couple of variables.

>a=5

> a^2

[1] 25

> b <- a

> b^2

[1] 25

A list of currently defined variables can be generated with ls(), and variables may be
removed with the rm() function.

Example - We define and remove a variable.

>a=5

> ls()

[1] "a"

> rm( a )

>a

Error: object "a" not found

In R, we take the logarithm of the numbers from 1 to 3 like this:


> log(1:3)
[1] 0.0000000 0.6931472 1.0986123

Whenever we use one of these functions, R calculates the natural logarithm if we don’t
specify any base.

We calculate the logarithm of these numbers with base 6 like this:


> log(1:3,base=6)
[1] 0.0000000 0.3868528 0.6131472

For the logarithms with bases 2 and 10, we can use the convenience
functions log2() and log10().

We carry out the inverse operation of log() by using exp(). This last function raises e to
the power mentioned between brackets, like this:
> x <- log(1:3)
> exp(x)

SCIENTIFIC NOTATION IN R
Scientific notation allows us to represent a very large or very small number in a
convenient way. The number is presented as a decimal and an exponent, separated
by e. We get the number by multiplying the decimal by 10 to the power of the exponent.
The number 13,300, for example, also can be written as 1.33 × 10^4, which is 1.33e4 in
R:
> 1.33e4
[1] 13300

Likewise, 0.0412 can be written as 4.12 × 10^–2 , which is 4.12e-2 in R:

> 4.12e-2
[1] 0.0412

R doesn’t use scientific notation just to represent very large or very small numbers; it
also understands scientific notation when we write it. We can use numbers written in
scientific notation as though they were regular numbers, like so:
> 1.2e6 / 2e3
[1] 600

R automatically decides whether to print a number in scientific notation. Its decision to


use scientific notation doesn’t change the number or the accuracy of the calculation; it
just saves some space.

Use of ‘c’ and ‘scan’ command


Function calls in R, such as ls(), always require parentheses, even when there are

no arguments. This is similar to some other programming languages, notably C. Without

the parentheses, R will return the definition of the function rather than evaluating it.

R works naturally with array variables, since data commonly occur in lists. The two

most typical ways to create arrays in R are via the c() concatenation function and the

scan() data input function.

Example: We concatenate several values together into an array a.

> a = c( 2,3,5,7,11 )

>a

[1] 2 3 5 7 11

The scan() function is usually more convenient for longer sets of data that can be

cumbersome to enter using the concatenation function. Data may be scanned from either

the keyboard or from a file. Individual data values should be separated by white space (by

default), either on the same line or on adjacent lines. It is possible to specify a different
delimiter for data that exists in other formats; check the help. The end of data is indicated

by the first blank line or the end of file.

Note. R will prompt with the position of the next item to read. If five items have already

been typed, the prompt is changed to 6: to indicate that the sixth item is next.

Example We read in the first ten prime numbers, as they are typed from the key-

board and then from an existing file called primes.txt.

> primes1 = scan()

1: 2 3 5 7 11

6: 13 17 19 23 29

11:

Read 10 items

> primes2 = scan( file="primes.txt" )

Read 10 items

> primes1

[1] 2 3 5 7 11 13 17 19 23 29

> primes2

[1] 2 3 5 7 11 13 17 19 23 29

Most mathematical operators in R work component-wise on arrays. For addition and

subtraction this is exactly the way mathematical arrays work. Unlike mathematical arrays,

R arrays can sometimes be combined even when they are not the same size. For example,

an array of length 3 can be added to an array of length 6 (and the answer is an array of

length 6). To make the process work, R will expand the shorter array by reusing entries

that start from the left; the array [1, 2, 3] would be expanded to [1, 2, 3, 1, 2, 3], for example.

152 14 Getting Started with R

Example.. We do some array arithmetic.

> a = c(1,2,3)

> b = c(5,5,5,5,5,5)

> a^2

[1] 1 4 9

> 4+a

[1] 5 6 7
> a+b

[1] 6 7 8 6 7 8

> sin( a*pi/2 )

[1] 1.000000e+00 1.224606e-16 -1.000000e+00

Explore and describe data


R includes descriptive statistics and plots for summarizing data. It can compute measures of center,
like mean and median, as well as measures of spread, like (sample) standard deviation and range.

Example .. We compute some statistics for a set of exam scores.

> scores = scan()

1: 81 81 96 77

5: 95 98 73 83

9: 92 79 82 93

13: 80 86 89 60

17: 79 62 74 60

21:

Read 20 items

> range(scores)

[1] 60 98

> median(scores)

[1] 81

> mean(scores)

[1] 81

> sd(scores)

[1] 11.3555

There are a number of plots and charts in R for presenting or exploring data. For

example, we might wonder if a set of exam scores is normally distributed. A stem and leaf

plot can help us decide this, and R can generate one.

Example. We explore a group of exam scores to learn about the shape of the Distribution.

> scores = scan()

1: 81 81 96 77
5: 95 98 73 83

9: 92 79 82 93

14.3 How to explore and describe data 153

13: 80 86 89 60

17: 79 62 74 60

21:

Read 20 items

> stem(scores)

The decimal point is 1 digit(s) to the right of the |

6 | 002

7 | 34799

8 | 0112369

9 | 23568

> stem(scores,scale=2) # same plot stretched twice as long

The decimal point is 1 digit(s) to the right of the |

6 | 002

6|

7 | 34

7 | 799

8 | 01123

8 | 69

9 | 23

9 | 568

Use of rm() command


We can remove objects from memory and therefore permanently delete them using the rm()

or remove() commands. To remove objects we can simply list them in the parentheses of the

command:

rm(list)

remove(list)

We can type the names of the objects separated by commas. For example:

>rm(answer1, my.data, sample1)


This removes the objects answer1, my.data, and sample1 from the workspace. We can use the

ls() command to produce a list, which will then be deleted. We need to include the instruction

list in the command like so:

>rm(list = ls(pattern = '^b'))

Here the ls() command is used to search for objects beginning with “b” and remove them.

WARNING Use the rm()command with caution; R doesn’t give you a warning

before it removes the data you indicate, it simply removes it after receiving the

command.

Pie chart
If we have data that represents how something is divided up between various categories, the pie
chartis a common graphic choice to illustrate our data. For example, we might have data that shows
salesfor various items for a whole year. The pie chart enables us to show how each item contributed
tototal sales. Each item is represented by a slice of pie—the bigger the slice, the bigger the
contribution to the total sales. In simple terms, the pie chart takes a series of data, determines the
proportion of each item toward the total, and then represents these as different slices of the pie.

NOTE : The human eye is not really that good at converting angular measure-

ments (slices of pie) into “real” values, and in many disciplines the pie chart is

falling out of favor. However, the pie chart is still an attractive proposition for

plenty of occasions.

The pie chart is commonly used to display proportional data. You can create pie charts using the
pie() command. In its simplest form, we can use a vector of numeric values to create your plot like
so:

> data11=c(3,5,7,5,3,2,6,8,5,6,9,8)

>data11

[1] 3 5 7 5 3 2 6 8 5 6 9 8

When we use the pie() command, these values are converted to proportions of the total and then

the angle of the pie slices is determined. If possible, the slices are labeled with the names of the
data.

In the current example you have a simple vector of values with no names, so you must supply them

separately. We can do this in a variety of ways; in this instance we have a vector of character labels:

> data8= ("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")

>data8

[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
To create a pie chart with labels we use the pie() command in the following manner:

> pie(data11, labels = data8)

We can alter the direction and starting point of the slices using the clockwise = and init.angle

= instructions. By default the slices are drawn counter-clockwise, so clockwise = FALSE; we

can set this to TRUE to produce clockwise slices. The starting angle is set to 0o (this is 3 o’clock) by

default when we have clockwise = FALSE. The starting angle is set to 90o (12 o’clock) when we

have clockwise = TRUE. To start the slices from a different point, w simply give the starting

angle in degrees; these may also be negative with –90 being equivalent to 270o.

The default colors used are a range of six pastel colors; these are recycled as necessary. We can

specify a range of colors to use with the col = instruction. One way to do this is to make a list of

color names. In the following example we make a list of gray colors and then use these for our

charted colors:

> pc = c('gray40', 'gray50', 'gray60', 'gray70', 'gray80', 'gray90')

> pie(data11, labels = data8, col = pc, clockwise = TRUE, init.angle = 180)

We can also set the slices to be drawn clockwise and set the starting point to 180o, which is

9 o’clock. The resulting plot looks like Figure

Dot chart
An alternative to the pie chart is a Cleveland dot plot. All data that might be presented as a pie

chart could also be presented as a bar chart or a dot plot. We can create Cleveland dot plots

using the dotchart() command. If our data are a simple vector of values then like the pie()

command, we simply give the vector name. To create labels we need to specify them. In the

following example we have a vector of numeric values and a vector of character labels; we met

these earlier when making a pie chart:

> data11; data8

[1] 3 5 7 5 3 2 6 8 5 6 9 8

[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

> dotchart(data11, labels = data8)

The resulting dot plot looks like Figure 7-23.

Bar Charts
The bar chart is suitable for showing data that fall into discrete categories. “Starting
Out: Working with Objects,” we met the histogram, which is a form of bar chart. In that example

each bar of the graph showed the number of items in a certain range of data values. Bar charts are

widely used because they convey information in a readily understood fashion. They are also flexible

and can show items in various groupings.

We use the barplot() command to produce bar charts. In this section we see how to create a range

of bar charts, and also have a go at making some for ourself by following the activity at the end.

Single-Category Bar Charts


The simplest plot can be made from a single vector of numeric values. In the following example we

have such an item:

> rain

[1] 3 5 7 5 3 2 6 8 5 6 9 8

To make a bar chart we use the barplot() command and specify the vector name in the instruction

like so:

barplot(rain)

This makes a primitive plot that looks like Figure

The chart has no axis labels of any kind, but we can add them quite simply. To start with, we can

make names for the bars; we can use the names = instruction to point to a vector of names. The

following example shows one way to do this:

> rain

[1] 3 5 7 5 3 2 6 8 5 6 9 8

> month

[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

> barplot(rain, names = month)

In this case we already had a vector of names; if we do not, we could make one or simply specify

the names using a c() command like so:

> barplot(rain, names = c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',

'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))

If our vector has a names attribute, the barplot() command can read the names directly. In the

following example you set the names() of the rain vector and then use the barplot() command:

> rain ; month

[1] 3 5 7 5 3 2 6 8 5 6 9 8
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

> names(rain) = month

> rain

Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

357532685698

> barplot(rain)

Now the bars are neatly labeled with the names taken from the data itself.

First, let’s make a data of a vector of months, a vector of the number of chickens and a vector of the number of eggs. That’s
random enough for this purpose.
# make some data
months <-rep(c("jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"), 2)
chickens <-c(1, 2, 3, 3, 3, 4, 5, 4, 3, 4, 2, 2)
eggs <-c(0, 8, 10, 13, 16, 20, 25, 20, 18, 16, 10, 8)
values <-c(chickens, eggs)
type <-c(rep("chickens", 12), rep("eggs", 12))
mydata <-data.frame(months, values)
If parts of the above code don’t make sense, take a look at my post on using the R functions seq (sequence), rep (repeat), and
cbind (column bind) HERE.
Now let’s load the ggplot package.

library(ggplot2)
We want to make a plot with the months as the x-axis and the number of chickens and eggs as the height of the bar. To do this,
we need to make sure we specify stat = “identity”. Here’s the basic code for this plot.
p <-ggplot(mydata, aes(months, values))
p +geom_bar()
Notice that you will get the error shown above, “stat_count() must not be used with a y aesthetic.” We forgot to specify that we
want the height of the column to equal the value for that month. So let’s do it again.

p <-ggplot(mydata, aes(months, values))


p +geom_bar(stat = "identity")

This time we get a plot, but it looks fairly ugly, and the months are out of order. In fact the months are in alphabetical order so
let’s fix that first. If we investigate the months, we will see they have ordered levels.

mydata$months
#[1] jan feb mar apr may jun jul aug sep oct nov dec jan feb mar apr may
#[18] jun jul aug sep oct nov dec
#Levels: apr aug dec feb jan jul jun mar may nov oct sep
We can fix the order of this category by changing the factor. Here’s some code that will fix our problem.

mydata$months <-factor(mydata$months,
levels = c("jan", "feb", "mar", "apr", "may", "jun",
"jul", "aug", "sep", "oct", "nov", "dec"))
Now if we look at the levels again, we will see that they’re rearranged in the order that we want.

mydata$months
#[1] jan feb mar apr may jun jul aug sep oct nov dec jan feb mar apr may
#[18] jun jul aug sep oct nov dec
#Levels: jan feb mar apr may jun jul aug sep oct nov dec
Okay, let’s make our plot again, this time with the months in the correct order.

p <-ggplot(mydata, aes(months, values))


p +geom_bar(stat = "identity", aes(fill = type))
About Mathematica
The article says Mathematica was "originally developed by Stephen Wolfram". It is true that Stephen
Wolfram initiated the project to develop Mathematica, but the first released version of Mathematica
included contributions from a team of 8 developers: Stephen Wolfram, Daniel Grayson, Roman
Maeder, Jerry Keiper, Henry Cejtin, Steve Omohundro and Dave Ballman collaborated on the
Mathematica kernel (the computational core of Mathematica), while Theo Gray designed and
implemented the front end that established the distinctive interaction style of Mathematica and
provided the ability to create "live" interactive, executable mathematical notebooks. Some of these
people were involved in the development of Mathematica for 2 years prior to its debut as a
commercial product, so the expresssion "originally developed by" would surely apply to them as
much as to Wolfram. My authority for this is personal recollection: I was one of the earliest
employees of Wolfram Research, and I personally knew 7 of the 8 original developers (all but
Omohundro). (I wasn't one of the developers myself, but I was one of the first 10 or so people hired
when the company was being organized in preparation for the initial commercial release.) In the
interest of historical accuracy and fairness, I suggest that the article text at least be amended to read
"originally developed by a team assembled and led by Stephen Wolfram". I realize that doesn't flow
as smoothly as the current wording; maybe someone else can find a better way to say it. I will wait a
day or two for comment before proceeding with the edit. --Logician1989 06:23, 16 February 2006
(UTC)

Use a CAS as a calculator

When using any computer algebra system as a calculator, it is important to understand

that it is a bit difficult to translate mathematical writing directly to the computer. Humans

instinctively adapt to ambiguity, but software is less flexible. For example, mathematicians

use parentheses for grouping, as in (x−1)(x+ 2). But they also use parentheses to indicate

ordered pairs, like (1, 3), and to denote functions f(x).

Another ambiguity that is perhaps a bit more subtle occurs with “equals.” Humans

have little trouble understanding that sometimes we intend equals to assign values, as with

“let x = 2.” At other times, we mean to assert equality; a circle is the set of points (x, y)

such that x

2+y

2 = 1.

Each computer algebra system addresses the job of translating mathematical syntax into

unambiguous “computer syntax” in its own way. To a first-time software user this can feel

somewhat unintuitive, even quirky, but mastering the language of your favorite CAS is an

important part of using it effectively.


Mathematica use as a calculator
Since Mathematica notebooks are used for text as well as calculations, you will almost

immediately notice one idiosyncrasy when you try to use Mathematics as a calculator. The

ENTER key does not run a calculation (it ends a paragraph or makes it possible to enter

multi-line computations). To use Mathematica as a calculator, type the expression you wish

to evaluate and press SHIFT+ENTER. (The special ENTER key on the lower-right corner

of the keypad of an extended keyboard also works.)

Example . We add 2 and 2 to get . . . 4.

In[1]:= 2 + 2

Out[1]= 4

Note: Mathematica assigns line numbers to the input and output, e.g., the “In[1]:=” and

“Out[1]=” above. You do not type them yourself.

To multiply two numbers, type the numbers with a space between them. Use a caret (^) for
exponentiation. Notice that Mathematica can handle very large numbers easily, even numbers with
hundreds of digits.

Use a CAS as a calculator


Example . A product, and the value of 3100

In[2]:= 1024 59049

Out[2]= 60466176

In[3]:= 3^100

Out[3]= 515377520732011331036461129765621272702107522001

The values of important mathematical constants (such as π, e, and i) are stored in

Mathematica. To distinguish them from variables you might create yourself, Mathematica’s

internal constants are capitalized (Pi, E, I, etc.).

The built-in constants are handled algebraically, but we can request the numerical value

of an expression with the N function.

Mathematica also performs matrix calculations. Matrices are entered with braces and

are stored as lists of lists.

Example 12.7. We define two matrices:


M= 1 2 3

4 5 6

7 8 9

and N =0 1 0

001

100

In[13]:= m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

In[14]:= n = {{0, 1, 0}, {0, 0, 1}, {1, 0, 0}};

Note. The ; (semicolon) symbol is used to separate commands, allowing you to perform

more than one calculation on a line. If you end a command with a semicolon, the output

will not be displayed.

12.8. We add and multiply the matrices.

In[15]:= m + n

Out[15]= {{1, 3, 3}, {4, 5, 7}, {8, 8, 9}}

In[16]:= m . n

Out[16]= {{3, 1, 2}, {6, 4, 5}, {9, 7, 8}}

Functions in Mathematica
The operator N, which we saw earlier, is actually a function. Mathematica contains

many such built-in functions, and you can usually guess the names of common functions.

For instance, Sin[x] computes sin x.

Note. In Mathematica, every built-in function name begins with a capital letter. Arguments

of functions follow in square brackets.

Example . We calculate sin(π/2) and the binomial coefficient(7/2)

In[1]:= Sin[Pi/2]

Out[1]= 1

In[2]:= Binomial[7,2]

Out[2]= 21

Example. The command FactorInteger determines the prime factorization of an

integer. Here we find the prime factorization of the number 60466176.

In[3]:= FactorInteger[60466176]

Out[3]= {{2, 10}, {3, 10}}


The output tells us that 60466176 = 2^10.3^10

Example: The function Prime[n] gives the nth prime number. Using this function,

we construct a table of the first 100 prime numbers.

In[4]:= Table[Prime[n], {n, 1, 100}]

Out[4]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,

> 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,

> 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,

> 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,

> 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311,

> 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,

> 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,

> 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541}

Mathematica can evaluate functions both arithmetically and symbolically.

Example:

In[5]:= Sum[i^2, {i, 1, 10}]

Out[5]= 385

In[6]:= Sum[i^2, {i, 1, n}]

Out[6]= 1/6[ n (1 + n) (1 + 2 n)]

You can define your own functions. To create a function f(x), write f[x_] := followed

by the definition of f.

Example 1. We define a function f(x) = x^3 + sin x.

In[7]:= f[x_] := x^3 + Sin[x]

In[8]:= f[Pi/2]

We can differentiate and integrate our function.

In[9]:= D[f[x],x]

In[10]:= Integrate[f[x],x]

In[11]:= Integrate[f[x], {x, 0, Pi}]

Note. Mathematica does not supply an additive constant (+C) for indefinite integrals.

You can define functions recursively (in terms of previous values), as with the function

below. Notice the use of = for the assignment of initial values in contrast with := for the
definition of the iteration.

Example 1. We define the Fibonacci sequence.

In[1]:= f[0] = 1;

In[2]:= f[1] = 1;

In[3]:= f[n_] := f[n] = f[n-2] + f[n-1]

In[4]:= Table[f[n], {n, 0, 10}]

You may wonder at the construction in line 3. In Mathematica, := is the “delayed”

assignment operator as opposed to =, which does immediate assignment. When we use

delayed assignment, Mathematica will wait to fully evaluate an expression.

To explore this idea, consider the first time we ask Mathematica for the value f[3]. Since

we used :=, the value of 3 will be substituted for n on the right side of the definition, which

will evaluate to the expression f[3] = f[1] + f[2] (which is actually an assignment itself).

Mathematica already knows the values of f[1] and f[2] and consequently sets f[3] = 2

How to compute functions


The steps performed by Mathematica to do the evaluation of f[3] are:

1. f[3] = f[3-2] + f[3-1] (this is the right-hand side of the :=, with 3 substituted

for n in all places)

2. f[3] = f[1] + f[2]

3. f[3] = 1 + 1 (from previously defined values)

4. f[3] = 2 (this is ready to perform immediate assignment)

5. 2 (the return value of the = assignment).

Of course, all we see is the final result:

In[5]:= f[3]

The consequence of doing the computation this way is that Mathematica now knows

permanently that f[3] has the value 2 and will never have to evaluate it again (say, when

we ask for f[4] or any other value). This becomes important for larger values, like f[100],

which would evaluate too slowly if we created the function less carefully.

Graphs in Mathematica
Mathematica offers many graphing options. We show a few examples here. You can

create graphs of functions using Mathematica’s Plot command.


Example : A graph of the function y = sin x, for 0 ≤ x ≤ 2π.

In[1]:= Plot[Sin[x], {x, 0, 2 Pi}]

graph of three lines, y = 4x + 1, y = −x + 4, and y = 9x − 8, for

0 ≤ x ≤ 2.

In[1]:= f[x_] := 4 x + 1;

In[2]:= g[x_] := -x + 4;

In[3]:= h[x_] := 9 x - 8;

In[4]:= Plot[{f[x], g[x], h[x]}, {x, 0, 2}]

Example 1. A graph of the surface z = e^−(x^2+y^2)

, for −2 ≤ x, y ≤ 2.

In[1]:= Plot3D[E^(-(x^2 + y^2)), {x, -2, 2}, {y, -2, 2}]

Example: We plot a sphere using parametric equations.

In[1]:= ParametricPlot3D[{Sin[phi] Cos[theta],Sin[phi] Sin[theta], Cos[phi]},{phi, 0, Pi}, {theta, 0, 2 Pi}]

You might also like