M3

 Question 1.1:

Find the root of the equation π‘₯

3 − 3π‘₯ − 5 = 0 on the interval [2, 3] using bisection method

correct to three decimal places.

// Scilab code for Bisection method

clc;

clear;

close;

deff('y = f(x)' , 'y = x^3-3*x-5') ;

x1 = 2, x2 = 3;

e = 0.0001 ;

c = 0 ;

printf('Successive approximations \n \t x1 \t \t x2 \t \t m \t

\t f(m)\n');

while abs(x1-x2)>e

 m = (x1+x2)/2 ;

 printf('\t%f\t%f\t%f\t%f\n', x1, x2, m, f(m));

 if f(m)*f(x1) > 0

 x1 = m ;

 else

 x2 = m ;

 end

 c = c+1 ; // to count number of iterations

end

printf('Solution of equation after %i iteration is %8.4f', c,

m)




Question 2.1. Use Newton-Raphson method to find a root of 𝑓(π‘₯) = π‘₯

3 + 2π‘₯

2 + 0.4 = 0

which lies near π‘₯0 = −2, with a tolerance of 0.01%.

// Scilab code for Newton-Raphson method

clc;

clear;

close

deff('y=f(x)','y=x^3+2*x^2-0.4');

deff('y1=f1(x)','y1=3*x^2+4*x');

x0=-2;

e=0.0001;

c=0; n=1;

printf('Successive iterations\n\tx0\t\tf(x0)\t\tf1(x0)\n');

while n==1

 x2=x0;

 x1=x0-(f(x0)/f1(x0));

 x0=x1;

 printf('\t%f\t%f\t%f\n', x2, f(x1), f1(x1) )

 c=c+1;

 if abs(f(x0))<e then

 break;

 end

end

printf('The root of %i iteration is:%8.4f', c, x0);





Exp 3

//Scilab Code for fitting a straight line to given set of data points (x, y)

clc;

n = input('Enter the no. of pairs of values (x, y):')

disp('Enter the values of x:')

for i=1:n

 x(i)=input(' ')

end

disp('Enter the corresponding values of y')

for i=1:n

 y(i)=input(' ')

end

sumx=0; sumx2=0; sumy=0; sumxy=0

for i=1:n

sumx=sumx+x(i);

sumx2=sumx2+x(i)*x(i);

sumy=sumy+y(i);

sumxy=sumxy+x(i)*y(i);

end

A=[sumx n; sumx2 sumx];

B=[sumy; sumxy];

C=inv(A)*B ;

printf('The line of best fit is y =(%g)x+(%g)', C(1,1),C(2,1))




Question 4.1. Evaluate ∫ √π‘π‘œπ‘ (π‘₯)

πœ‹/2

0

𝑑π‘₯ by using Trapezoidal rule into 6 sub-intervals.

//Scilab Code for Trapezoidal Rule

clc;

clear;

close;

deff('y=f(x)','y=sqrt(cos(x))')

a=input ("Enter Lower Limit: ")

b=input ("Enter Upper Limit: ")

n=input ("Enter number of sub-intervals: ")

h=(b-a)/n

add1=0

add2=0

for i=0 : n

 x=a + i * h

 y=f(x)

 disp([x y])

 if(i==0)|(i==n)

 add1=add1+y

 else

 add2=add2+y

 end

end

I=(h/2)*(add1+2*add2)

disp("Integration by Trapezoidal Rule is:", I)







Question 5.1. Evaluate ∫ 𝑒

−π‘₯

1 2

0

𝑑π‘₯ by using Simpson’s 1/3 rule with 6 sub-intervals.

//SciLab code for Simpson's (1/3) Rule

clc;

clear;

close;

deff('y=f(x)','y=exp(-x^2)')

a=input("Enter Lower Limit: ")

b=input("Enter Upper Limit: ")

n=input("Enter number of sub-intervals: ")

h=(b-a)/n

add1=0

add2=0

add3=0

for i=0:n

 x=a+i*h

 y=f(x)

 disp([x y])

 if (i==0)|(i==n) then

 add1=add1+y

 else if (modulo(i,2)==0) then

 add2=add2+y

 else

 add3=add3+y

 end

end

end

I=(h/3)*(add1+2*add2+4*add3)

disp("Integration by Simpsons (1/3) Rule is:", I)









Question 6.1. Using Taylor's series method, solve the differential equation

𝑑𝑦

𝑑π‘₯

= π‘₯

2 + 𝑦, given 𝑦(0) = 1 for π‘₯ = 0.4.

//Scilab code for Taylor's series method

clear ;

close ;

clc ;

deff('F = f(x,y)','F = x^2+y')

deff('D2Y = d2y(x,y)','D2Y = 2*x + f(x,y)');

deff('D3Y = d3y(x,y)','D3Y = 2 + d2y(x,y)’);

deff('D4Y = d4y(x,y)','D4Y = d3y(x,y)’);

deff('Y = y(x)','Y = 1 + f(0,1)*x + d2y(0,1)*x^2/2

+ d3y(0,1)*x^3/6+d4y(0,1)*x^4/24');

printf('y(0.4) = %8.5g ',y(0.4))








Question 6.2. Given

𝑑𝑦

𝑑π‘₯

= 2𝑦 + 3𝑒

π‘₯

, 𝑦(0) = 0, find 𝑦(0.2) using Taylor series method

//Scilab code for Taylor's series method

clear ;

close ;

clc ;

deff('F = f(x,y)','F = 2*y+3*exp(x)')

deff('D2Y = d2y(x,y)','D2Y = 2*f(x,y)+3*exp(x)');

deff('D3Y = d3y(x,y)','D3Y = 2*d2y(x,y)+3*exp(x)');

deff('D4Y = d4y(x,y)','D4Y = 2*d3y(x,y)+3*exp(x)');

deff('Y = y(x)','Y = 0 + f(0,0)*x + d2y(0,0)*x^2/2 +

d3y(0,0)*x^3/6+d4y(0,0)*x^4/24');

printf('y(0.2) = %8.5g ',y(0.2))





Question 7.1. Find 𝑦(2.5) using Runge-Kutta 4

th order formula given that

𝑦

′ =

π‘₯+𝑦

π‘₯

, 𝑦(2) = 2. Take β„Ž = 0.1.

// Scilab code for Runge Kutta 4

th order

clc;

close;

clear;

deff('g=f(x,y)','g = (x+y)/x')

xo=input("Enter initial value of xo: ")

yo=input("Enter the value of yo: ")

h=input("Enter value of h: ")

xn=input("Enter Final value of xn: ")

n=(xn-xo)/h

for i=1:n

k1=h*f(xo,yo)

k2=h*f(xo+(h/2),yo+(k1/2))

k3=h*f(xo+(h/2),yo+(k2/2))

k4=h*f(xo+h,yo+k3)

y1=yo+(1/6)*(k1+2*k2+2*k3+k4)

xo=xo+h

disp([xo y1])

yo=y1

end




Question 8.1.

The contents of three urns are: 1 white, 2 red, 3 green balls; 2 white, 1 red, 1 green ball and

4 white, 5 red, 3 green balls. Two balls are drawn from an urn chosen at random. These are

found to be one white and one green. Find the probability that the balls so drawn came

from the third urn

clear ;

close ;

clc ;

Let E1 = Urn I is chosen

Let E2 = Urn II is chosen

Let E3 = Urn III is chosen

Let A = Two balls are drawn at random & white and red.

p(1) = p(2) = p(3) = 1/3 // p(Selection of urn)

p(4) = 1/5; //p(1)= p(A/E1)=p(White and red from Urn I)

p(5) = 1/3; //p(1)= p(A/E2)=p(White and red from Urn II)

p(6) = 2/11; //p(1)= p(A/E3)=p(White and red from Urn III)

Prob = p(3)*p(6)/(p(1)*p(4)+p(2)*p(5)+p(3)*p(6));

disp(Prob, 'Probability that the balls drawn from the third

urn:');








clear ;

close ;

clc ;

mu=4300;

sig=750;

X=2500:0.001:4200;

n=(4200-2500)/0.001;

z=(X-mu)./sig;

h=z(2)-z(1);

f=((1/sqrt(2*%pi))*exp(-(z.^2)/2));

Value=h*(sum(f)-(f(1)/2)-(f(n+1)/2))

disp(Value, Probability that between 2500 and 4200 acres will

be burned)

plot(X,f)






Question 10.1. Calculate the Karl Pearson's correlation coefficient for the following

paired data.

Price

10 20 30 40 50 60 70

Supply (Units) 8 6 14 16 10 20 24

//Scilab code for Correlation coefficient by Karl Pearson method

clear ;

close ;

clc ;

X=[10 20 30 40 50 60 70]

Y=[8 6 14 16 10 20 24]

N=7;

Sx=0;Sy=0;Sxy=0;Sx2=0;Sy2=0;r=0;

for(i=1:N)

 Sx=Sx+X(i);

 Sy=Sy+Y(i);

 Sxy=Sxy+X(i)*Y(i);

 Sx2=Sx2+X(i)*X(i);

 Sy2=Sy2+Y(i)*Y(i);

end

r=(Sxy-((Sx*Sy)/N))/(sqrt(Sx2-((Sx*Sx)/N))*sqrt(Sy2-

((Sy*Sy)/N)))

disp('Karl Pearsons correlation coefficient r is:', r)








Question 10.2. From the following data compute Karl Pearson’s coefficient of

correlation.

X 9 8 7 6 5 4 3 2 1

Y 15 16 14 13 11 12 10 8 9

//Scilab code for Correlation coefficient by Karl Pearson method

clear ;

close ;

clc ;

X=[9 8 7 6 5 4 3 2 1]

Y=[15 16 14 13 11 12 10 8 9]

N=9;

Sx=0;Sy=0;Sxy=0;Sx2=0;Sy2=0;r=0;

for(i=1:N)

 Sx=Sx+X(i);

 Sy=Sy+Y(i);

 Sxy=Sxy+X(i)*Y(i);

 Sx2=Sx2+X(i)*X(i);

 Sy2=Sy2+Y(i)*Y(i);

end

r=(Sxy-((Sx*Sy)/N))/(sqrt(Sx2-((Sx*Sx)/N))*sqrt(Sy2-

((Sy*Sy)/N)))

disp('Karl Pearsons correlation coefficient r is:', r)






Comments