'C' Language Solved MCA Paper

19
www.InfoFanz.com SOLVED MCA PAPER on ‘C’ Language Presented By Muazzam Mehmood [email protected] Courtesy! www.InfoFanz.com www.Forum.InfoFanz.com www.PodCast.InfoFanz.com www.Photo.InfoFanz.com

Transcript of 'C' Language Solved MCA Paper

Page 1: 'C' Language Solved MCA Paper

www.InfoFanz.com

SOLVED MCA PAPER on ‘C’ LanguagePresented By

Muazzam Mehmood

[email protected]

Courtesy!

www.InfoFanz.com

www.Forum.InfoFanz.com

www.PodCast.InfoFanz.com

www.Photo.InfoFanz.com

www.Chill.InfoFanz.com

Page 2: 'C' Language Solved MCA Paper

www.InfoFanz.com

Question No. 1

‘A’ Write a program in C to find a root using Secant Method. Perform the five iterations to obtain the smallest positive root of the equation f(x) = cos x – xex = 0,, verify your answer with the program written by you.

Answer

/* Root Using Secant Method*/#include <stdio.h>#include <math.h>#define f(x) (exp(x)*log(x)-x*x)main()/* Main program to use the Secant Method to find the root off(x)=exp(x)*ln(x)-x*x=0. Copyright © Tao Pang 1997. */{int istep;double dl=1e-6;double a,b,xi,xf,di,df;void secant ();a = 1; b = 2;di = (b-a)/10;xi = (a+b)/2;secant (dl,xi,di,&xf,&df,&istep);printf("%4d %16.8lf %16.8lf\n", istep,xf,df);}void secant (dl,xi,di,xf,df,istep)/* Subroutine for the root of f(x)=0 with the secant method.Copyright © Tao Pang 1997. */int *istep;double dl,xi,di;double *xf,*df;{double x0,x1,x2,d;x0 = xi;x1 = xi+di;*df = di;*istep = 0;while (fabs(*df) > dl){d = f(x1)-f(x0);x2 = x1-f(x1)*(x1-x0)/d;x0 = x1;x1 = x2;*df = x1-x0;*istep = *istep+1;}*xf = x0;

Page 3: 'C' Language Solved MCA Paper

www.InfoFanz.com

}

‘B’

Out of the three methods i.e. Secant method, Regula Falsi method and the Newton Raphson method which method is more efficient and why? Determine the efficiency or the order of these three methods?

Answer

The Newton-Raphson method is considered to be the fastest method. But the method approaches to the root only when the initial guess is chosen very near the root. If the initial guess is not near the actual root, the method may diverge. The Secant method gives better results than Regula Falsi method method. But the Secant method may not converge to the root sometimes. But the Regula Falsi method always converges.

If f '(x) evaluation is not difficult then Newton Raphson method is recommended. If evaluation of f '(x) is difficult then Secant method is recommended. But the convergence must be verified using bisection method once in every 10 iterations.

This is a detailed examination of four methods of discovering roots and the effects of different equations on the efficiency of each method and the amount of error produced by each method. Although some calculation here has been done by hand, the vast majority of our conclusions were arrived at by using computer programs for each of the methods. The four methods examined were bisection, false-position, secant, and Newton. Each method seemed to have a particular type of equation or desirable outcome for which it is most efficient.

Secant

One of the faster methods, compared to bisection and false position, for approximating roots is the secant method. Unlike the other two methods, f(a) and f(b) of the two initial values that we pick to start with do not have be on opposites sides of the root and have a sign change. This one works by always making the old b value into the new a value. The new b value then becomes the zero of the secant line.

One of the problems with this method is that the slope of the secant line can become very small and this will cause it to move very far from the points you have. This method fails when estimating the roots of an equation, which has a very low slope near the root because the secant line will jump large amounts due to the low slope. Another problem is that this method like the false position method does not know when to stop. It must be performed several times until the f of the current guess is very small.

Newton’s Method

Newton’s method is generally used in calculus to approximate roots. One main difference between Newton and other methods is that it only requires one initial guess instead of two. Also,

Page 4: 'C' Language Solved MCA Paper

www.InfoFanz.com

if the guess is relatively close to a root, Newton's method works incredibly fast. If the guess is not close to a root, however, Newton’s method can work slowly and, depending on the function, may be impossible to use.

