0% found this document useful (0 votes)
3 views

Polynomial_plot_Codes

The document provides R code examples for plotting polynomial functions of degrees 2, 3, and 4 using the plot() and curve() functions. It includes specific polynomial equations with defined coefficients and ranges for the variable x, demonstrating how to visualize these functions graphically. Each section outlines the setup for the plots, including the sequence of x values and the corresponding y values calculated from the polynomial equations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Polynomial_plot_Codes

The document provides R code examples for plotting polynomial functions of degrees 2, 3, and 4 using the plot() and curve() functions. It includes specific polynomial equations with defined coefficients and ranges for the variable x, demonstrating how to visualize these functions graphically. Each section outlines the setup for the plots, including the sequence of x values and the corresponding y values calculated from the polynomial equations.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

## Plotting the graphs of polynomial of degree 2,3 and 4

## Polynomial of degree 2 ( y = a + b x + c x )
2

## Plot y = a + b*x + c*(x^2) ; 0 < x < 3.0 for a=2.0, b=3, c=1.0
## R Code, using plot() function we execute as
x <- seq(0, 3.0, by=0.01)
y <- 2.0 - 3*x + 1.0*(x^2)
plot(x, y, type="l", lwd=2, col="blue")
2.0
1.5
1.0
y

0.5
0.0

0.0 0.5 1.0 1.5 2.0 2.5 3.0

## We can use curve() function to draw the same graph as


curve(2.0 - 3*x + 1.0*(x^2),from=0.0, to=3.0, lwd=2, col="blue")
## ----------------------------------------------------------
## Plot y = a + b*x + c*(x^2) ; -2 < x < 2 for a=2.0 , b=0.5, c=1.5
x <- seq(-2, 2, by=0.01)
y <- 2.0 + 0.5*x + 1.5*(x^2)
plot(x, y, type="l", lwd=2, col="red")
9
8
7
6
y

5
4
3
2

-2 -1 0 1 2

x
## Using the curve() function
curve(2.0 + 0.5*x + 1.5*(x^2),from=-2.0, to=2.0, lwd=2, col="red")
## ----------------------------------------------------------
## Plot y = a + b*x + c*(x^2) ; -2.0 <x <2.0 for a=-10.0, b=-1.5, c=5.0
x <- seq(-2.0, 2.0, by=0.01)
y <- -10.0 - 1.5*x + 5.0*{x^2}
plot(x, y, type="l", lwd=2, col="blue")
10
5
y

0
-5
-10

-2 -1 0 1 2

## ***********************************************************
## Polynomial of degree 3 ( y = a + b x + c x + d x )
2 3

## Plot y = a + b*x + c*(x^2) +d*(x^3) ; -2 < x < 2 for a=2.0, b=0.5,


c=1.5 and d=2.0
x <- seq(-2, 2, by=0.01)
y <- 2.0 + 0.5*x + 1.5*{x^2}+ 2.0*(x^3)
plot(x, y, type="l", lwd=2, col="red")
25
20
15
10
y

5
0
-5
-10

-2 -1 0 1 2

x
## ----------------------------------------------------------
## Plot y = a + b*x + c*(x^2) +d*(x^3) ; -2 < x < 2 for a=-10.0, b=-
1.5, c=5.0 and d=2.0
x <- seq(-2.0, 2.0, by=0.01)
y <- -10.0 - 1.5*x + 5.0*{x^2}+ 2.0*(x^3)
plot(x, y, type="l", lwd=2, col="blue")

20
15
10
y

5
0
-5
-10

-2 -1 0 1 2

## ----------------------------------------------------------
## Plot y = a + b*x + c*(x^2) +d*(x^3) ; -5 < x < 5 for a=4.0, b=3.0,
c=5.0 and d=2.0
x <- seq(-5.0, 5.0, by=0.01)
y <- 4.0 + 3.0*x + 5.0*{x^2}+ 2.0*(x^3)
plot(x, y, type="l", lwd=2, col="blue")
400
300
200
y

100
0
-100

-4 -2 0 2 4

x
## ***********************************************************
## Polynomial of degree 4 ( y = a + b x + c x + d x + e x )
2 3 4

## Plot y = a + b*x + c*(x^2) +d*(x^3)+e*(x^4) ; -2 < x < 2


## for a=-20.0, b=0.5, c=1.5, d=-2.0 and e= 2.0
x <- seq(-2, 2, by=0.01)
y <- -20.0 + 0.5*x + 1.5*{x^2}- 2.0*(x^3)+2*(x^4)
plot(x, y, type="l", lwd=2, col="red")
30
20
10
y

0
-10
-20

-2 -1 0 1 2

## ----------------------------------------------------------
## Plot y = a + b*x + c*(x^2) +d*(x^3)+e*(x^4) ; -2 < x < 2
## for a=-12.0, b=-4.0, c=3.0, d=-2.0 and e=3.0
x <- seq(-2.0, 2.0, by=0.01)
y <- -12.0 -4.0*x + 3.0*{x^2}- 2.0*(x^3)+3*(x^4)
plot(x, y, type="l", lwd=2, col="blue")
60
40
y

20
0

-2 -1 0 1 2

x
## ***********************************************************
Ex: Plot y = x^3-6*x+11*x-6 ; 0 < x < 4
x <- seq(0, 4, by=0.01)
y <- -6 + 11*x -6*{x^2}+ 1.0*(x^3)
plot(x, y, type="l", lwd=2, col="red")
abline(h=0) # adds horizontal line
6
4
2
0
y

-2
-4
-6

0 1 2 3 4

## ----------------------------------------------------------
Ex: Plot y = x^4-10*(x^3) +35*(x^2)-50*x+24 ; 0.5 < x < 4.5
x <- seq(0.5, 4.5, by=0.01)
y <- 24.0 -50.0*x + 35.0*{x^2}- 10.0*(x^3)+1.0*(x^4)
plot(x, y, type="l", lwd=2, col="blue")
6
4
y

2
0

1 2 3 4

You might also like