Question

2. The roots of the quadratic $a x^2 + b x + c$ are given by...

2.  The roots of the quadratic $a x^2 + b x + c$ are given by

$$\frac{-b \pm \sqrt{b^2-4ac}}{2a}$$

If $b^2-4ac <0$, the quadratic has no real roots.

Write a function to calculate the real roots of a quadratic.  The function
should have 3 arguments, *a*, *b* and *c*.
If $b^2-4ac <0$, the function should print "quadratic has no real roots",
and then return(NULL).
Otherwise, the function should return a vector of length 2, those being
the real roots (which may be the same if  $b^2-4ac =0$).

Test your function using the quadratic $x ^2 - 3 x + 2$.



3.  Where $x_1, x_2, \ldots , x_n$ is a sample from a normal distribution
with unknown mean $\mu$ and unknown variance $\sigma^2$, the level
$100(1-\alpha)$% confidence interval for $\mu$ is given by

$$\bar x \pm t_{1-\alpha/2, n-1} \frac{s}{\sqrt n}$$

where $\bar x$ and $s$ are the sample mean and sample standard deviation
of the data, and $t_{1-\alpha/2, n-1}$ cuts off an area $1-\alpha/2$ to
its left under the $t$ curve with $n-1$ degrees of freedom.

Write a function which has two arguments, a vector of data $x$, and
alpha, which should have a default value of .05.
The function should return
a vector of length 2, which contains the endpoints of the confidence interval.

The percentiles of the t-distrubtion can be calculated as follows. Suppose
that you want the 97.5'th percentile of the t-distribution with 23 degrees
of freedom.  This can be calculated in R as

```{r}
qt(.975,23)
```

Test your function by calculating the 99% confidence interval using
the following data

```{r}
set.seed(87612345)
data=rnorm(25,mean=4.5,sd=.75)
```
You can check your calculation using

```{r}
t.test(data,conf.level=.99)
```

When putting your two endpoints together, you may find something similar to the 
following to be useful.

```{r}
1+c(-1,1)*.25
```

4.
The derivative of a function $f(x)$ can be approximated by the Newton's quotient

$$\frac{f(x+h) - f(x)}{h}$$

where $h$ is a small number.  Write a function to calculate the Newton's quotient
for $f(x) = exp(x)$.  The function should take two scalar arguments, $x$ and $h$.
Use a default value of $h=1.e-6$.
Test your function at the point $x=1$ using the default value of $h$, and compare
to the true value of the derivative $f'(1) = e^1$.

5.  A very useful feature in R is the ability to pass a function name as an argument.
Here is an example, where 2 is added to the value of a function, for three different
functions $exp(x)$, $log(x)$, and $sin(x)$, at selected points $x$.

```{r}
test=function(x,f){
 output=f(x)+2
 return(output)
  }

test(0,exp)
test(1,log)
test(0,sin)
test(pi/2,sin)
```

Modify your function from problem 4 so that you pass in the name of the function for which
you want to approximate the derivative.  Use the same default value for $h$, and approximate
the derivative of sin(x) at $x=\pi/4$, of $log(x)$ at $x=2$, and of $exp(x)$ at $x=1$.

6. 
Write a function which takes one argument $x$ of length 2, and returns the ordered
values of $x$.  That is, if $x_2<x_1$, your function should return $c(x_2,x_1)$, otherwise it
should return $x$.  (WRITE YOUR OWN FUNCTION. DO NOT USE THE BUILT IN FUNCTION ORDER)

Use your function to process a dataset with 2 columns as follows.  Iterate over the rows of the
data set, and if the element in the 2nd column of row *i* is less than the element in the first
column of row *i*, switch the order of the two entries in the row by making a suitable call to
the function you just wrote.

Test using the following data.

```{r}
set.seed(1128719)
data=matrix(rnorm(20),byrow=T,ncol=2)
```
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The root of is given by

The R function for roots of quadratic equation is given below :

roots<-function(a,b,c){

if(b*b-4*a*c < 0){

print(quadratic has no real roots)

return(NULL)

}

else{

b<-c( (-b-sqrt(b*b-4*a*c))/(4*a),(-b-sqrt(b*b+4*a*c))/(4*a) )

return(c)

}

}

Test for

roots(1,-3,2)

