Download as pdf
Download as pdf
You are on page 1of 12
Tonov Stvey HELPER JOONIAZ SUMIL POONA UBLICATIONS Course Code : BCSL-058 Course Title : Computer oriented Numerical techniques Lab Assignment Number : BCA(S)/L-058/Assignment/2023-24 Maximum Marks : 50 ‘te ‘eit Weightage : 25% www.ignousite.com Last Dates for Submission : 31 October, 2023 (For July arse up Y* April, 2024 (For January 2024 Session) Note:- This assignmont only for students, not fr sal or re-upload any media or wobsito. Al iit reserve to “IGNOU Study Helper". tis legato share or ‘reupload it. anything lke thiss found, then appropriate action willbe taken and appl copyright ACT to you. You will te responsible for legal work. So don't share and upload on any mea, QI. Write a C/C++ program that implements (Do not use pivot condensation) Gaussian elimination method for solving A linear equations in n variables, that calls procedures 6) (i) Exchange of rows (ii) Lower-triangularisation and (iii) back substitutions (Codes of procedures are also to be written). Use the program for solving the following system of linear equations: a4 y+ 3x + 2y + 3z= 16 Aye Sy #32233 Q2. Write a program in C/CH+ to find:the root of the following equation by using “Bisection Method”. Equation: 8 -Sx+1=0; x€ (1,2) © (Q3. Write a C/C++ program that approximates a root of the equation f(x) = 0 in an interval [a, b] using Newton-Raphson method. The necessary assumptions for application of this method should be explicitly mentioned, Use the method to find one root of the equation: x* + 4x? - 19=0, © Q4. Write a C/C++ program thatvapproximates the value-of, a@efinite integral {? f(x)dx using ‘Trapezoidal Rule, with M Sample points, Find an approximate value of the integral of 4x° using the program With 6 intervals over the interval [0,3] 6) QS. Write a C/C++ program that approximates the solution of the initial value problem: y= ty) with ya) = yo over [a, b) tisiig Euler's method, Using the program approximate the solution of the initial value problem: y* =-2ty® with y() =1 (5) Ignou Study Helper-Sunil Poonia Page 1 JOONIAZ UBLICATIONS Tonov Stvey HELPER SuMie Paonia Q6. Write a program in C/C++ to calculate the value of “cos x” by using the series expansion given below: (5) Now: © Evaluate cos x only upto first three terms. Also find the value of cos x by Using the inbuilt function, * Compare the results ie., the result produced by your program and ‘© that produced by inbuilt function, Based on comparison, determine error. Q7. Write a program in C/C++ to find the value of Sin(6) by using Lagrange’s Interpolation, thé related datalis given below (5) x! 0 nid x =Sinog: 0 070711 LO Q8. Write a program that approximates the value of a definite integral f.” F(x)dx using Simpson 1/3 Rule, with M sample points. Find an approximate value of the integral of 2x“? using the program. with 8 intervals over the interval [1, 9} © ‘Ans. 1A C++ program that implements the Gaussian Elimination method for solving 2 system of linear equations using procedures for row exchange, lower-triangularization, and back substitution: ince sosteame Spe gs Hinclude Nines e using namespace std; const double EPSILON = 1e-9; void exchangeRows|vectorcvector>& matrix, int row4, int row2) { swap(matrixirow1], matrix{row2I); } void lowerTriangularize(vector>& matrix, int n) { for (int = 0; i backSubstitution(const vector>& matrix, int n) { vector salutions(n); for (int i= double sum = 0.0; for (int j =i; J> matrix=( 12.0, 1.0, 1.0, 7.0), {3.0, 2.0, 3.0, 16.0}, {4.0, 5.0, 3.0, 23.0} i 1/ Dispiay the original matrix cout << "Original Matrix"-<« endl; for int i=0;i solutions = backSubstitution(matrix, n); J Display the solutions cout << "\nsolutions for (int i= Oj} <1; +41) { cout <<"x" <= 0) { core ce "Rizectinn mathnd cannat he applied tn this interval " << endl; ; exit(1); (en double mid while (fabs(b- a) >= EPSILON) { mid = (a+) /2; if (function{mid) return mid; else if (function{mid) * function(a) <0} { b=mid; Jelse{ a= mid; ) } return mid; t 1.0) £ int maint) { double a = 1.0; // Lower bound of the interval double b = 2.0; // Upper bound of the interval double root = bisectionMethodla, b); cout << "Root of the equation within the interval [1, 2:""<< root << endl; return; t Ignou Study Helper-Sunil Poo Page 5 JOONIAZ Fees Touou Srny Hee UBLICATIONS. gsi? SUMIL Paoula Output: sn Ans. 3 A C++ program to find one root of the equation x*+ 4x” -19 = 0 using the Newtan-Raphson method: include =. finclude ey Using namespace std; const double EPSILON = 1e-9; const int MAX_ITERATIONS = 100; 1/ The function whose root we want to find: ffx) = x83 + 4x42 - 19 double function(double x) { return pow(x, 3)+4* pow(x, 2) - 19; , // Derivative of the function: f'(x) = 3x92 + 8x double derivative(double x) ( return 3" powlx, 2) +8 * x: t // Newton-Raphson Method implementation double newtonRaphsonMethod(double x0) { double x = x0, Int iterations ~ 0; while (fabs{function(x)) >= EPSILON && iterations < MAX_ITERATIONS) ( double x_next = x. (function(x) / derivative(x)); x=x next iterationst+; if iterations == MAX_ITERATIONS) { cerr << "Newton-Raphson method did not converge within the given maximum iterations." << endl; exit(1); } return x; Int main() { double a = 0; // Lower bound of the interval Ignou Study Helper-Sunil Poonia Page 6 JOONIAZ Fees Touou Srny Hee UBLICATIONS. gsi? SUMIL Paoula double b = 4.0; // Upper bound of the interval double initial_guess = 3.0; // Initial guess for the root double root = newtonRaphsonMethod{initial_guess); cout << "Approximate root of the equation x83 + 4x"2 - 19 = 0: " << root << endl; return 0; Ans. 4 A C+ program that uses the Trapezoidal Rule to approximate the definite integral of a function f(x) over an interval [a, b] ising M sample paints include include using namespace std; / The function for which we want to find the integral (x) = 4x%2 double function(double x) { return 4* powix, 2}; } 1/ Trapezoidal Rule implementation to approximate the definite integral double trapezoidalRtule(double a, double b, int M1) { double h = (b ~a) / My double integral = 0.5 * (function(a) + function(b)); for (int i= 1; i include sing namespace std; 1/ Function f(t, yh in the given differential equation: y' double function(double t, double y) { return-2* t* powly, 2]; } {/euler’s method implementation to approximate the solution of the initial value probiem void eulerMethod|double 2, double b, double yO, int N) { double h = (b-a) /N; double t double y = cout <<"t\t\tApproximation (y)" << endl; cout << fixed < ffinclude using namespace std; // Function to calculate the factorial of a number int factorial(int n)( if(n<=4){ 4 } return * factorial(n. 2); 1/ Function to calculate cos(x) using the series expansion up to the first three terms double cosSeriesExpansion|double x) { double result = 1.0; double term; Ignou Study Helper-Sunil Poonia Page 9 JOONIAZ es Towou Sry Heer UBLICATIONS SUMIL POONA for (int n<=6;4=2){ term poten} / factorial if (n%4 Jelse { result += term; return result; int main(){ double x = 0.5; // The value of x for which to calculate cosy) 1{ Calculate cos(x) using the series expansion double cos_series = cosSeries€xpansion(x) // Calculate cos(x) using the inbuilt function double cos_inbullt = cos(x); cout <<"€os(x) using series expansion (3 terms): " << cos_seties << endl} cout <<""cos(x| using inbuilt function: "-<« cos_inbuilt << endl Jf Calculate the error double error = fabs(cos_series ~cos_inbuilt); cout << "Error: " << error << endl; return 0; Output: ee reer enn ene race Beets asta nae Le Gree Ans. 7 A C+ program to find the value of Sin(/6) using Lagrange's Interpolation: include ffinclude Using namespace std; // Function to calculate the value of Sin(n/6) using Lagrange’s Interpolation Ignou Study Helper-Sunil Poonia Page 10 JOONIAZ fess Towou Stuny Heveee UBLICATIONS ey SUMIL POONA double lagrangeinterpolation({double x], double yf], int n, double target) { double result = for (int i= 0;i ffinclude using namespace std; // Function f(x) for which we want to find the integral: f(x) = 2x%(3/2) double function(double x] { return 2 sqrt); Ignou Study Helper-Sunil Poonia Page 11 JOONIAE Fees Touou Srny Hee UBLICATIONS Noe Sumit Paowia 1/ Simpson's 1/3 Rule implementation to approximate the definite integral double simpsonsRule{double a, double b, int M) { double h = (b-a)/M; double integral = function(a) + function); for (int |= 2; 1< Mi; +40) € double x_i=a+i*h; ffi %2==0){ integral += 2 * function); Jelse { integral } t 4 function(x integral *= h/3.0; return integral; _, ey ) fs ray Nat int main()( - double a = 1.0; // Lower bound of the interval double b = 9.0; // Upper bound of the interval int M=8; // Number of intervals. double integral = simpsonsRule(a, b, M); cout << "Approximate value of the integral of 2x%(3/2] over [1, 9]: " << integral << ent return O; Output: Ser en ee eee Ignou Study Helper-Sunil Poonia Page 12

You might also like