One similarity between Newton’s method and the bisection method is that both approximate roots in a consistent fashion. This allows the user to predict exactly how long it will take to arrive at a root, assuming the function and the original guess are sufficiently "nice" for Newton’s method. Newton’s method doubles in precision with each step. This is why Newton requires the same number of steps in Table B, regardless of the value of n in the equation.

Newton’s method uses not only the function itself but also the derivative of that function to approximate the root. One chooses a point ‘a’. Throughout the course of the method, this will be replaced by the zero of the tangent line (the zero of the derivative). To find the new point, use the formula [a – (f(a))/(f’(a))]. This will be the new ‘a’, and one repeats this process until the root is found to sufficient precision.

Another interesting possibility with Newton’s method is error calculation.

Error calculations simply show the difference between the values arrived at by some method and the actual value. Notice below where R is the root and x is the result of the most recent Newton iteration.

Error = x - R

New Error = x- (f(x)/f'(x))-R ~ f(x) and f'(x) are expanded out to a power series.

With the Newton's method, we make the assumption that X is close enough to R so that the first nonzero term in the power series in numerator and denominator is almost the entire error. Finally, following this assumption, one can determine that the new error is about (x-R)/2, which means that the error is cut in half each time.

False Position

Another method used for estimating roots of equations is the false position method. This method functions by first starting with two endpoints a and b. If f(a) and f(b) are not roots, then f(a) times f(b) is a negative value since they both need to lie on opposites sides of the root. This follows the logic that a negative value times a positive value equals a negative value. After the endpoints are chosen, false position finds the zero of the secant line joining (a,f(a)) and (b,f(b)) and then setting that as its new endpoint. This is done repetitively as the endpoints become continually closer to the root. An examination of the computer code for the false position method shows that it is very similar to the bisection method except that it uses the zero of the secant line to approach the root rather then finding the midpoint each time.

The false position method is often significantly faster then the bisection method for equations such as y = xn –2 for a small value of n. When n becomes large then false position often becomes the slowest method. In the data section of this paper, table B shows the number of repetitions for each method and value of n using the equation y = xn-2.

Data

Page 5: 'C' Language Solved MCA Paper

www.InfoFanz.com

Table A:

f(x) = x2 – 2 a = 1 b = 2 accuracy within 0.00001

Method Name Number of steps

Bisection 17 steps

False – Position 8 steps

Secant 5 steps

Newton 4 steps

Table B:

For f(x) = xn – 2, a = 1, b = 2, n = {3, 4,…, 10} and accuracy within 0.0001

Number of steps n = 3 n = 4 n = 5 n = 6 n = 7 n = 8 n = 9 N = 10

Bisection 17 17 17 17 17 17 17 17

False – Position 13 20 31 49 76 120 189 296

Secant 6 7 7 7 8 8 8 8

Newton’s 4 4 4 4 4 4 4 4

Table C:

For f(x) = x2 – 2, a = 1, b = 2, done for 25 iterations of each method, the error was:

Method: Error:

Page 6: 'C' Language Solved MCA Paper

www.InfoFanz.com

Bisection 9.501 x 10-8

False-Position -2.420323 x 10-8

Secant -2.420323 x 10-8

Newton -2.420323 x 10-8

For secant and Newton, the program was not able to complete 25 iterations. After 6 iterations, the memory overflowed.

Analysis of Data

The above data produces "nice" endpoints since the functions that were examined are also ideal. Even so, there was a lot of variation in the speed, accuracy, and reliability of each method. Table A shows the variation in speed between all four methods. It is clear that in this case Newton’s method would be a good choice for finding the root.

In Table B, one can see the consistency with which the bisection and Newton methods work for nice functions. This same consistency could be seen using the bisection method on any function, but Newton requires a relatively calm function in order to be helpful. One can also see that one method’s definition of a nice function is not the same as another method’s definition. Newton will work well on any non-oscillating, non-asymptotic function whose slope is not very close to zero. Nearly any polynomial fits this description. However, the false -position method’s effectiveness is inversely proportional to the degree of the polynomial. False-position’s definition of a nice function would be something more on the order of a low – degree function.

Table C shows the degree of accuracy, which can be achieved in 25 iterations of each method. The errors for all the methods are the same except for the bisection method. This once again emphasizes how relatively fast the other three are for functions they consider nice. The main problem encountered in this exercise had to do with the speed of these functions. Because they worked so quickly, within six steps secant and Newton’s methods had produced results, which were as accurate as the computer’s memory could hold. After six iterations, the computer would have a memory overflow. We corrected this by altering the number of iterations.

Conclusion

It was shown first-hand in this analysis that the speed of the method was inversely proportional to the consistency of the method to deal with non-nice functions. Newton’s method originally seemed to be an exception to this rule, because it was about five times as fast as bisection, but just as consistent for xn – 2 for all n. It was after trying some functions of different forms that one can find that the functions which are nice for Newton’s method are different than those for false-position and secant methods. These non-nice functions are easily found for Newton’s method, which makes Newton inconsistent and unable to efficiently solve for the root.

Page 7: 'C' Language Solved MCA Paper

www.InfoFanz.com

For nice functions and guesses relatively close to the root, each of the methods used require relatively few iterations before coming very close to that root. By examining the type of function, one can determine which sort of method would be most efficient in finding the root.

‘C’

Please solve the equations given below?

You’ll be using the LU decomposition-method

Answer

I got the values asL11 = 10 L21 = 1 L22 = 101/10

L31 = 2L32 = 32/10L33 = 2018/101

U12 = -1/10U13 = 1/5U23 = -12/101

Using LUX = BLet UX = y LY = B

10y1 = 4 y1 = 4/10

y1 + 101/10 y2 = 3 y2 = 26 /101

2y1 +32/10 y2 + 2018/101 y3 = 7 y3 = 543/2018Now solving UX = Y The equation are..

x1 -1/10 x2 + 1/5 x3 = 4/10x2 - 12/101 x3 = 26/101

Page 8: 'C' Language Solved MCA Paper

www.InfoFanz.com

x3 = 543/2018 = 0.269

x2 = 58984/203818 = .289

x1 = 76457/203818 = 0.375You can verify the answer by substituting it in the given equation.

‘D’ Look into the system of equations as given below. You’ll be using Jacobi iterative-method and will perform three iterations.

This system can be written as X1 = 0.5X2 + 0X3 + 3.5 X2 = 0.5X1 +0.5X3 + 0.5 X3 = 0X1 + 0.5X3 + 0.5X1

k+1 = ½ (X2k + 7)

X2k+1 = ½ (X3

k + X1k + 1)

X3k+1 = ½ (X2

k + 1)

So the Jacobbi iteration is:

No initial iteration is given so we start with X0 = (0, 0, 0)T

We get the following iteration X1 = [3.500 0.500 0.500 ]T

X2 = [3.750 2.500 0.750]T

X3 = [4.750 2.750 1.750]T

X4 = [4.875 3.750 1.875]T

After 4 iteration the errors in the solutions are

Page 9: 'C' Language Solved MCA Paper

www.InfoFanz.com

The solution X4 is almost correct to 3 decimal positions.

Question No.2 (a)Find the unique polynomial of degree 2 or less, such that f(0)=1, f(1)=3, f(3)=55, using the Lagrange interpolation? Write a program in C evaluating the polynomial using Lagrange interpolation.

Answer :

Here we have given the following dataf(0) = f(X0) = 1f(1) = f(X1) = 3f(3) = f(X2) = 5Therefore, f(x) =(x-x1)(x-x2)/(x0-x1)(x0-x2) * f(x0)+

(x-x0)(x-x2)/(x1-x0)(x1-x2) * f(x1)+(x-x0)(x-x1)/(x2-x0)(x0-x1) * f(x2)

=(x-1)(x-3)/(0-1)(0-3) *1+(x-0)(x-3)/(1-0)(1-3)*3+(x-1)(x-0)/(3-0)(3-1)*55

Simplifying this above equation, we get the following f(x) polynomial

F(x) =8 x square - 6 x.

/* Lagrange Polynomial */#include <math.h>#include <stdio.h>main(){float x[12],y[12];float xtem,ytem;int points,i,j,count;float xval,temp,total;scanf("%d %f\n",&points,&xval);printf("number of points = %d xvalue to calculate = %f\n",points,xval);printf("\nx y\n");for (i=0;i<points;i++){ scanf("%f %f\n",&xtem,&ytem); x[i] = xtem; y[i] = ytem; printf("%f %f\n",xtem,ytem);}total = 0.0;for (i=0;i<points;i++){ temp = 1.;

Page 10: 'C' Language Solved MCA Paper

www.InfoFanz.com

for(j=0;j<points;j++){ if (j != i) { temp = temp * (xval - x[j]) / (x[i] - x[j]); } } temp *= y[i]; total += temp;}printf("The value of the function at x = %f is y = %f\n\n\n",xval,total);}

