Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cmath>
- #include <iostream>
- #include <string>
- #include <fstream>
- struct SResult {
- double m_result;
- bool m_correctness;
- SResult(double result, bool correct){
- m_result = result;
- m_correctness = correct;
- }
- bool isCorrect(){
- return m_correctness;
- }
- };
- double inputFunction(double x, int fooNo){
- if(fooNo == 0)
- return pow(x,6)+(3*pow(x,5))-(101*pow(x,4))-(15*pow(x,3))+(2836*pow(x,2))-(7044*x)+4320;
- else if(fooNo == 1)
- return (4*pow(x,6))+(8*pow(x,3))-(11*x)-9;
- else if(fooNo == 2)
- return x-3*sin(x)+6;
- else if(fooNo == 3)
- return sin(1.f/x);
- else
- return x;
- }
- double inputFunctionDervievative(double x, int fooNo){
- if(fooNo == 0)
- return (6*pow(x,5))+(15*pow(x,4))-(404*pow(x,3))-(45*pow(x,2))+(5672*x)-7044;
- else if(fooNo == 1)
- return (24*pow(x,5))+(24*pow(x,2))-11;
- else if(fooNo == 2)
- return 1 - 3*cos(x);
- else if(fooNo == 3)
- return -(cos(1/x)/pow(x,2));
- else
- return 1;
- }
- bool asymptoteAt(double point, int fooNo){
- double precision = 0.005;
- bool firstCondition = inputFunction(point-precision, fooNo) <= inputFunction(point, fooNo);
- bool secondCondition = inputFunction(point, fooNo) <= inputFunction(point+precision, fooNo);
- bool thirdCondition = inputFunction(point-precision, fooNo) >= inputFunction(point, fooNo);
- bool fourthCondition = inputFunction(point, fooNo) >= inputFunction(point+precision, fooNo);
- if((firstCondition && secondCondition) || (thirdCondition && fourthCondition))
- return true;
- else
- return false;
- }
- SResult bisect(double intervalBeginInput, double intervalEndInput, int maxIterations, double precision, int fooNo){
- SResult tmp(0,false);
- std::string filename;
- filename = "bisect";
- filename += (char)(fooNo+'1');
- filename += ".txt";
- std::fstream output;
- output.open(filename,std::ios::app);
- output.precision(10);
- output << "Metoda bisekcji dla funkcji nr " << fooNo << " z dokładnością " << precision << " na przedziale <" << intervalBeginInput << "," << intervalEndInput << ">" << std::endl;
- double error = precision + 1;
- int actualIteration = 0;
- std::vector<double> estimations;
- std::vector<double> functionValues;
- std::vector<double> differences;
- double intervalCenter;
- double x;
- double centerValue;
- double fxValue;
- double intervalBegin = intervalBeginInput;
- double intervalEnd = intervalEndInput;
- while(actualIteration < maxIterations && error > precision){
- actualIteration++;
- intervalCenter = (intervalBegin + intervalEnd) / 2;
- x = intervalCenter;
- centerValue = inputFunction(x, fooNo);
- estimations.push_back(x);
- functionValues.push_back(centerValue);
- x = intervalBegin;
- if(centerValue*inputFunction(x, fooNo) > 0)
- intervalBegin = intervalCenter;
- else
- intervalEnd = intervalCenter;
- error = fabs(intervalEnd - intervalBegin);
- differences.push_back(error);
- }
- output << "Ilosc iteracji\t" << actualIteration << std::endl;
- output << "Wynik\t" << estimations[estimations.size()-1] << std::endl;
- output.close();
- if((estimations[estimations.size()-1] + error) != intervalEndInput){
- tmp.m_correctness = true;
- tmp.m_result = estimations[estimations.size()-1];
- }
- return tmp;
- }
- SResult newton(double x0, double b, int maxIterations, double precision, int fooNo){
- SResult tmp(0, false);
- std::string filename;
- filename = "newton";
- filename += (char)(fooNo+'1');
- filename += ".txt";
- std::fstream output;
- output.open(filename,std::ios::app);
- output.precision(10);
- output << "Metoda stycznych dla funkcji nr " << fooNo << " z dokładnością " << precision << " od x0 = " << x0 << std::endl;
- double error = precision + 1;
- int actualIteration = 0;
- std::vector<double> estimations;
- std::vector<double> functionValues;
- std::vector<double> differences;
- double x = x0;
- double dfx;
- estimations.push_back(x);
- double functionValue = inputFunction(x, fooNo);
- functionValues.push_back(functionValue);
- double xn;
- bool noError = true;
- while (actualIteration < maxIterations && error > precision && noError){
- x = estimations[actualIteration];
- dfx = inputFunctionDervievative(x, fooNo);
- if(dfx == 0){
- printf("Błąd! Pochodna równa zero\n");
- noError = false;
- }
- else {
- xn = x - functionValues[actualIteration]/dfx;
- error = fabs(xn - x);
- differences.push_back(error);
- x = xn;
- estimations.push_back(x);
- functionValue = inputFunction(x, fooNo);
- functionValues.push_back(functionValue);
- if(x > b || x < x0){
- noError = false;
- }
- }
- actualIteration++;
- }
- output << "Ilosc iteracji\t" << actualIteration << std::endl;
- output << "Wynik\t" << estimations[estimations.size()-1] << std::endl;
- output.close();
- if(noError)
- tmp.m_correctness = true;
- tmp.m_result = estimations[estimations.size()-1];
- return tmp;
- }
- int main(){
- std::cout.precision(20);
- double prec = 1e-6;
- double intervalStart = -10.0;
- double intervalEnd = 6.0;
- int iters = 1000;
- int fooNo = 0;
- //SResult bisectResult = bisect(0.2,0.3,1000,prec, 1), newtonResult = newton(0.2,0.3,1000,prec, 1);
- //if(bisectResult.isCorrect())
- // std::cout << bisectResult.m_result << std::endl;
- //if(newtonResult.isCorrect())
- // std::cout << newtonResult.m_result << std::endl;
- //std::cout << bisect(-2.0,0.5,1000,prec, 1) << std::endl;
- //std::cout << newton(-2.0,1000,prec, 1) << std::endl;
- //std::cout << bisect(-7.5,-4.5,1000,prec, 2) << std::endl;
- //std::cout << newton(-7.5,1000,prec, 2) << std::endl;
- bool result = true;
- std::vector<SResult> results;
- SResult tmp(0,false);
- double lastResult = intervalStart;
- while (result){
- tmp = bisect(lastResult, intervalEnd, iters, prec, fooNo);
- if(tmp.isCorrect()){
- results.push_back(tmp);
- lastResult = tmp.m_result;
- }
- result = tmp.m_correctness;
- }
- if(!results.empty()){
- std::cout << "Znaleziono nastepujace pierwiastki za pomoca metody bisekcji:\n";
- for(std::vector<SResult>::iterator it = results.begin(); it != results.end(); it++)
- std::cout << (*it).m_result << std::endl;
- }
- else
- std::cout << "Nie znaleziono zadnych pierwiastkow w zadanym przedziale przy uzyciu metody bisekcji" << std::endl;
- result = true;
- results.clear();
- lastResult = intervalStart;
- while (result){
- tmp = newton(lastResult, intervalEnd, iters, prec, fooNo);
- if(tmp.isCorrect()){
- results.push_back(tmp);
- lastResult = tmp.m_result + 0.001;
- }
- result = tmp.m_correctness;
- }
- if(!results.empty()){
- std::cout << "Znaleziono nastepujace pierwiastki za pomoca metody stycznych:\n";
- for(std::vector<SResult>::iterator it = results.begin(); it != results.end(); it++)
- std::cout << (*it).m_result << std::endl;
- }
- else
- std::cout << "Nie znaleziono zadnych pierwiastkow w zadanym przedziale przy uzyciu metody stycznych" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment