You are on page 1of 13

# Assignment 3

2014CH10131
24 August 2015

## 1 Lab sheet3 : Computational Algorithms

Write programs to do the following in a language of your choice:

## 2. Solution of two simultaneous linear algebraic equations without worrying

about the accuracy of the results.

## 5. Summation of first n terms of series approximation to ln(1 + x) where

ln(1 + x) = x x2 /2 + x3 /3 x4 /4 + ................

## S (x) = 1/px+q ( a1 x + a2 x2 + a3 x3 +.................)

2 Fibonacci
2.1 Algorithm
To generate the fibonacci series the algorithm is

1
1.input the number of terms say n
2.assign the 1st term as a=0
3. assign the second term as b=1;
4.now replace the next term (c) as sum of last two term (a+b)
5.a=b
6.b=c
do same for n-2 times

## 2.2 Code 1 (Loop Method)

#include<math.h>
#include <fstream>

## using namespace std;

//MAIN MODULE-//
int main ()
{ ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n,a,b,c,i,sum;
//INPUT MODULE//
myfile>>n; //NO. OF TERMS
//SETUP MODULE//
a=0; //initializing series//
b=1;
out<<a<< ;
out<<b<< ;
sum=1;
//WORK AND OUTPUT MODULE//
for (i=2;i<n;i++)
{
c=a+b; //updating c//
out<<c<< ;
sum+=c; //summation of series//

## a=b; //updateing a//

b=c; // updateing b//

out<<sum=<sum<<
return 0;

2
}

## 2.3 Code 2 (Recursion)

#include <iostream>
#include< fstream >
using namespace std;
int fibonacci(int);
int main(void);
int count;
int sum=0;
out <<Sum upto which term u want ;
myfile >> count; for (int i = 0; i <count; i++)
{
sum = sum + fibonacci(i)
out<<sum=<<sum;
return 0;
}
int fibonacci(int num)
{
if (num == 1)
{
return 1;
}
else if (num == 0)

{
return 0;
}
else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}

2.4 Analysis
loop method is better than recursion.it han n computation while recursion has
3n .

3
3 Linear Algebric Equations
3.1 Algorithm
1. Read the six numbers a, b, c, p, q, and r and assign them to the variables of
the corresponding name, respectively
2. Evaluate (r pc/a) and assign it to s (a new variable).
3. Evaluate (q pb/a)) and assign it to t (another new variable).
4. Check whether t is zero or not.
5. If t = 0 then stop solving and make an unsuccessful exit as value of y can not
be evaluated. Skip further instructions to go to Instruction 9 (Stop). In fact,
if t = 0 then we do not have two separate equations but only one equation. If
eq.(1) is same as eq.(2)
aq=pb
16. Evaluate (s/t) and assign it to y (a new variable).
7. Evaluate (c by) /a and assign it to x (a new variable).
8. Successful exit with values of x and y.

## 3.2 Code 1 (Substitution method)

#include<iostream>
#include<math.h>
#include <fstream>

## using namespace std;

//-MAIN MODULE//
int main
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

float a,b,c,p,q,r,x,y;
//INPUT MODULE//
myfile>>a>>b>>c>>p>>q>>r; //coffi. of equa-
tions//
//SETUP MODULE//
float s;
s=p*b-a*q; //-WORK MODULE//
if (s!=0)

4
//checking condition for solution//
{
y=(p*c-a*r)/s;
x=(-c-b*y)/a;
//OUTPUT MODULE//
out<<x=<<x<<endl;
out<<y=<<y<<endl;
}
else
out<<no answer;
return 0;
}

## 3.3 Code 2 (Matrix method)

#include< iostream >
#include< fstream >
using namespace std;
//MAIN MODULE-//
int main()
{
{ ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);
float a1, b1, c1, a2, b2, c2,x,y;
//INPUT MODULE//
// coefficients of equations//
out << Enter a1= << endl;
myfile >> a1;
out<< Enter b1= <<endl;
myfile>> b1;
out << Enter c1=<< endl;
myfile >> c1;
out << Enter a2= << endl;
myfile a2;
out<< Enter b2= << endl;
myfile >> b2;
out << Enter c2= << endl;
myfile>> c2;
//SETUP AND WORK MODULE-//

## if ((a1 * b2) - (b1 * a2) == 0) // checking

denominater//
{

5
out <<The system has no solution. << endl;
}
else
{
x = ((c1*b2) - (b1*c2))/((a1*b2)-(b1*a2));
y = ((a1*c2) - (c1*a2)) / ((a1*b2) - (b1*a2));
//OUTPUT MODULE//
out << x= <<x << y=<< y << endl;
}
return 0;
}

3.4 Analysis
For two variables we see that substitution method is good than matrix method
. But when the number of variables is more matrix method will dominate
the substitution method . In matrix method the only limitation is that the
determinant of coefficient of variables must be non-zero .

4 Sin (x)
4.1 Algorithm
1. Read x, n
2. S = x
3. term = x
4. For i = 1 to i = n 1
begin
5. term =
term(x2 /2i(2i + 1))
6. Sum =Sum + term
end
7. write Sum
8. stop

4.2 Code 1
#include<iostream>
#include<math.h>
#include <fstream>

## using namespace std;

//MAIN MODULE-//

6
int main ()
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n;
float x,p,sum,i;
//INPUT MODULE//
myfile>>x; //given value of x where sum has
to be determine//
myfile>>n; //no. of terms//
//-SETUP MODULE//
p=x; //initializing term//
sum=x; //initializing sum//
//WORK MODULE//
for(i=1;i<n;i++)
{
p=(-1*pow(x,2)*p)/((2*i+1)*2*i); //updating term//
sum+=p;} //updating sum //
//-OUTPUT MODULE-//
out<<sum;
return 0;
}

4.3 Code 2
#include< iostream >
#include< fstream >
#include < math.h >
using namespace std;
//SETUP MODULE-//
int factorial(int n)
{

## if (n==0){ return 1;}

return n*factorial(n-1);
}
//MAIN MODULE//
int main()
float x;
//INPUT MODULE//

7
out<<x=;
myfile>>x; //given value of x where sum has
to be determine//
x = x*0.0174444444444 ;
int m;
out<<Number of terms=;
myfile>>m;
float sum=0;
int l=1;
//-WORK MODULE//
for (int i=1;i<=m;i+=2)
{
sum+=(k*(powf(x,i)/factorial(i)));
l=l*(-1);
}
//OUTPUT MODULE-//
out<<series sum is equal :<<sum<<endl;
return 0;

4.4 Analysis
Method 1 is more promising than the Method 2 because in code 2 we are using
recursion technique to get the factorial which is increasing our order as well as
time complexity and when we make extra function to call time comlexity always
increase . But for large number of terms method 2 dominate method 1 . Same
conditions for the following Cosine(X) calculation.

5 Cos (x)
5.1 Algorithm
1. Read x, n
2. S = 1
3. term = 1
4. For i = 1 to i = n 1
begin
5. term =
term(x2 /(2i + 2)(2i + 1))
6. Sum =Sum + term
end
7. write Sum
8. stop

8
5.2 Code
#include<iostream>
#include<math.h>
#include <fstream>

## using namespace std;

//-MAIN MODULE//
int main ()
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n;
float x,p,sum,i;
//INPUT MODULE//
myfile>>x; //given value of x where sum has
to be determine//
myfile>>n; //no. of terms//
//SETUP MODULE//
p=1; //initializing term//
sum=1; //initializing sum//

//WORK MODULE//
for(i=1;i<n-1;i++)
{
p=(-1*pow (x,2)*p)/((2*i+1)*(2*i+2)); //updat-
ing term//
sum+=p; //updating SUM//
//OUTPUT MODULE//
out<<sum;
return 0;
}

6 ln(1+x)
6.1 Algorithm
1. Read x, n
2. S = x

9
3. term = x
4. For i = 1 to i = n 1
begin
5. term =
term(x/i(i + 1))
6. Sum =Sum + term
end
7. write Sum
8. stop

6.2 Code
#include<iostream>
#include<math.h>
#include <fstream>

## using namespace std;

//MAIN MODULE//
int main ()
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n;
float x,p,sum,i;
//-INPUT MODULE//
myfile>>x; //given value of x where sum has
to be//
myfile>>n; //no. of terms//
//SETUP MODULE//
p=x; //initializing term//
sum=x; //initializing sum//
//WORK MODULE-//
for(i=1;i<n;i++)
{
p=(-1*x*p*i)/(i+1); //updating term//
sum+=p; //updating sum//
//OUTPUT MODULE//
out<<sum;
return 0;
}

10
7 Summation of a given series
7.1 Algorithm
1. Read x, n,p,q,a1 , a2 , a3 , ..........an
2. S = 0
3.define an r=1/(px+q)
4. For i = 1 to i = n 1
begin

end
7.writeSum
8.stop

## 7.2 Code 1 (Direct method)

#include<iostream>
#include<math.h>
#include <fstream>

## using namespace std;

//MAIN MODULE-//
int main ()
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n,i;
float x,p,q,sum,r;

//-INPUT MODULE//
myfile>>x; //given value of x where sum has
to be//
myfile>>n; //no. of terms//
myfile>>p>>q; //given values of p and q// float
a[n];
for(i=0;i<n;i++)

11
{

## myfile>>a[i]; //coefficients of series//

}
{
//SETUP MODULE//
r=1/(p*x+q);
sum=0; //initializing sum//
//WORK MODULE//
for (i=0;i<n;i++)
{
sum+=a[i]*pow(x,i+1)*r; //updating sum//

//OUTPUT MODULE//
out<<sum; //output sum//
return 0;
}

## 7.3 Code 2 (Horners method)

#include < iostream >
#include< fstream>
using namespace std;
//-MAIN MODULE//
int main()
{

ifstream myfile;
myfile.open (a.txt);
ofstream out;
out.open (b.txt);

int n,p,q;
//INPUT MODULE-//
out<<No. of terms;
myfile>>n; //no. of terms//
int a[n];
for (int i = n-1; i = 0; i)
{
myfile>>a[i]; //coefficients of series// }
out<<p=;
myfile>>p; //given value of p// out<<q=;
myfile>>q;
//given value of q// int x;

12
out<<Enter x=;
myfile>>x; //given value of x where sum has
to be determine//
//SETUP MODULE-//
float g;
g= (p*x) + q;
int sum = a[0]; //initializing summation//
//-WORK MODULE//
for( int j=1; jn; j++)
{
sum = sum*x + a[j]; //updating sum//
}
float k;
k=sum*x;
//OUTPUT MODULE//
out<<Result=<<k/g; //output//
return 0;
}

7.4 Analysis
code 1 is based upon direct method(Brute Force) and the code 2 describes the
Hornors method. horners is better than brute force.It is very efficient way to
evaluate the value of polynomial at any given point and the time complexity of
horners algorithm is also low in comparison of Brute Force method.

13