Question.2 (b) Compute the integral by applying Gauss’s Quadrature formula. Answer:

Note:-All numerical coefficients of y and u which I use here should be written as suffix

For example---->>> y(0) i.e. y suffix 0 AND

u (-2) i.e. u suffix -2

Fi means special mathematical symbol

So plz consider all following terms as above

Soln:-

Here we put, x= (b-a)u+a+b/2 = 7u+8.5

y=1/x=1/7u+8.5=Fi(u)

Taking n=5, we have following

y(0)=Fi(u(0))=1/8.5=0.117647059

y(1)=Fi(u(1))=1/7u(1)+8.5=1/10.3846426=0.0962960439

y(-1)=Fi(u(-1))=1/7u(-1)+8.5=1/6.61535741=0.151163412

y(2)=Fi(u(2))=1/7u(2)+8.5=1/11.67162946=0.0856778399

y(-2)=Fi(u(-2))=1/7u(-2)+8.5=1/5.32837054=0.187674636

Substituting these values in the formula, together with the corresponding R's for n=5, we get

I=7[64/225*0.117647059+0.2393143352 (0.151163412+0.0962960439) +0.1184634425(0.187674636+0.0856778399)]

=0.875468458

Question.2 (c)

Page 11: 'C' Language Solved MCA Paper

