Professional Documents
Culture Documents
R Project 1
R Project 1
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.
> 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
>
> a= -1
> b= 2
> n=6
>
>
>
> delta.x
[1] 0.5
>
> #ii)
>
>
> #set X1 = -1
> x.i[1]=-1
>
>
>
+ x.i[i+1]= x.i[i]+ dx
+}
> x.i
>
>
> n=4
>
>
> foo
[1] 1 2 3 4
>
> foo.squared = rep(0,n)
>
+ foo.squared[i] = foo[i]^2
+}
>
> #iii)
>
> #x1 = 1
> #x{x2,x3,x4,x5,x6,7}
> #x.i=x(x1,x)
>
>
>
> # 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
> #Problem B:
>
> n=6
>
> # for loop to take left and right values and calculate the midpoint for each seperate intervals
>
+}
> 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
>
> #Problem C:
>
> #i)start by writing a program that avaluates the function y= f(x) for x
>
> n= (2+1)/.03
>
> n = round(n)
>
> f = function(x.m){
+ for (i in 1:n){
+ y2[i]= (-2*x.m[i]+4)
+}
+ return (y2)
+}
>
>
> x = seq(-1,2,.03)
> x.n
x.l x.h
+ 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)
>
> y4 = (y3*0.03)
> #display y4
> y4
[1] 9.09
>
Problem C (part ii)
7
0
-1 0 1 2
Area = (3*6)/2
Area = 18
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).
>
> a= -1
> b=1
> n=6
>
>
> x= 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
+ 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
> # We want to define our sub.mid by combining the sub.low and sub.high using cbind.
>
> # now use "for" to loop sub.low and sub.high to get the midpoint values
+ xi.star[i]=(sub.mid[i,1] + sub.mid[i,2])/2
+}
>
> xi.star
>
> # 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.e3
[1] 0.7107411
Problem D: graph of f(x)= x*e^x