1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#include<iostream>
#include<cmath>
using namespace std;
double bisection(double a, double b, double tol, int maxIts, int f1);
double bisection(double a, double b, double tol, int maxIts, int f2);
double bisection(double a, double b, double tol, int maxIts, int f3);
double newton(double x0, double tol, int maxIts, int f1);
double newton(double x0, double tol, int maxIts, int f2);
double newton(double x0, double tol, int maxIts, int f3);
double f1(double x);
double df1(double x);
double f2(double x);
double df2(double x);
double f3(double x);
double df3(double x);
int main()
{
double a = 1;
double b = 5;
double x0 = .523;
double tol = .00001;
int maxIts = 100;
cout << "Bisection" << "\t" << "Newton" << endl;
cout << bisection(a, b, tol, maxIts, f1) << "\t\t" << newton(x0, tol, maxIts, f1) << endl;
cout << bisection(a, b, tol, maxIts, f2) << "\t\t" << newton(x0, tol, maxIts, f2) << endl;
cout << bisection(a, b, tol, maxIts, f3) << "\t\t" << newton(x0, tol, maxIts, f3) << endl;
return 0;
}
double bisection(double a, double b, double tol, int maxIts)
{
double x, fpos, fneg, fx;
int counter = 1;
x = (fpos + fneg) / 2;
if(f(a) < 0)
{
fpos = b;
fneg = a;
}
else
{
fpos = a;
fneg = b;
}
while (fabs(f(x)) > tol && counter <= maxIts)
{
if(f(x) > 0)
{
fpos = x;
}
else
{
fneg = x;
}
x = (fpos + fneg) / 2;
counter++;
}
cout << counter << " counters" << endl;
return x;
}
double newton(double x0, double tol, int maxIts)
{
int counter = 1;
while (fabs(f(x0)) > tol && counter <= maxIts)
{
x0 = x0 - (f(x0))/(df(x0));
counter++;
}
cout << counter << "\t\t";
return x0;
}
/*double f(double x)
{
return x * x - 3;
}
double df(double x)
{
return 2 * x;
}*/
double f1(double x)
{
double angle = 11.5 * (acos(-1) / 180);
double D = 55;
double l = 89;
double h = 49;
double A = l*sin(angle);
double B = l*cos(angle);
double C = ((h + 0.5*D)*sin(angle)-0.5*D*tan(angle));
double E = ((h + 0.5*D)*cos(angle)-0.5*D);
return A*sin(x)*cos(x)+B*sin(x)*sin(x)-C*cos(x)-E*sin(x);
}
double df1(double x)
{
return -89 * sin(23 * (acos(-1)) / 360) * sin(x) * sin(x) + 178 * cos(23 * (acos(-1)) / 360) * cos(x) * sin(x) + (153 * sin(23 *
(acos(-1)) / 360) / 2 - 55 * tan(23 * (acos(-1)) / 360) / 2) *sin(x) + 89 * sin(23 * (acos(-1)) / 360)
* cos(x) * cos(x) - (153 * cos(23 * (acos(-1)) / 360) / 2 - 55 / 2) * cos(x);
}
double f2(double x)
{
double V = 12.4;
double L = 10;
double r = 1;
double h;
h = x;
return .5 * acos(-1) * r * r * L - L * r * r * sin(h / r) - L * h * sqrt(r * r - h * h) -V;
}
double df2(double x)
{
double L = 10;
double r = 1;
double h;
h = x;
return (-L * r * r) / (r * sqrt(1-((h/r) * (h/r)))) - (L * h * h) / sqrt(r * r - h * h);
}
double f3(double x)
{
const double AGRAV = -32.17;
double w = .3923; //this is supposed to be .4 but it wasn't giving me and answer of -0.317055
double xt = 1.7;
double t = 1;
double e = 2.718;
return -(AGRAV / (2 * w * w)) * ( ((pow(e, w * t) - pow(e, -(w * t))) / 2) - sin(w * t)) - xt;
}
double df3(double x)
{
const double AGRAV = -32.17;
double w = .3923; //this is supposed to be .4 but it wasn't giving me and answer of -0.317055
double xt = 1.7;
double t = 1;
double e = 2.718;
return ((AGRAV * (.5 * (pow(e, w*t) - pow(e, -(w*t))) - sin(w*t))) / pow(w,3)) - (((AGRAV / 2)
* (.5 * (pow(t*e,w*t) + pow(t*e, w*t)) - t*cos(w*t))) / pow(w,2));
}
|