kamilosxd678

MN04

May 13th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. struct SResult {
  8.     double m_result;
  9.     bool m_correctness;
  10.     SResult(double result, bool correct){
  11.         m_result = result;
  12.         m_correctness = correct;
  13.     }
  14.     bool isCorrect(){
  15.         return m_correctness;
  16.     }
  17. };
  18.  
  19. double inputFunction(double x, int fooNo){
  20.     if(fooNo == 0)
  21.         return pow(x,6)+(3*pow(x,5))-(101*pow(x,4))-(15*pow(x,3))+(2836*pow(x,2))-(7044*x)+4320;
  22.     else if(fooNo == 1)
  23.         return (4*pow(x,6))+(8*pow(x,3))-(11*x)-9;
  24.     else if(fooNo == 2)
  25.         return x-3*sin(x)+6;
  26.     else if(fooNo == 3)
  27.         return sin(1.f/x);
  28.     else
  29.         return x;
  30. }
  31.  
  32. double inputFunctionDervievative(double x, int fooNo){
  33.     if(fooNo == 0)                                         
  34.         return (6*pow(x,5))+(15*pow(x,4))-(404*pow(x,3))-(45*pow(x,2))+(5672*x)-7044;
  35.     else if(fooNo == 1)
  36.         return (24*pow(x,5))+(24*pow(x,2))-11;
  37.     else if(fooNo == 2)
  38.         return 1 - 3*cos(x);
  39.     else if(fooNo == 3)
  40.         return -(cos(1/x)/pow(x,2));
  41.     else
  42.         return 1;
  43. }
  44.  
  45. bool asymptoteAt(double point, int fooNo){
  46.     double precision = 0.005;
  47.     bool firstCondition = inputFunction(point-precision, fooNo) <= inputFunction(point, fooNo);
  48.     bool secondCondition = inputFunction(point, fooNo) <= inputFunction(point+precision, fooNo);
  49.     bool thirdCondition = inputFunction(point-precision, fooNo) >= inputFunction(point, fooNo);
  50.     bool fourthCondition = inputFunction(point, fooNo) >= inputFunction(point+precision, fooNo);
  51.     if((firstCondition && secondCondition) || (thirdCondition && fourthCondition))
  52.         return true;
  53.     else
  54.         return false;
  55. }
  56.  
  57. SResult bisect(double intervalBeginInput, double intervalEndInput, int maxIterations, double precision, int fooNo){
  58.     SResult tmp(0,false);
  59.     std::string filename;
  60.     filename = "bisect";
  61.     filename += (char)(fooNo+'1');
  62.     filename += ".txt";
  63.     std::fstream output;
  64.     output.open(filename,std::ios::app);
  65.     output.precision(10);
  66.     output << "Metoda bisekcji dla funkcji nr " << fooNo << " z dokładnością " << precision << " na przedziale <" << intervalBeginInput << "," << intervalEndInput << ">" << std::endl;
  67.     double error = precision + 1;
  68.     int actualIteration = 0;
  69.     std::vector<double> estimations;
  70.     std::vector<double> functionValues;
  71.     std::vector<double> differences;
  72.     double intervalCenter;
  73.     double x;
  74.     double centerValue;
  75.     double fxValue;
  76.     double intervalBegin = intervalBeginInput;
  77.     double intervalEnd = intervalEndInput;
  78.     while(actualIteration < maxIterations && error > precision){
  79.         actualIteration++;
  80.         intervalCenter = (intervalBegin + intervalEnd) / 2;
  81.         x = intervalCenter;
  82.         centerValue = inputFunction(x, fooNo);
  83.         estimations.push_back(x);
  84.         functionValues.push_back(centerValue);
  85.         x = intervalBegin;
  86.         if(centerValue*inputFunction(x, fooNo) > 0)
  87.             intervalBegin = intervalCenter;
  88.         else
  89.             intervalEnd = intervalCenter;
  90.         error = fabs(intervalEnd - intervalBegin);
  91.         differences.push_back(error);
  92.     }
  93.     output << "Ilosc iteracji\t" << actualIteration << std::endl;
  94.     output << "Wynik\t" << estimations[estimations.size()-1] << std::endl;
  95.     output.close();
  96.     if((estimations[estimations.size()-1] + error) != intervalEndInput){
  97.         tmp.m_correctness = true;
  98.         tmp.m_result = estimations[estimations.size()-1];
  99.     }
  100.     return tmp;
  101. }
  102.  
  103. SResult newton(double x0, double b, int maxIterations, double precision, int fooNo){
  104.     SResult tmp(0, false);
  105.     std::string filename;
  106.     filename = "newton";
  107.     filename += (char)(fooNo+'1');
  108.     filename += ".txt";
  109.     std::fstream output;
  110.     output.open(filename,std::ios::app);
  111.     output.precision(10);
  112.     output << "Metoda stycznych dla funkcji nr " << fooNo << " z dokładnością " << precision << " od x0 = " << x0 << std::endl;
  113.     double error = precision + 1;
  114.     int actualIteration = 0;
  115.     std::vector<double> estimations;
  116.     std::vector<double> functionValues;
  117.     std::vector<double> differences;
  118.     double x = x0;
  119.     double dfx;
  120.     estimations.push_back(x);
  121.     double functionValue = inputFunction(x, fooNo);
  122.     functionValues.push_back(functionValue);
  123.     double xn;
  124.     bool noError = true;
  125.     while (actualIteration < maxIterations && error > precision && noError){
  126.         x = estimations[actualIteration];
  127.         dfx = inputFunctionDervievative(x, fooNo);
  128.         if(dfx == 0){
  129.             printf("Błąd! Pochodna równa zero\n");
  130.             noError = false;
  131.         }
  132.         else {
  133.             xn = x - functionValues[actualIteration]/dfx;
  134.             error = fabs(xn - x);
  135.             differences.push_back(error);
  136.             x = xn;
  137.             estimations.push_back(x);
  138.             functionValue = inputFunction(x, fooNo);
  139.             functionValues.push_back(functionValue);
  140.             if(x > b || x < x0){
  141.                 noError = false;
  142.             }
  143.         }
  144.         actualIteration++;
  145.     }
  146.     output << "Ilosc iteracji\t" << actualIteration << std::endl;
  147.     output << "Wynik\t" << estimations[estimations.size()-1] << std::endl;
  148.     output.close();
  149.     if(noError)
  150.         tmp.m_correctness = true;
  151.     tmp.m_result = estimations[estimations.size()-1];
  152.     return tmp;
  153. }
  154.  
  155. int main(){
  156.     std::cout.precision(20);
  157.     double prec = 1e-6;
  158.     double intervalStart = -10.0;
  159.     double intervalEnd = 6.0;
  160.     int iters = 1000;
  161.     int fooNo = 0;
  162.     //SResult bisectResult = bisect(0.2,0.3,1000,prec, 1), newtonResult = newton(0.2,0.3,1000,prec, 1);
  163.     //if(bisectResult.isCorrect())
  164.     //  std::cout << bisectResult.m_result << std::endl;
  165.     //if(newtonResult.isCorrect())
  166.     //  std::cout << newtonResult.m_result << std::endl;
  167.     //std::cout << bisect(-2.0,0.5,1000,prec, 1) << std::endl;
  168.     //std::cout << newton(-2.0,1000,prec, 1) << std::endl;
  169.     //std::cout << bisect(-7.5,-4.5,1000,prec, 2) << std::endl;
  170.     //std::cout << newton(-7.5,1000,prec, 2) << std::endl;
  171.     bool result = true;
  172.     std::vector<SResult> results;
  173.     SResult tmp(0,false);
  174.     double lastResult = intervalStart;
  175.     while (result){
  176.         tmp = bisect(lastResult, intervalEnd, iters, prec, fooNo);
  177.         if(tmp.isCorrect()){
  178.             results.push_back(tmp);
  179.             lastResult = tmp.m_result;
  180.         }
  181.         result = tmp.m_correctness;        
  182.     }
  183.     if(!results.empty()){
  184.         std::cout << "Znaleziono nastepujace pierwiastki za pomoca metody bisekcji:\n";
  185.         for(std::vector<SResult>::iterator it = results.begin(); it != results.end(); it++)
  186.             std::cout << (*it).m_result << std::endl;
  187.     }
  188.     else
  189.         std::cout << "Nie znaleziono zadnych pierwiastkow w zadanym przedziale przy uzyciu metody bisekcji" << std::endl;
  190.  
  191.     result = true;
  192.     results.clear();
  193.     lastResult = intervalStart;
  194.     while (result){
  195.         tmp = newton(lastResult, intervalEnd, iters, prec, fooNo);
  196.         if(tmp.isCorrect()){
  197.             results.push_back(tmp);
  198.             lastResult = tmp.m_result + 0.001;
  199.         }
  200.         result = tmp.m_correctness;        
  201.     }
  202.     if(!results.empty()){
  203.         std::cout << "Znaleziono nastepujace pierwiastki za pomoca metody stycznych:\n";
  204.         for(std::vector<SResult>::iterator it = results.begin(); it != results.end(); it++)
  205.             std::cout << (*it).m_result << std::endl;
  206.     }
  207.     else
  208.         std::cout << "Nie znaleziono zadnych pierwiastkow w zadanym przedziale przy uzyciu metody stycznych" << std::endl;
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment