0% found this document useful (0 votes)
20 views14 pages

CM File

Uploaded by

nimitbharti235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

CM File

Uploaded by

nimitbharti235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Experiment 4

Aim : program for finding area under curve ∫ 𝑑x/(1 + x)10 by Trapezoidal rule .

Theory :
∫ a f(x) dx = Area Under the Curve = (h/2) [y0 + 2 (y1 + y2 + y3 + ….. + yn-1) + y n] Where
, y0, y1, y2, …. Y n , are the values of function at x = 1, 2, 3, ….., n respectively h=(b-a)/n

Procedure/code:
#include<iostream>
using namespace std;
int i; int n = 6;
float h;
float ansh = 0;
float ans(float sum)
{ ansh = h * sum;
return ansh;}
float sum(float k)
{ float sum = 0; if((i
== 0) || (i == n))
{ sum += k / 2;} else{
sum += k;} return
ans(sum);}
void equation(float j){float y = (j + 1);
float k = (1 / y);
cout << "the value of X" << i << " = " << k << endl;
sum(k);}

int main()
{ cout<<"ByVipulBharti"<<endl
; float a, b; cout << "Enter
lower limit = ";
cin >> a; cout << "Enter upper
limit = "; cin >> b; h = (b - a) /
n; float j; for(i = 0; i < 7; i++)
{ j = static_cast<float>(i) / n;
equation(j); }
cout << "The final answer is = " << ansh << endl; return
0;
}
OUTPUT:-

Experiment 5
Aim : program for finding area under curve 01dx/(1+x) by Simpson's 1/3 Rule .
Theory :
∫ba f(x) dx = Area Under the Curve = (h/3) [y0 + 2 ( y2 + y4 + ….. )+4(y1+y3+……) + y n]
Where , y0, y1, y2, …. Y n , are the values of function at x = 1, 2, 3, ….., n respectively
h=(b-a)/n
Procedure/code:
#include<iostream>
using namespace std;
int n;
int i = 0; float suma =
0; float sumb = 0; float
sumc = 0; void
sum(float a,int i) {
if ((i == 0) || (i == n)) { suma
= suma + a;
}

else if ((i % 2) != 0) { sumb = sumb + a;


}
else {sumc = sumc + a;
}
}

void equation(float k,int i) { float a = 1 / (1 + k); sum(a,i);


}
int main() {
cout << "By Vipul Bharti" << endl;
float a, b;
cout << "Enter lower limit = ";
cin >> a;
cout << "Enter upper limit = ";
cin >> b;
cout << "Enter number of intervals in which you want to break it = ";
cin >> n;
float h = ((b - a) / n);

for (i = 0; i <= n; i++)


{ float j =i * h;
equation(j,i);
}
float sim;
sim = (h / 3) * (suma + 4 * sumb + 2 * sumc);
cout << sim;
return 0;
}
OUTPUT:-

Experiment 6
Aim : program for finding area under curve ∫ 𝑑𝑑/(1 + 𝑑)10 by Simpson's 3/8 Rule .
Theory :
∫ 𝑓(𝑥)𝑑(𝑥)𝑏𝑎= Area Under the Curve = (h)(3/8) [yo+yn] + 3 (y1 + y2 + y4+y5 + ….. +y n-1)
+ 2(y3+y6+ y9 + ….. + yn-3)] Where , y0, y1, y2, …. Y n , are the values of function at x = 1,
2, 3, ….., n respectively h=(b-a)/n

Procedure/code :
#include<iostream>
using namespace std;
int n;
int i=0; float suma=0;
float sumb=0; float
sumc=0; void
sum(float a,int i){
if(i==0||i==n){
suma=suma+a;
}
else if(i%3==0)
{ sumb=sumb+a;
}
else{
sumc=sumc+a;
}
}
void equation(float k,int i)
{ float a=1/(1+k);
sum(a,i);
}
int main(){
cout<<"By Vipul Bharti "<<endl;
float a,b;
cout<<"Enter lower limit = ";
cin>>a;
cout<<"Enter Upper limit = ";
cin>>b;
cout<<"enter number of intervals in which you want to break it = ";
cin>>n; float h=(b-a)/n; for(i=0;i<=n;i++){ float j=i*h;
equation(j,i);
}
float sumd;
sumd=h*(suma+2*sumb+3*sumc);
cout<<(3*sumd)/8;
return 0;
}
OUTPUT:-

Experiment- 7
AIM:-To write a C++ program to implement Newton’s Divided Difference Formula for
estimating value of f(x).
Algorithm:
1. Start
2. Input the number of observations.
3. Input all the values of x.
4. Input all the values of y corresponding to the given values of x.
5. Calculate the divided difference table.
6. Put the values in “Newton’s Divided Difference Formula”.
7. Print the calculated value.
8. Stop

Practical Code:
#include<iostream> using namespace
std; float proterm(int i, float value, float
x[]) { float pro = 1; for (int j = 0; j < i; j+
+) { pro = pro * (value - x[j]);
}
return pro;
}
void dividedDiffTable(float x[], float y[][10], int n) {
for (int i = 1; i < n; i++) { for (int j = 0; j < n - i; j++) {
y[j][i] = ( y[j][i - 1] - y[j + 1][i - 1] ) / (x[j] - x[i + j] );
}
}
}
float applyFormula(float value, float x[], float y[][10], int n) {
float sum = y[0][0]; for (int i = 1; i < n; i++) { sum = sum +
(proterm(i, value, x) * y[0][i]);
}
return sum;
}
int main() { cout<<"By Vipul
Bharti "<<endl; int n;
float k, sum, y[10][10]; cout<<"Enter the
no. of observations : "; cin>>n; float
x[n];

cout<<"Enter the different values of x\n";


for(int i=0; i<n; i++) { cin >> x[i];
}
cout<<"Enter the corresponding values of y\n";
for(int i=0; i<n; i++) { cin >> y[i][0];
}

cout << "Enter the value of 'k' for f(k) evaluation : ";
cin >> k;

dividedDiffTable(x, y, n); cout << "\nValue at " << k << " is "<<
applyFormula(k, x, y, n) << endl; return 0;
}
Output:

Experiment-8
AIM:-To write a C++ program for finding the inverse of a system of linear equations using
Gauss Jordan method.

Algorithm:
1. Start
2. Read Order of Matrix (n).
3. Read Matrix (A) of Order (n).
4. Augment and Identity Matrix of Order n to Matrix A.
5. Apply Gauss Jordan Elimination on Augmented Matrix (A).
6. Perform Row Operations to Convert the Principal Diagonal to 1.
7. Display the Inverse Matrix.
8. Stop.

Practical Code:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define SIZE 10 using
namespace std;
int main() {
cout<<"By Vipul Bharti "<<endl;

float a[SIZE][SIZE], x[SIZE], ratio;


int i,j,k,n;
cout<< setprecision(3)<< fixed;
cout<<"Enter order of matrix: "; cin>>n;
cout<<"Enter coefficients of Matrix: " << endl;
for(i=1;i<=n;i++) { for(j=1;j<=n;j++) {
cout<<"a["<< i<<"]["<< j<<"]= ";
cin>>a[i][j];
}
}
cout<< endl<<"\n\nGiven Matrix is:"<< endl;
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) cout<< a[i][j]<<"\t";
cout<< endl;
}
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++) { if(i==j)
a[i][j+n] = 1;
else a[i][j+n] = 0;
}
}
for(i=1;i<=n;i++) {
if(a[i][i] == 0.0) {
cout<<"Mathematical Error!";
exit(0); }
for(j=1;j<=n;j++)
{ if(i!=j) { ratio =
a[j][i]/a[i][i];
for(k=1;k<=2*n;k++) a[j][k] = a[j][k] - ratio*a[i][k];
}
}
}
for(i=1;i<=n;i++) {
for(j=n+1;j<=2*n;j++) a[i][j] = a[i][j]/a[i][i];
}
cout<< endl<<"Inverse Matrix is:"<< endl;
for(i=1;i<=n;i++) { for(j=n+1;j<=2*n;j++)
cout<< a[i][j]<<"\t"; cout<< endl;
}
return(0);
}
Output:

Experiment-9
AIM:-To write a C++ program for finding the solution of equations using Gauss
Elimination method.

Algorithm:
1. Start
2. Read Number of Unknowns: n
3. Read Augmented Matrix (A) of n by n+1 Size
4. Transform Augmented Matrix (A) to Upper Triangular Matrix by Row Operations.
5. Obtain Solution by Back Substitution.
6. Display Result.
7. Stop

Practical Code:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define SIZE 10 using
namespace std;
int main() {
cout<<"By Vipul Bharti "<<endl;
float a[SIZE][SIZE], x[SIZE], ratio;
int i,j,k,n;
cout<< setprecision(3)<< fixed;
cout<<"Enter number of unknowns: ";
cin>>n;
cout<<"Enter Coefficients of Augmented Matrix: "<< endl;
for(i=1;i<=n;i++) { for(j=1;j<=n+1;j++) { cout<<"a["<< i<<"]
["<< j<<"]= ";
cin>>a[i][j];
}
}
for(i=1;i<=n-1;i++) {
if(a[i][i] == 0.0) {
cout<<"Mathematical Error!";
exit(0);
}
for(j=i+1;j<=n;j++)
{ ratio = a[j][i]/a[i][i];
for(k=1;k<=n+1;k++) {
a[j][k] = a[j][k] -
ratio*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--) { x[i]
= a[i][n+1];
for(j=i+1;j<=n;j++) {
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
cout<< endl<<"Solution: "<< endl;
for(i=1;i<=n;i++) {
cout<<"x["<< i<<"] = "<< x[i]<< endl;
}
return 0;
}
Output:-

Experiment-10
AIM: To write a C++ program for solving Ordinary Differential Equations by Runge-Kutta
method.

Algorithm:
1. Start
2. Define function f(x,y)
3. Read values of initial condition(x0 and y0), number of steps (n) and calculation point
(xn)
4. Calculate step size (h) = (xn - x0)/n
5. Set i=0
6. Loop

k1 = h * f(x0, y0)
k2 = h * f(x0+h/2, y0+k1/2)
k3 = h * f(x0+h/2, y0+k2/2)
k4 = h * f(x0+h, y0+k3)
k = (k1+2*k2+2*k3+k4)/6

yn = y0 + k
i = i+1
x0=x0+h
y0=yn

While i < n

7.Display yn as result

8.Stops

Practical Code:
#include <iostream>
#include <iomanip>
#define f(x,y) (x*x+y*y)/10
using namespace std;
int main() {
cout<<"By Vipul Bharti "<<endl;
float x0, y0, xn, h, yn, k1, k2, k3, k4, k;
int i, n;
cout<<"Enter Initial Condition"<< endl;
cout<<"x0 = ";
cin>> x0;
cout<<"y0 = ";
cin >> y0;
cout<<"Enter calculation point xn = ";
cin>>xn;
cout<<"Enter number of steps:”;
cin>> n;
h = (xn-x0)/n;
cout<<setw(5); cout<<"\nx0\t\t\
ty0\t\t\tyn\n";
cout<<"------------------------------\n";
for(i=0; i < n; i++) {
k1 = h * (f(x0, y0));
k2 = h * (f((x0+h/2), (y0+k1/2)));
k3 = h * (f((x0+h/2), (y0+k2/2)));
k4 = h * (f((x0+h), (y0+k3)));
k=(k1+2*k2+2*k3+k4)/6;
yn = y0 + k;
cout<< setw(5) ;
cout << setprecision(6);
cout<< x0<<"\t\t"<< y0<<"\t\t"<< yn<< endl;
x0 = x0+h;
y0 = yn;
}
cout<<"\nValue of y at x = "<< xn<< " is " << yn;
return 0;
}
OUTPUT:

You might also like