www.InfoFanz.com

Using Runge-kutta method obtain y when x=1.1, given that y=1.2 when x=1 any y satisfies the equation.

Dy/dx=3x2+y2.

Answer:

We h've given, dy/dx = 3x sqr + y sqry = 1.2, when x = 1 We h've to find y (1.1) i.e value of y when x = 1.1.We take x 0 = 1, y 0 = 1.2 and h = 1.1Here k1 =hf (x 0, y 0)= 1.1*{3(1) sqr + 1.2 sqr }= 4.884

k2 = hf (x 0 + 1/2 h, y 0 + 1/2 k1)= 1.1 f (1 + 1.1/2, 1.2 + 4.884/2)= 22.51876k3 = hf (x 0 + 1/2 h, y 0 +1/2 k2)= 1.1 f (1.55, 1.2 + 1/2 * 22.51876)= 178.68801

k4=hf (x 0 + h, y 0 + k3)= 1.1 f (2.1, 179.88801)= 35610.21871Therefore, y (1.1) = 2+1/6(4.884+2*22.51876+2*178.68801)= 2+1/6(4.884+45.03752+357.37602)= 2+1/2(407.29754)=69.8829

Question.2 (d) Evaluate the integral

Using (i) composite trapezoidal rule, (ii) composite Simpson’s rule, with 2, 4 and 8 equal subintervals. Ans:

When N=2, we have h=0.5, Nodes are 0, 0.5 and 1 We obtain lT = ¼ [f(0) + 2f(0.5) + f(1) ] = ¼ [1+ 4/3 + ½] =0.708333lS = 1/6 [ f(0) + 4f(0.5) + f(1) ] = 1/6 [1+ 8/3 + ½] = 0.694444

N=4 : h = 0.25 , Nodes are 0, 0.25 , 0.5 , 0.75 and 1 . We obtain

Page 12: 'C' Language Solved MCA Paper

www.InfoFanz.com

lT = 1/8 [ f(0) + 2{f(0.25) +f(0.5) +f(0.75)} +f(1) ] = 0.697024lS=1/12 [f(0) + 4f(0.25) + 2f(0.5) + 4f(0.75) + f(1) ] = 0.693254

N = 8 : h = 0.125 , nodes are 0, 0.125 , 0.25 , …….1.0

We have eight subintervals for trapezoidal rule and four subintervals for Simpson’s rule. We get

lT = 1/6 [ f(0) + 27i=1 f(i/8) + f(1) ]

= 0.694122lS= 1/24 [ f(0) + 44

i=1 f(2i-1)/8 + 23i=1 f (2i)/8 + f(1) ]

= 0.693155The exact value of the integral is I = 0.693147

Question-3: (a)Find the probability of getting between 6 and 9 tails inclusive in 20 tosses of a fair coin by using (i) the binomial distribution, (ii) the normal approximation to the binomial distribution.

Answer:-

(i)

Let us call it a 'success' when tails comes .

Then , p = 1/2 , q=1/2 and n=20

Therefore the probability of getting between 6 and 9 tails in 20 tosses are as follows

p(6)+p(7)+p(8)+p(9)= 20 C 6 (1/2)^6 (1/2)^14 +20 C 7 (1/2)^7 (1/2)^13 +20 C 8 (1/2)^8 (1/2)^12 +20 C 9 (1/2)^9 (1/2)^11 = fact20/fact6 fact14*64*16384 + fact20/fact7 fact13*128*8192 + fact20/fact8 fact12*256*4096 + fact20/fact9 fact11*512*2048=4845/131072+77520/1048576+125970/1048576+1175720/1048576=1.3521

(ii)

Here n=20 , p=1/2 , q=1/2 σ^2=npq=5σ=2.236μ=np=10We require P[6<X<9]

Using standard normal distribution , we get

Page 13: 'C' Language Solved MCA Paper

www.InfoFanz.com

p[6-10/2.236<Z<9-10/2.236]=p[-1.78<Z<-0.44]=0.2925

Question-3 (b)Table 2 below shows the respective heights x and y of a sample of 12 fathers and their oldest sons. (i) Construct a scatter diagram. (ii) Find the least-squares regression line of y on x. (iii) Find the least-squares regression line of x and y.

Table 2

Height x of Father (centimeters)

165 160 170 163 173 158 178 168 173 170 175 180

Height y of Son (centimeters)

173 168 173 165 175 168 173 165 180 170 173 178

Answer:-

From given table we have, ∑x=2033 , Mean(x) =169.42,

∑y=2061, Mean(y) =171.75 , u=x-169, v=y-171

X y u u2 v v2 u*v

165 173 -104 10816 2 4 -208

160 168 -9 81 -3 9 27

170 173 1 1 2 4 2

163 65 -6 36 -6 36 36

173 175 4 16 4 16 16

158 168 -11 121 -3 9 33

178 173 9 81 2 4 18

168 165 -1 1 -6 36 6

173 180 4 16 9 81 36

170 170 1 1 -1 1 -1

175 173 6 36 2 4 12

Page 14: 'C' Language Solved MCA Paper

www.InfoFanz.com

180 178 11 121 7 49 77

From above table we get,

∑u=-95, ∑v=9, ∑uv=54, ∑u^2=11327, ∑v^2=253

Therefore, b_yx = ∑uv-1/n ∑u∑v / ∑u^2-(∑u) ^2/n =1503/126899 =0.01 b_xy = ∑uv-1/n ∑u∑v / ∑v^2-(∑v) ^2/n =1503/2955 =0.51

Therefore, the least-square regression line of y on x is: y-171.75=(0.01)(x-169.42)

=≫ y=0.01x+169.31

Regression line of x on y is: x-169.42=(0.51)(y-171.75) =≫ x=0.51y+169.31 =≫ x=0.51y+81.83Question-3 (c) Find the area under the standard normal curve (a) between z = 0 and z = 1.2, (b) between z = – 0.68 and z = 0, (c) between z = – 0.46 and z = 2.21, (d) between z = 0.81 and z = 1.94.

Ans:-

(a) z=0 and z=1.2p (0 ≤ z ≤ 1.2)

z = 0 z = 1.2– 0.68

0.81 1.94

Figure a Figure b

Figure dFigure c

Page 15: 'C' Language Solved MCA Paper

www.InfoFanz.com

=F (1.2)-F (0)=.3849 (from normal dist table)

(b) z=-0.68 and z=0p (-0.068 ≤ z ≤ 0)=p (0 ≤z ≤0.068)=F (0.068)-F(0)=0.7517 (from normal dist table)

(c) z=-0.46 and z=2.21p (-0.46 ≤ z ≤ 2.21)=F (2.21)-[1-F (0.46)]=0.9864-0.3228 (from normal dist table)=0.6636

(d)z=0.81 and z=1.94p (0.81 ≤ z ≤ 1.94)=F (1.94)-F (0.81)=0.9738-0.7910 (from normal dist table)=0.1828