SlideShare a Scribd company logo
Course Code : BCSL-058
Course Title : Computer Oriented Numerical Techniques Lab
Assignment Number : BCA(V)/058/Assign/14-15
Problem 2: Write a programme that implements (non-pivoting) Gaussian elimination method for
solving n linear equations in n variables, that calls procedures
(i) lower-triangularisation and
(ii) back substitutions
(codes of procedures are also to be written).
Use the programme for solving the following system of linear equations:
x+2y=10z
2x+y+z=9
5x+3y+2z=23
Solution:
#include<stdio.h>
int main()
{
double matrix[10][10],a,b, temp[10]; int i, j, k, n;
printf(“Enter the no of variables: “); scanf(“%d”, &n);
printf(“Enter the agumented matrix:n”); for(i = 0; i < n ; i++){
for(j = 0; j < (n+1); j++){
scanf(“%lf”, &matrix[i][j]);
}
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(j<i){
a = matrix[j][i];
b = matrix[i][i];
for(k = 0; k < n+1; k++){
matrix[j][k] = matrix[j][k] – (a/b) * matrix[i][k];
}
}
}
}
printf(“The Upper triangular matrix is: n”); for( i = 0; i < n; i++){
for(j = 0; j < n+1; j++){
printf(“%.2f”, matrix[i][j]); printf(“t”);
}
printf(“n”);
}
printf(“nThe required result is: “); for(i = n-1; i>=0; i–){
b = matrix[i][n];
for(j = n-1 ; j > i; j–){
b -= temp[n-j]*matrix[i][j];
}
temp[n-i] = b/matrix[i][i];
printf(“n%c => %.2f”,97+i, temp[n-i]);
}
}
Problem 3: Write a programme that approximates a root of the equation f (x) = 0 in an interval
[a, b] using regula-falsi method. The necessary assumptions for application of regulafalsi method
should be explicitly mentioned. Use the method to find a root of the equation x2-8x+15=0.
Solution:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define ESP 0.001
#define F(x)(x)*(x)-8*(x)+15
void main()
{
int i = 1;
float x0,x1,x2;
double f1,f2,f0,t;
clrscr();
printf(“nEnter the value of x0: “);
scanf(“%f”,&x0);
printf(“nEnter the value of x1: “);
scanf(“%f”,&x1);
printf(“n_______________________________________________________________
___n”);
printf(“niterationt x0t x1t x2t f0t f1t f2″);
printf(“n_______________________________________________________________
____n”);
do
{
x2=(x0*f1-x1*f0)/(f1 - f0);
f0=F(x0);
f1=F(x1);
f2=F(x2);
printf(“n%d %f %f %f %lf %lf %lf”,i,x0,x1,x2,f0,f1,f2);
if(f0*f2<0)
{
x1=x2;
}
else
{
x0=x2;
}
i++;
}while(fabs(f2)>ESP);
printf(“n__________________________________________________________n”);
printf(“nnApp.root = %f”,x2);
getch();
}
Problem No. 4: Write a programme that approximates a root of the equation f (x) = 0 in interval
[a, b] using Gauss-Seidel method. Use the method to solve the system of linear equations given in
Q. No. 2 above. You may make assumption for appropriate initial values of the variable.
Solution:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_DIM 100
#define MAX_ITER 500
#define TOLERANCE 1.e-6
void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n); void main ()
{
int i, j, n;
int violation_counter, answer;
int violation_rows[MAX_DIM];
double sum;
double a[MAX_DIM][MAX_DIM];
double b[MAX_DIM],x[MAX_DIM];
/* read in data */
n = MAX_DIM + 1;
while (n > MAX_DIM) {
printf (“Enter the dimension of the system to be solved: “); scanf (“%d”,&n);
}
printf (“n”);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf (“Enter the (%d,%d) element of the system matrix: “,i+1,j+1); scanf (“%lf”,&a[i][j]);
}
printf (“Enter element (%d) of the right-hand-side vector: “,i+1); scanf (“%lf”,&b[i]);
printf (“n”);
}
/* test the convergence criterion */
violation_counter = 0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++)
if (i != j)
sum = sum + fabs(a[i][j]);
if (fabs(a[i][j]) < sum) {
violation_rows[violation_counter] = i;
violation_counter = violation_counter + 1;
}
if (a[i][j] == 0.0) {
printf (“Found diagonal element equal to zero; rearrange equations; exiting …n”); exit
(0);
}
}
if (violation_counter > 0) {
printf (“The Gauss_Seidel convergence criterion is violated in %d rows out of %dn”,
violation_counter,n);
printf (“Specifically, it was violated in rows:n”);
for (i = 0; i < violation_counter; i++)
printf (“%d “,violation_rows[i]+1);
printf (“n”);
printf (“Enter 1 if you want to continue; any other number to abort: “); scanf
(“%d”,&answer);
if (answer != 1)
exit(1);
printf (“Check results carefullynn”);
}
/* Initialize the solution vector — initial guesses */
for (i = 0; i < n; i++) {
printf (“Enter an initial guess for element (%d) of the solution vector: “i+1); scanf
(“%lf”,&x[i]);
}
/* solve the system */
gauss_seidel (a,b,x,n);
/* output solution */
for (i = 0; i < n; i++)
printf (“x[%d]=%fn”,i+1,x[i]);
printf (“n”);
}
/* function to solve a system using Gauss-Seidel. THIS IS THE PART WHERE I NEED
HELP!!*/ void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n)
maxerror = 1.0;
while (maxerror > 1.e-6) {
on_error = 0;
for (i = 0; i < n; i++) {
xold = x[i];
/* Need help here! */
e = fabs((x[i] – xold) / x[i]);
if (e > iteration_error)
iteration_error = e;
}
maxerror = iteration_error;
}
Problem No. 5: Write a programme that constructs Newton’s Interpolating Polynomials, for
which at most four nodes are given (hence interpolating polynomial will be at most cubic). Using
the programme, find Newton’s Interpolating polynomial that approximiates f(x)=2×3–x+3 and
the nodes given are x0=-1, x1=0, x2=1 and x3=2. Use the polynomial to approximate value at x =
1.5.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c;
clrscr();
printf(“nhow many record you will be enter: “); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]);
}
printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p);
for(i=0;i<n;i++)
{
temp = 1;
k = i;
for(j=0;j<n;j++)
{
if(k==j)
{
continue;
}
else
{
temp = temp * ((p-x[j])/(x[k]-x[j]));
}
}
f[i]=y[i]*temp;
}
{
sum = sum + f[i];
}
printf(“nn f(%.1f) = %f “,p,sum);
getch();
}
Problem No. 6: Repeat Problem No. 5 for constructing Lagrangian Polynomial instead of
Newton’s Polynomials.
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10][10],sum,p,u,temp;
int i,n,j,k=0,f,m;
float fact(int);
clrscr();
printf(“nhow many record you will be enter:”); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[k][i]);
}
printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p);
for(i=1;i<n;i++)
{
k=i;
for(j=0;j<n-i;j++)
{
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;
}
}
printf(“n_____________________________________________________n”);
printf(“n x(i)t y(i)t y1(i) y2(i) y3(i) y4(i)”);
printf(“n_____________________________________________________n”);
for(i=0;i<n;i++)
{
printf(“n %.3f”,x[i]);
for(j=0;j<n-i;j++)
{
printf(“ “);
printf(“ %.3f”,y[j][i]);
}
printf(“n”);
}
i=0;
do
{
if(x[i]<p && p<x[i+1])
k=1;
else
i++;
}while(k != 1);
f=i;
sum=0;
for(i=0;i<n-1;i++)
{
k=f;
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (p - x[k]);
k++;
}
sum = sum + temp*(y[i][f]);
}
printf(“nn f(%.2f) = %f “,p,sum);
getch();
}
Problem No.7: Write a programme that approximates the value of a definite integral (
)fb
z f(x)dx using Trapezoidal Rule, with m sample points. Find an approximate value of the
integral of f(x)=3x(cos2 x) over the interval [1, 6].
Solution:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf(“nhow many record you will be enter: “); scanf(“%d”,&n);
for(i=0; i<n; i++)
{
printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
sum = sum + y[i];
sum = sum * (h/2);
printf(“nn I = %f “,sum);
}
getch();
}
Problem No. 8: Write a program that solves Initial Value Problem (IVP) of order
one:y’(x)=dy/dx=f(x,y) with initial condition at x=x0 as y(x0) = y0using Improved Euler’s Method.
Using your program solve the IVP: y’=dy/dx= x2 + y2 − 2, y(0)=1. Compute upto 5 places of
decimal.
Solution:
#include<stdio.h>
#include<conio.h>
float fun(float,float);
void main()
{
int i,n;
float x0,y0[10],h,e;
clrscr();
printf(“enter the value of x0 and y0n”);
scanf(“%f%f”,&x0,&y0[0]);
printf(“enter the value of hn”);
scanf(“%f”,&h);
printf(“enter the value at which you want to findn”); scanf(“%f”,&e);
n=(int)((e-x0)/h+0.5);
for(i=0;i<n;i++)
{
y0[i+1]=y0[i]+h*fun(x0,y0[i]);
x0=x0+h;
}
for(i=0;i<n;i++)
printf(“%f”,y0[i]);
getch();
}
float fun(float x,float y)
{
float a=x*x+y*y-2;
return(a);
}

More Related Content

DOC
Makalah komputer pemerintahan
PDF
18 me54 turbo machines module 03 question no 6a &amp; 6b
PPTX
Lecture 02 fundamental concepts of internet and www khalid khan
DOCX
Dms 22319 practical
DOCX
45. online sales and inventory management system
PPT
Regulafalsi_bydinesh
PPTX
Regula falsi method
Makalah komputer pemerintahan
18 me54 turbo machines module 03 question no 6a &amp; 6b
Lecture 02 fundamental concepts of internet and www khalid khan
Dms 22319 practical
45. online sales and inventory management system
Regulafalsi_bydinesh
Regula falsi method

Viewers also liked (10)

PPTX
The False-Position Method
PPTX
Teaching and Learning Process
PDF
What Makes Great Infographics
PDF
You Suck At PowerPoint!
PDF
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
PDF
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
PDF
Masters of SlideShare
PDF
10 Ways to Win at SlideShare SEO & Presentation Optimization
PDF
How to Make Awesome SlideShares: Tips & Tricks
The False-Position Method
Teaching and Learning Process
What Makes Great Infographics
You Suck At PowerPoint!
STOP! VIEW THIS! 10-Step Checklist When Uploading to Slideshare
How To Get More From SlideShare - Super-Simple Tips For Content Marketing
Masters of SlideShare
10 Ways to Win at SlideShare SEO & Presentation Optimization
How to Make Awesome SlideShares: Tips & Tricks
Ad