Add a comment
Know the answer?
Add Answer to:
2. The roots of the quadratic $a x^2 + b x + c$ are given by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠...

    C++ The roots of the quadratic equation ax² + bx + c = 0, a ≠ 0 are given by the following formula: In this formula, the term b² - 4ac is called the discriminant. If b² - 4ac = 0, then the equation has a single (repeated) root. If b² - 4ac > 0, the equation has two real roots. If b² - 4ac < 0, the equation has two complex roots. Instructions Write a program that prompts the...

  • The roots of the quadratic equation ax2 + bx + c = 0, a following formula:...

    The roots of the quadratic equation ax2 + bx + c = 0, a following formula: 0 are given by the In this formula, the term i2 - 4ac is called the discriminant. If b4ac 0 then the equation has a single (repeated) root. If -4ac > 0, th equation complex roots. Write a program that prompts the user to input the value of a (the coefficient of ), b (the coefficient of x), and c (the n has two...

  • 3. Produce the function quadratic(a,b,c) where a, b, and c are the coeffi- cients of the...

    3. Produce the function quadratic(a,b,c) where a, b, and c are the coeffi- cients of the quadratic ax2 + bx +c. The function should return: 1. a list of the two roots of ax2 + bx +c= 0, when two roots exist 2. the first root is the smaller of the two 3. if ax2 + bx +c=0 has no real roots, then return None. 4. if the quadratic has one real root only, then return a list with a...

  • CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation...

    CSC 211 - Lab-2 Write a C++ program to find the roots of a quadratic equation ax +bx+c=0. The roots are given by the formula, x=-b I56²-4ac 2a x = -b+ √b²-4ac 2. x2 = -b-√6²-4ac 2a Instructions: 1. Type, compile, and run the program in an online C++ compiler. 2. Open a word document and copy the following items onto it: a. The source code b. Screenshot of your program's result 3. Save the word document as lastnameFirstname_Lab2.docx (for...

  • Write a C program, to solve the quadratic equation a x2 + b x + c...

    Write a C program, to solve the quadratic equation a x2 + b x + c = 0 of given coefficients a, b and c. When running the program, it prompts for the input of coefficients a,b,c as floating numbers. After inputting three floating numbers, it computes and prints out the solutions, then prompts for another round of input. Your program will quit when getting input 0,0,0. Your program should handle four situations: (1) a=0, not a quadratic equation; (2)...

  • Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c...

    Use Python Programming. Design a class named Quadratic Equation for a quadratic equation ax + bx+c 0. The class contains: • The data fields a, b, and c that represent three coefficients. . An initializer for the arguments for a, b, and c. • Three getter methods for a, b, and c. • A method named get Discriminant() that returns the discriminant, which is b- 4ac The methods named getRoot 1() and getRoot 2() for returning the two roots of...

  • A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the...

    A quadratic equation is generally represented as, ax^2 + bx + c The root(s) of the above quadratic equation is computed using the following formula, root1 = (-b + sqrt(D))/ 2a root2 = (-b - sqrt(D))/2a Where D is the discriminant of the quadratic equation and is computed as, D = b^2 - 4ac Given the value of D, the roots of a quadratic equation can be categorized as follows, D > 0 : Two distinct real roots D =...

  • The following procedure can be used to determine the roots of a cubic equation a_3x^3 +...

    The following procedure can be used to determine the roots of a cubic equation a_3x^3 + a_2x^2 + a_1x + a_0 = 0: Set: A =a_2/a_3, B = a_1/a_3, and C = a_0/a_3 Calculate: D = Q^3 + R^2 where Q = (3B - A^2)/9 and R = (9AB - 27C - 2A^3)/54. If D > 0, the equation has complex roots. It D = 0, all roots are real and at least two are equal. The roots are given...

  • 2. Suppose Y ~ Exp(a), which has pdf f(y)-1 exp(-y/a). (a) Use the following R code to generate data from the model Yi...

    2. Suppose Y ~ Exp(a), which has pdf f(y)-1 exp(-y/a). (a) Use the following R code to generate data from the model Yi ~ Exp(0.05/Xi), and provide the scatterplot of Y against X set.seed(123) n <- 500 <-rnorm (n, x 3, 1) Y <- rexp(n, X) (b) Fit the model Yi-Ao + Ax, + ε¡ using the lm function in R and provide a plot of the best fit line on the scatterplot of Y vs X, and the residual...

  • Using matlab and if/else statement please! Write a function that determines the real roots of a...

    Using matlab and if/else statement please! Write a function that determines the real roots of a quadratic equation ax2 + bx + c = 0. To calculate the roots of the equation, the function calculates the discriminant D, given by: D = b2-4ac If D> 0, the code should display "The equation has two roots" and print the values on a new line. If D 0, the code should display "The equation has one root.", and print the value on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT