You are on page 1of 13

Richard Chear

Calc2

R Project 1 8/7/2017

#Problem A:

> #i) Over [-1,2] we like to create increment of 0.3 for the function f(x)=-2*x+4.

> #create a domain x.dom[-1,2]

> #and seperate it using increment of 0.3

> x.dom

[1] -1.00 -0.97 -0.94 -0.91 -0.88 -0.85 -0.82 -0.79 -0.76 -0.73 -0.70 -0.67

[13] -0.64 -0.61 -0.58 -0.55 -0.52 -0.49 -0.46 -0.43 -0.40 -0.37 -0.34 -0.31

[25] -0.28 -0.25 -0.22 -0.19 -0.16 -0.13 -0.10 -0.07 -0.04 -0.01 0.02 0.05

[37] 0.08 0.11 0.14 0.17 0.20 0.23 0.26 0.29 0.32 0.35 0.38 0.41

[49] 0.44 0.47 0.50 0.53 0.56 0.59 0.62 0.65 0.68 0.71 0.74 0.77

[61] 0.80 0.83 0.86 0.89 0.92 0.95 0.98 1.01 1.04 1.07 1.10 1.13

[73] 1.16 1.19 1.22 1.25 1.28 1.31 1.34 1.37 1.40 1.43 1.46 1.49

[85] 1.52 1.55 1.58 1.61 1.64 1.67 1.70 1.73 1.76 1.79 1.82 1.85

[97] 1.88 1.91 1.94 1.97 2.00

>

> # create n=6 subintervals over [-1,2]

> a= -1

> b= 2

> n=6

>

> # calculate delta x (delta.x)

> delta.x = (b-a)/n

>

> #display delta x

>
> delta.x

[1] 0.5

>

> #ii)

>

>

> #set X1 = -1

> x.i[1]=-1

>

>

> # use for loop to define X2, X3, X4 values

>

> for (i in 1:n){

+ x.i[i+1]= x.i[i]+ dx

+}

> # display contents of x.i

> x.i

[1] -1.0 -0.5 0.0 0.5 1.0 1.5 2.0

>

>

> n=4

>

> #define vector and specify contents

> foo = c(1,2,3,4)

>

> #display contents of foo

> foo

[1] 1 2 3 4

>
> foo.squared = rep(0,n)

>

> for (i in 1:n){

+ foo.squared[i] = foo[i]^2

+}

>

> #iii)

>

> #x1 = 1

> #x{x2,x3,x4,x5,x6,7}

> #x.i=x(x1,x)

>

> #iv) Create sub.low and sub.high

>

> sub.low = x.i[1:6]

> sub.high = x.i[2:7]

>

> # v) Use cbind to create sub which binds sub.low and sub.high column-wise

>

> sub=cbind(sub.low,sub.high)

>

> sub

sub.low sub.high

[1,] -1.0 -0.5

[2,] -0.5 0.0

[3,] 0.0 0.5

[4,] 0.5 1.0

[5,] 1.0 1.5

[6,] 1.5 2.0


>

> #Problem B:

>

> #Define x.i star

> xi.star = rep (0,20)

> n=6

>

> # for loop to take left and right values and calculate the midpoint for each seperate intervals

>

> for ( i in 1:n){

+ xi.star[i] = (sub[i,1] +sub [i,2])/2

+}

> # listing all our midpoints, which we should have6

> xi.star

[1] -0.75 -0.25 0.25 0.75 1.25 1.75 0.00 0.00 0.00 0.00 0.00 0.00

[13] 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

>

> #Problem C:

>

> #i)start by writing a program that avaluates the function y= f(x) for x

> # Plot (x,y)

> # lets start by defining n

>

> n= (2+1)/.03

>

> n = round(n)

>

> # define function

> f = function(x.m){
+ for (i in 1:n){

+ y2[i]= (-2*x.m[i]+4)

+}

+ return (y2)

+}

>

>

> #specify domain

> x = seq(-1,2,.03)

> x.n = cbind (x.l,x.h)

> x.n

x.l x.h

[1,] -1.00 -0.97

[2,] -0.97 -0.94

[3,] -0.94 -0.91

[4,] -0.91 -0.88

[5,] -0.88 -0.85

[6,] -0.85 -0.82

[7,] -0.82 -0.79

[8,] -0.79 -0.76

[9,] -0.76 -0.73

[10,] -0.73 -0.70

[11,] -0.70 -0.67

[12,] -0.67 -0.64

[13,] -0.64 -0.61

[14,] -0.61 -0.58

[15,] -0.58 -0.55

[16,] -0.55 -0.52

[17,] -0.52 -0.49


[18,] -0.49 -0.46

[19,] -0.46 -0.43

[20,] -0.43 -0.40

[21,] -0.40 -0.37

[22,] -0.37 -0.34

[23,] -0.34 -0.31

[24,] -0.31 -0.28

[25,] -0.28 -0.25

[26,] -0.25 -0.22

[27,] -0.22 -0.19

[28,] -0.19 -0.16

[29,] -0.16 -0.13

[30,] -0.13 -0.10

[31,] -0.10 -0.07

[32,] -0.07 -0.04

[33,] -0.04 -0.01

[34,] -0.01 0.02

[35,] 0.02 0.05

[36,] 0.05 0.08

[37,] 0.08 0.11

[38,] 0.11 0.14

[39,] 0.14 0.17

[40,] 0.17 0.20

[41,] 0.20 0.23

[42,] 0.23 0.26

[43,] 0.26 0.29

[44,] 0.29 0.32

[45,] 0.32 0.35

[46,] 0.35 0.38


[47,] 0.38 0.41

[48,] 0.41 0.44

[49,] 0.44 0.47

[50,] 0.47 0.50

[51,] 0.50 0.53

[52,] 0.53 0.56

[53,] 0.56 0.59

[54,] 0.59 0.62

[55,] 0.62 0.65

[56,] 0.65 0.68

[57,] 0.68 0.71

[58,] 0.71 0.74

[59,] 0.74 0.77

[60,] 0.77 0.80

[61,] 0.80 0.83

[62,] 0.83 0.86

[63,] 0.86 0.89

[64,] 0.89 0.92

[65,] 0.92 0.95

[66,] 0.95 0.98

[67,] 0.98 1.01

[68,] 1.01 1.04

[69,] 1.04 1.07

[70,] 1.07 1.10

[71,] 1.10 1.13

[72,] 1.13 1.16

[73,] 1.16 1.19

[74,] 1.19 1.22

[75,] 1.22 1.25


[76,] 1.25 1.28

[77,] 1.28 1.31

[78,] 1.31 1.34

[79,] 1.34 1.37

[80,] 1.37 1.40

[81,] 1.40 1.43

[82,] 1.43 1.46

[83,] 1.46 1.49

[84,] 1.49 1.52

[85,] 1.52 1.55

[86,] 1.55 1.58

[87,] 1.58 1.61

[88,] 1.61 1.64

[89,] 1.64 1.67

[90,] 1.67 1.70

[91,] 1.70 1.73

[92,] 1.73 1.76

[93,] 1.76 1.79

[94,] 1.79 1.82

[95,] 1.82 1.85

[96,] 1.85 1.88

[97,] 1.88 1.91

[98,] 1.91 1.94

[99,] 1.94 1.97

[100,] 1.97 2.00

> for (i in 1:n){

+ x.m[i]=(x.n[i,1] + x.n[i,2])/2

+}

> x.m
[1] -0.985 -0.955 -0.925 -0.895 -0.865 -0.835 -0.805 -0.775 -0.745 -0.715

[11] -0.685 -0.655 -0.625 -0.595 -0.565 -0.535 -0.505 -0.475 -0.445 -0.415

[21] -0.385 -0.355 -0.325 -0.295 -0.265 -0.235 -0.205 -0.175 -0.145 -0.115

[31] -0.085 -0.055 -0.025 0.005 0.035 0.065 0.095 0.125 0.155 0.185

[41] 0.215 0.245 0.275 0.305 0.335 0.365 0.395 0.425 0.455 0.485

[51] 0.515 0.545 0.575 0.605 0.635 0.665 0.695 0.725 0.755 0.785

[61] 0.815 0.845 0.875 0.905 0.935 0.965 0.995 1.025 1.055 1.085

[71] 1.115 1.145 1.175 1.205 1.235 1.265 1.295 1.325 1.355 1.385

[81] 1.415 1.445 1.475 1.505 1.535 1.565 1.595 1.625 1.655 1.685

[91] 1.715 1.745 1.775 1.805 1.835 1.865 1.895 1.925 1.955 1.985

[101] 0.000

> y2 = f(x)

> y2

[1] 6.00 5.94 5.88 5.82 5.76 5.70 5.64 5.58 5.52 5.46 5.40 5.34 5.28 5.22 5.16

[16] 5.10 5.04 4.98 4.92 4.86 4.80 4.74 4.68 4.62 4.56 4.50 4.44 4.38 4.32 4.26

[31] 4.20 4.14 4.08 4.02 3.96 3.90 3.84 3.78 3.72 3.66 3.60 3.54 3.48 3.42 3.36

[46] 3.30 3.24 3.18 3.12 3.06 3.00 2.94 2.88 2.82 2.76 2.70 2.64 2.58 2.52 2.46

[61] 2.40 2.34 2.28 2.22 2.16 2.10 2.04 1.98 1.92 1.86 1.80 1.74 1.68 1.62 1.56

[76] 1.50 1.44 1.38 1.32 1.26 1.20 1.14 1.08 1.02 0.96 0.90 0.84 0.78 0.72 0.66

[91] 0.60 0.54 0.48 0.42 0.36 0.30 0.24 0.18 0.12 0.06 0.00

>

> plot(x,y2)

>

> y3 = sum (y2)

> y4 = (y3*0.03)

> #display y4

> y4

[1] 9.09

>
Problem C (part ii)
7

0
-1 0 1 2

X -1 -0.5 0 0.5 1.0 1.5 2.0


F(x) 6 5 4 3 2 1 0
Area= ½ B*H

Area = (3*6)/2

Area = 18

iii) Calculation by calculator for f(x) = -2x+4

Area= 18

iv) The calculations are exactly the same (as they should be)

> # Problem D:

>

> # Given f(x) = xe^x over the interval [-1,1] you will investigate and then write the R code that sets up
the numerical intgration ; up to the evaluation of f(xi.stars).

>

> # we were given the values


>

> a= -1

> b=1

> n=6

>

> # now we calculate delta.x

> delta.x = (b-a)/n

>

> # using the given values of n to define vectors

> x= rep(0,n+1)

>

> # y.e is going to represent our exponiential function f(x) = x*e^x

>

> y.e = rep (0,(n+1))

> y.e

[1] 0 0 0 0 0 0 0

>

> # To define the function we will be using the mis point values from xi.star to calculate the y values

> f = function (xi.star){

+ for ( i in 1:n){

+ y.e[i]= ((xi.star[i])*exp(xi.star[i]))

+}

+ return(y.e)

+}

>

> # x[1] = -1 to be used for the loop to be used for each subsequent x[i] by delta.x

> x[1] = -1

> for (i in 1:n){

+ x[i+1] = x[i]+ delta.x


+}

> # We want to define our sub.mid by combining the sub.low and sub.high using cbind.

> sub.low = x[1:n]

> sub.high = x[2:(n+1)]

> sub.mid = cbind (sub.low, sub.high)

>

> # now use "for" to loop sub.low and sub.high to get the midpoint values

> for (i in 1:n){

+ xi.star[i]=(sub.mid[i,1] + sub.mid[i,2])/2

+}

>

> xi.star

[1] -0.8333333 -0.5000000 -0.1666667 0.1666667 0.5000000 0.8333333

[7] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000

[13] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000

[19] 0.0000000 0.0000000

>

> # now let's compute the corresponding y vlaues for each x mid-point values with the function
f(xi.star)and should give you y.e

> y.e = f(xi.star)

>

> #now we compute the integration of the function y = x*e^x

>

> y.e2 = sum (y.e)

> y.e3 = (y.e2*deltax)

Error: object 'deltax' not found

> y.e3

[1] 0.7107411
Problem D: graph of f(x)= x*e^x

You might also like