Source Codes
Source Codes
#include <iostream>
#include <iomanip>
#include <cmath>
double function(double x)
{
double y=82944*pow(x,10)-118656*pow(x,8)+60292*pow(x,6)-13133*pow(x,4)+1189*pow(x,2)-36;
//The given eqn. is an even function. Hence, the value of y is the same for (+) & (-) x.
return y;
}
int main()
{
cout <<"Combined Bisection and Secant Method"<< endl<<endl;
y1=function(x1);
y2=function(x2);
cout<<"f(a) = "<<y1<<" "<<"f(b) = "<<y2<<" "<<"f(a)*f(b) = "<<y1*y2<<endl<<endl;
if(y1*y2<=0)
{
cout<<"Root exists."<<endl<<endl;
int root=0;
while(root<4)
{
do
{
x3=interpolate(x1,y1,x2,y2);
y3=function(x3);
//cout<<"c = "<<x3<<" "<<"f(c) = "<<y3<<endl;
y2=y3;
x2=x3;
}
while(abs(y3)>0.0001);
cout<<fixed<<setprecision(4)<<"x = "<<x3<<" & "<<-1*x3<<endl;
x2=bisect(x1,x2);
y2=function(x2);
root++;
}
}
else
{
cout<<"No root exists within the specified interval.";
}
return 0;
}
1
SOURCE CODE
//Machine Problem No. 3 - Newton's Method for determining reciprocal
//code written by RCQuilala - MSCE (S)
#include <iostream>
#include <cmath>
int main()
{
cout <<"Newton's method for determining reciprocal"<<endl<<endl;
do
{
x1=x0*(2-n*x0);
delta=x1-x0;
iter+=1;
cout<<iter<<" x = "<<x1<<endl;
x0=x1;
}
while(abs(delta)>0.000001);
return 0;
}
2
SOURCE CODE
//Machine Problem No. 2.4 - System of Nonlinear Equations
//code written by RCQuilala
#include <iostream>
#include <iomanip>
#include <cmath>
void text ()
{
cout<<"Using Fixed Point Iteration Method,"<<endl;
cout<<"solve for x1, x2, & x3."<<endl<<endl;
}
void solution ()
{
double x[3], dist[3], xout[3];
double tol, Linf;
int iter=0;
for(int i=0;i<3;i++)
{
dist[i]=abs(xout[i]-x[i]);
if(dist[i]>=mx)
{
mx=dist[i];
}
else
{
mx=mx;
}
}
Linf=mx;
for(int i=0;i<3;i++)
{
x[i]=xout[i];
}
cout<<iter<<fixed<<setprecision(5)<<" x1 = "<<x[0];
cout<<" x2 = "<<x[1]<<" x3 = "<<x[2]<<" Linf = "<<Linf<<endl;
}
while(Linf>tol);
cout<<endl<<"---RESULT---"<<endl<<endl;
cout<<"No. of iterations : "<<iter<<endl<<endl;
cout<<fixed<<setprecision(3)<<"x1 = "<<x[0]<<endl;
cout<<"x2 = "<<x[1]<<endl;
cout<<"x3 = "<<x[2]<<endl;
}
int main()
{
text ();
solution ();
return 0;
}
3
SOURCE CODE
//Machine Problem No. 2.5 - Fixed-point Iteration Method
//code written by RCQuilala - MSCE (S)
#include <iostream>
#include <cmath>
#include <iomanip>
void text()
{
cout <<"Fixed-point Iteration Method"<<endl<<endl;
}
double convergent(double x)
{
double y=log10(4-x);
return y;
}
double divergent(double x)
{
double y=pow(10,x)+2*x-4;
return y;
}
double no_solution(double x)
{
double y=sqrt(4*x-x*pow(10,x));
return y;
}
int main()
{
text();
do
{
xc=convergent(xc0);
xd=divergent(xd0);
xn=no_solution(xn0);
iter+=1;
delta=xc-xc0;
xc0=xc;
xd0=xd;
xn0=xn;
}
while(abs(delta)>tol);
return 0;
}
4
SOURCE CODE
//Machine Problem No. 2.6 - Laguerre's Method
//code written by RCQuilala - MSCE (S)
#include <iostream>
#include <cmath>
#include <iomanip>
double function(double x)
{
double p=pow(x,pow(x,3))-10*x+1;
return p;
}
double prime(double x)
{
double pprime=pow(x,pow(x,3)+2)*(3*log(x)+1)-10;
return pprime;
}
double doubleprime(double x)
{
double pdprime=pow(x,pow(x,3)+1)*(5+6*log(x))+pow(x,pow(x,3)+4)*(6*log(x)+1+9*pow(log(x),2));
return pdprime;
}
int main()
{
cout <<"Laguerre's Method for finding root"<<endl<<endl;
do
{
y=function(x);
yp=prime(x);
ydp=doubleprime(x);
G=yp/y;
H=pow(G,2)-ydp/y;
d1=G+sqrt((n-1)*(n*H-pow(G,2)));
d2=G-sqrt((n-1)*(n*H-pow(G,2)));
if(abs(d1)>abs(d2))
{
d=d1;
}
else
{
d=d2;
}
a=n/d;
iter+=1;
x-=a;
//cout<<iter<<" x = "<<x<<endl;
}
while(abs(a)>tol);
return 0;
}
5