Fitting of polynomials & exponential curves. Experiment :1 Fitting the polynomials upto degree 3 to the given data Experiment :2 Fitting the polynomial to an exponential curve of the form y = aebx

 #LAB-5

#Program 1
x=1:6
y=c(5,70,150,380,550,740)
fitsline=lm(y~x)
fitsline
plot(x,y,pch=19,ylim=c(0,800))
lines(x,fitted(fitsline),col='red',type='b')

#b(Quardic_line)
x=1:6
y=c(5,70,150,380,550,740)
fitquaratic=lm(y~poly(x,2,raw=TRUE))
fitquaratic
plot(x,y,pch=19,ylim=c(0,800))
lines(x,fitted(fitquaratic),col='blue',type='b')

#Cubic
x=1:6
y=c(5,70,150,380,550,740)
fitcubic=lm(y~poly(x,3,raw=TRUE))
fitcubic
plot(x,y,pch=19,ylim=c(0,800))
lines(x,fitted(fitcubic),col='green',type='b')


#Program 2:
x=c(32,64,96,118,126,144,152.5,158)
y=c(99.5,104.8,108.5,100,86,64,35.3,15)
fitstline=lm(y~x)
fitstline
plot(x,y,pch=19,ylim=c(0,120))
lines(x,fitted(fitstline),col='red',type='b')

#Quadratic

x=c(32,64,96,118,126,144,152.5,158)
y=c(99.5,104.8,108.5,100,86,64,35.3,15)
fitquadratic=lm(y~poly(x,2,raw=TRUE))
fitquadratic
plot(x,y,pch=42,ylim=c(0,120))
lines(x,fitted(fitquadratic),col='blue',type='b')

#Cubic
x=c(32,64,96,118,126,144,152.5,158)
y=c(99.5,104.8,108.5,100,86,64,35.3,15)
fitcubic=lm(y~poly(x,3,raw=TRUE))
fitcubic
plot(x,y,pch=42,ylim=c(0,120))
lines(x,fitted(fitcubic),col='green',type='b')



#Program-4
x = c(1,3,5,7)
y = c(5,20,100,400)
plot(x,y,col='red',pch=19)
r = lm(log(y)~x)
a = exp(r$coeff[1])
b = r$coeff[2]
print(a)
print(b)
fit = a*exp(b*x)
lines(x,fit,col = 'green',type = "b",pch=15)


#Student Activity
x=c(2,4,6,8,11,12)
y=c(13,36,49,58,72,88)
fitsline=lm(y~x)
fitsline
plot(x,y,pch=19,ylim=c(0,100))
lines(x,fitted(fitsline),col='black',type='b')

#b(Quardic_line)
x=c(2,4,6,8,10,12)
y=c(6,36,49,58,72,88)
fitquaratic=lm(y~poly(x,2,raw=TRUE))
fitquaratic
plot(x,y,pch=19,ylim=c(0,100))
lines(x,fitted(fitquaratic),col='black',type='b')

#Cubic
x=c(2,4,6,8,11,12)
y=c(13,36,49,58,72,88)
fitcubic=lm(y~poly(x,3,raw=TRUE))
fitcubic
plot(x,y,pch=19,ylim=c(0,100))
lines(x,fitted(fitcubic),col='black',type='b')

#Program exponentional
x = c(2,4,6,8,10,12)
y = c(6,14,25,38,55,77)
plot(x,y,col='red',pch=19)
r = lm(log(y)~x)
a = exp(r$coeff[1])
b = r$coeff[2]
print(a)
print(b)
fit = a*exp(b*x)
lines(x,fit,col = 'green',type = "b",pch=15)

Comments

Popular posts from this blog

Probability distributions using R Experiment :1 Binomial Distribution Experiment :2 Poisson Distribution Experiment :3 Normal Distribution

Lines of Regression, Angle between two lines of regression and estimated values of variables. Experiment :1 Finding (a) Regression line of y on x and (b) Regression line of x on y. Experiment :2 Predicting y value for a given x. Experiment :3 Angle between two lines of regression