Similar to BCSL 058 solved assignment (20)

PDF
Assignment on Numerical Method C Code
DOCX
DataStructures notes
DOCX
C lab manaual
DOCX
DOCX
ADA FILE
PDF
C Language Lecture 17
PPTX
L25-L26-Parameter passing techniques.pptx
PDF
Data Structure using C
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
PPTX
C Programming Example
DOCX
DAA Lab File C Programs
DOCX
PDF
C programs
PDF
C Programming Example
PDF
C Language Lecture 18
DOC
Daapracticals 111105084852-phpapp02
PPTX
3. chapter ii
DOCX
Numerical Method Assignment
Assignment on Numerical Method C Code
DataStructures notes
C lab manaual
ADA FILE
C Language Lecture 17
L25-L26-Parameter passing techniques.pptx
Data Structure using C
Let us C (by yashvant Kanetkar) chapter 3 Solution
C Programming Example
DAA Lab File C Programs
C programs
C Programming Example
C Language Lecture 18
Daapracticals 111105084852-phpapp02
3. chapter ii
Numerical Method Assignment
Ad

More from Indira Gnadhi National Open University (IGNOU) (18)

PDF
Mcs 17 solved assignment 2015- 16
PDF
Mcs 16 solved assignment 2015-16
PDF
Mcs 014 solved assignment 2015-16
PDF
Mcs 012 soved assignment 2015-16
PDF
Mcs 011 solved assignment 2015-16
PDF
PDF
PDF
Bcs 053 solved assignment 2014-15
PDF
2012 bcsl-021 solve assihnment
PDF
Bcsl 022 solved-assignment_2012-13

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Institutional Correction lecture only . . .
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Institutional Correction lecture only . . .
O5-L3 Freight Transport Ops (International) V1.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Week 4 Term 3 Study Techniques revisited.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
102 student loan defaulters named and shamed – Is someone you know on the list?
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pharma ospi slides which help in ospi learning
Microbial diseases, their pathogenesis and prophylaxis
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Classroom Observation Tools for Teachers

