Sie sind auf Seite 1von 5

SOURCE CODE 

//Machine Problem No. 2 - Bisection & Secant Method


//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

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;
}

double bisect(double a, double b)


{
double c=(a+b)/2;
return c;
}

double interpolate(double a, double ya, double b, double yb)


{
double c=(b*ya-a*yb)/(ya-yb);
return c;
}

int main()
{
cout <<"Combined Bisection and Secant Method"<< endl<<endl;

double x1, x2, x3, y1, y2, y3;

cout<<"Enter lower boundary (a) : "; cin>>x1; cout<<endl;


cout<<"Enter upper boundary (b) : "; cin>>x2; cout<<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;
}


SOURCE CODE 
//Machine Problem No. 3 - Newton's Method for determining reciprocal
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
cout <<"Newton's method for determining reciprocal"<<endl<<endl;

double n, x0, x1, delta;


int iter=0;

cout<<"Enter a number (n) : "; cin>>n; cout<<endl;


cout<<"Enter initial value (x) : "; cin>>x0; cout<<endl<<endl;
//initial value must be close enough to the expected answer

do
{
x1=x0*(2-n*x0);
delta=x1-x0;
iter+=1;

cout<<iter<<" x = "<<x1<<endl;

x0=x1;
}
while(abs(delta)>0.000001);

cout<<endl<<"The reciprocal of "<<n<<" is "<<x0<<" .";

return 0;
}


SOURCE CODE 
//Machine Problem No. 2.4 - System of Nonlinear Equations
//code written by RCQuilala

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

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;

cout<<"Suggested initial values : {1.5,1.5,-1.5}"<<endl<<endl;


cout<<"Enter initial value of x1 : "; cin>>x[0]; cout<<endl;
cout<<"Enter initial value of x2 : "; cin>>x[1]; cout<<endl;
cout<<"Enter initial value of x3 : "; cin>>x[2]; cout<<endl<<endl;
cout<<"Suggested tolerance : 0.0001"<<endl<<endl;
cout<<"Enter value for tolerance : "; cin>>tol; cout<<endl<<endl;
//Use smaller value of tolerance for higher accuracy of solution.
do
{
double mx=0;
iter+=1;
xout[0]=2-x[1]-x[2];
xout[1]=sqrt(6-pow(x[0],2)-pow(x[2],2));
xout[2]=-2/(x[0]*x[1]);

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;
}


SOURCE CODE 
//Machine Problem No. 2.5 - Fixed-point Iteration Method
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

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();

double tol, x0, xc, xd, xn, delta;


int iter=0, i=0;

cout<<"Enter value for tolerance : "; cin>>tol; cout<<endl;


cout<<"Enter initial value of x : "; cin>>x0; cout<<endl<<endl;
//initial value must be close enough to the expected answer

double xc0=x0, xd0=x0, xn0=x0;

cout<<" "<<"Convergent Divergent No Solution"<<endl<<endl;

do
{
xc=convergent(xc0);
xd=divergent(xd0);
xn=no_solution(xn0);

iter+=1;
delta=xc-xc0;

cout<<iter<<fixed<<setprecision(6)<<" "<<xc<<" "<<xd<<" "<<xn<<endl;

xc0=xc;
xd0=xd;
xn0=xn;
}
while(abs(delta)>tol);

cout<<endl<<"The value of x is "<<xc0<<" .";

return 0;
}


SOURCE CODE 
//Machine Problem No. 2.6 - Laguerre's Method
//code written by RCQuilala - MSCE (S)

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

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;

double tol, x, y, yp, ydp, G, H, a, d1, d2, d, n=1;


int iter=0;

cout<<"Enter value for tolerance : "; cin>>tol; cout<<endl;


cout<<"Enter initial value of x : "; cin>>x; cout<<endl<<endl;
//initial value must be close enough to the expected answer

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);

cout<<endl<<"No. of iterations : "<<iter<<endl;


cout<<endl<<"The value of x is "<<x<<" .";

return 0;
}

Das könnte Ihnen auch gefallen