BCSL 058 solved assignment

  • 1. Course Code : BCSL-058 Course Title : Computer Oriented Numerical Techniques Lab Assignment Number : BCA(V)/058/Assign/14-15 Problem 2: Write a programme that implements (non-pivoting) Gaussian elimination method for solving n linear equations in n variables, that calls procedures (i) lower-triangularisation and (ii) back substitutions (codes of procedures are also to be written). Use the programme for solving the following system of linear equations: x+2y=10z 2x+y+z=9 5x+3y+2z=23 Solution: #include<stdio.h> int main() { double matrix[10][10],a,b, temp[10]; int i, j, k, n; printf(“Enter the no of variables: “); scanf(“%d”, &n); printf(“Enter the agumented matrix:n”); for(i = 0; i < n ; i++){ for(j = 0; j < (n+1); j++){ scanf(“%lf”, &matrix[i][j]); } } for(i = 0; i < n; i++){ for(j = 0; j < n; j++){ if(j<i){ a = matrix[j][i]; b = matrix[i][i]; for(k = 0; k < n+1; k++){ matrix[j][k] = matrix[j][k] – (a/b) * matrix[i][k]; } } } } printf(“The Upper triangular matrix is: n”); for( i = 0; i < n; i++){ for(j = 0; j < n+1; j++){ printf(“%.2f”, matrix[i][j]); printf(“t”); } printf(“n”); }
  • 2. printf(“nThe required result is: “); for(i = n-1; i>=0; i–){ b = matrix[i][n]; for(j = n-1 ; j > i; j–){ b -= temp[n-j]*matrix[i][j]; } temp[n-i] = b/matrix[i][i]; printf(“n%c => %.2f”,97+i, temp[n-i]); } } Problem 3: Write a programme that approximates a root of the equation f (x) = 0 in an interval [a, b] using regula-falsi method. The necessary assumptions for application of regulafalsi method should be explicitly mentioned. Use the method to find a root of the equation x2-8x+15=0. Solution: #include<stdio.h> #include<math.h> #include<conio.h> #define ESP 0.001 #define F(x)(x)*(x)-8*(x)+15 void main() { int i = 1; float x0,x1,x2; double f1,f2,f0,t; clrscr(); printf(“nEnter the value of x0: “); scanf(“%f”,&x0); printf(“nEnter the value of x1: “); scanf(“%f”,&x1); printf(“n_______________________________________________________________ ___n”); printf(“niterationt x0t x1t x2t f0t f1t f2″); printf(“n_______________________________________________________________ ____n”); do { x2=(x0*f1-x1*f0)/(f1 - f0); f0=F(x0); f1=F(x1); f2=F(x2);
  • 3. printf(“n%d %f %f %f %lf %lf %lf”,i,x0,x1,x2,f0,f1,f2); if(f0*f2<0) { x1=x2; } else { x0=x2; } i++; }while(fabs(f2)>ESP); printf(“n__________________________________________________________n”); printf(“nnApp.root = %f”,x2); getch(); } Problem No. 4: Write a programme that approximates a root of the equation f (x) = 0 in interval [a, b] using Gauss-Seidel method. Use the method to solve the system of linear equations given in Q. No. 2 above. You may make assumption for appropriate initial values of the variable. Solution: #include <stdio.h> #include <math.h> #include <stdlib.h> #define MAX_DIM 100 #define MAX_ITER 500 #define TOLERANCE 1.e-6 void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n); void main () { int i, j, n; int violation_counter, answer; int violation_rows[MAX_DIM]; double sum; double a[MAX_DIM][MAX_DIM]; double b[MAX_DIM],x[MAX_DIM]; /* read in data */ n = MAX_DIM + 1; while (n > MAX_DIM) { printf (“Enter the dimension of the system to be solved: “); scanf (“%d”,&n); } printf (“n”);
  • 4. for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf (“Enter the (%d,%d) element of the system matrix: “,i+1,j+1); scanf (“%lf”,&a[i][j]); } printf (“Enter element (%d) of the right-hand-side vector: “,i+1); scanf (“%lf”,&b[i]); printf (“n”); } /* test the convergence criterion */ violation_counter = 0; for (i = 0; i < n; i++) { sum = 0.0; for (j = 0; j < n; j++) if (i != j) sum = sum + fabs(a[i][j]); if (fabs(a[i][j]) < sum) { violation_rows[violation_counter] = i; violation_counter = violation_counter + 1; } if (a[i][j] == 0.0) { printf (“Found diagonal element equal to zero; rearrange equations; exiting …n”); exit (0); } } if (violation_counter > 0) { printf (“The Gauss_Seidel convergence criterion is violated in %d rows out of %dn”, violation_counter,n); printf (“Specifically, it was violated in rows:n”); for (i = 0; i < violation_counter; i++) printf (“%d “,violation_rows[i]+1); printf (“n”); printf (“Enter 1 if you want to continue; any other number to abort: “); scanf (“%d”,&answer); if (answer != 1) exit(1); printf (“Check results carefullynn”); } /* Initialize the solution vector — initial guesses */ for (i = 0; i < n; i++) { printf (“Enter an initial guess for element (%d) of the solution vector: “i+1); scanf (“%lf”,&x[i]);
  • 5. } /* solve the system */ gauss_seidel (a,b,x,n); /* output solution */ for (i = 0; i < n; i++) printf (“x[%d]=%fn”,i+1,x[i]); printf (“n”); } /* function to solve a system using Gauss-Seidel. THIS IS THE PART WHERE I NEED HELP!!*/ void gauss_seidel (double a[][MAX_DIM], double b[], double x[], int n) maxerror = 1.0; while (maxerror > 1.e-6) { on_error = 0; for (i = 0; i < n; i++) { xold = x[i]; /* Need help here! */ e = fabs((x[i] – xold) / x[i]); if (e > iteration_error) iteration_error = e; } maxerror = iteration_error; } Problem No. 5: Write a programme that constructs Newton’s Interpolating Polynomials, for which at most four nodes are given (hence interpolating polynomial will be at most cubic). Using the programme, find Newton’s Interpolating polynomial that approximiates f(x)=2×3–x+3 and the nodes given are x0=-1, x1=0, x2=1 and x3=2. Use the polynomial to approximate value at x = 1.5. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],temp=1,f[10],sum,p; int i,n,j,k=0,c; clrscr(); printf(“nhow many record you will be enter: “); scanf(“%d”,&n); for(i=0; i<n; i++) { printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]);
  • 6. printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]); } printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p); for(i=0;i<n;i++) { temp = 1; k = i; for(j=0;j<n;j++) { if(k==j) { continue; } else { temp = temp * ((p-x[j])/(x[k]-x[j])); } } f[i]=y[i]*temp; } { sum = sum + f[i]; } printf(“nn f(%.1f) = %f “,p,sum); getch(); } Problem No. 6: Repeat Problem No. 5 for constructing Lagrangian Polynomial instead of Newton’s Polynomials. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10][10],sum,p,u,temp; int i,n,j,k=0,f,m; float fact(int); clrscr(); printf(“nhow many record you will be enter:”); scanf(“%d”,&n);
  • 7. for(i=0; i<n; i++) { printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]); printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[k][i]); } printf(“nnEnter X for finding f(x): “); scanf(“%f”,&p); for(i=1;i<n;i++) { k=i; for(j=0;j<n-i;j++) { y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]); k++; } } printf(“n_____________________________________________________n”); printf(“n x(i)t y(i)t y1(i) y2(i) y3(i) y4(i)”); printf(“n_____________________________________________________n”); for(i=0;i<n;i++) { printf(“n %.3f”,x[i]); for(j=0;j<n-i;j++) { printf(“ “); printf(“ %.3f”,y[j][i]); } printf(“n”); } i=0; do { if(x[i]<p && p<x[i+1]) k=1; else i++; }while(k != 1); f=i; sum=0; for(i=0;i<n-1;i++) {
  • 8. k=f; temp=1; for(j=0;j<i;j++) { temp = temp * (p - x[k]); k++; } sum = sum + temp*(y[i][f]); } printf(“nn f(%.2f) = %f “,p,sum); getch(); } Problem No.7: Write a programme that approximates the value of a definite integral ( )fb z f(x)dx using Trapezoidal Rule, with m sample points. Find an approximate value of the integral of f(x)=3x(cos2 x) over the interval [1, 6]. Solution: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); printf(“nhow many record you will be enter: “); scanf(“%d”,&n); for(i=0; i<n; i++) { printf(“nnenter the value of x%d: “,i); scanf(“%f”,&x[i]); printf(“nnenter the value of f(x%d): “,i); scanf(“%f”,&y[i]); } h=x[1]-x[0]; n=n-1; sum = sum + y[0]; for(i=1;i<n;i++) { sum = sum + y[i]; sum = sum * (h/2); printf(“nn I = %f “,sum);
  • 9. } getch(); } Problem No. 8: Write a program that solves Initial Value Problem (IVP) of order one:y’(x)=dy/dx=f(x,y) with initial condition at x=x0 as y(x0) = y0using Improved Euler’s Method. Using your program solve the IVP: y’=dy/dx= x2 + y2 − 2, y(0)=1. Compute upto 5 places of decimal. Solution: #include<stdio.h> #include<conio.h> float fun(float,float); void main() { int i,n; float x0,y0[10],h,e; clrscr(); printf(“enter the value of x0 and y0n”); scanf(“%f%f”,&x0,&y0[0]); printf(“enter the value of hn”); scanf(“%f”,&h); printf(“enter the value at which you want to findn”); scanf(“%f”,&e); n=(int)((e-x0)/h+0.5); for(i=0;i<n;i++) { y0[i+1]=y0[i]+h*fun(x0,y0[i]); x0=x0+h; } for(i=0;i<n;i++) printf(“%f”,y0[i]); getch(); } float fun(float x,float y) { float a=x*x+y*y-2; return(a); }