Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "stdafx.h"
- #define _USE_MATH_DEFINES
- #include <math.h>
- #include <iostream>
- #include <cmath>
- using namespace std;
- void falseposition();
- void newtonraphson();
- void nonlinear();
- int main()
- {
- cout << "choose the method" << endl;
- cout << "enter 1 for false position" << endl;
- cout << "enter 2 for modified" << endl;
- cout << "enter 3 for nonlinear" << endl;
- int choice;
- cin >> choice;
- if (choice == 1) falseposition();
- if (choice == 2) newtonraphson();
- if (choice == 3) nonlinear();
- switch(choice){
- case 1: falseposition(); break;
- case 2: newtonraphson(); break;
- case 3: nonlinear(); break;
- default: falseposition();
- }
- return 0;
- }
- void nonlinear(){
- double u, v, dux, duy, dvx, dvy, xi, yi, xr, yr;;
- cout << "enter xi" << endl;
- cin >> xi;
- cout << "enter yi" << endl;
- cin >> yi;
- for (int i = 1; i < 1000; i++)
- {
- u = 0.45 * cos(xi) - 0.28 * cos(yi) - 0.275;
- v = 0.45 * sin(xi) - 0.28 * sin(yi) - 0.13;
- dux = -0.45 * sin(xi);
- duy = 0.28 * sin(yi);
- dvx = 0.45 * cos(xi);
- dvy = -0.28 * cos(yi);
- xr = xi - ((u * dvy - v * duy) / (dux * dvy - duy * dvx));
- yr = yi - ((v * dux - u * dvx) / (dux * dvy - duy * dvx));
- xi = xr; yi = yr;
- }
- cout << "xr = " << xr << endl;
- cout << "yr = " << yr << endl;
- }
- void falseposition(){
- double tmpx, tmpy,xl, xu, fxl, fxu, xr, fxr, d = 0, error;
- int i;
- cout << "enter lower interval" << endl;
- cin >> tmpx;
- cout << "enter upper interval" << endl;
- cin >> tmpy;
- if (tmpx < tmpy) {
- xl = tmpx;
- xu = tmpy;
- }
- else {
- xl = tmpy;
- xu = tmpx;
- }
- for (i = 0; i < 10; i++){
- fxl = ((sin(2.0 * M_PI * xl / 16) * (cos(2 * M_PI * 12 * 48 / 16))) + pow(M_E, -xl)) - 0.8;
- fxu = ((sin(2.0 * M_PI * xu / 16) * (cos(2 * M_PI * 12 * 48 / 16))) + pow(M_E,-xu)) - 0.8;
- if ((fxl * fxu) < 0){
- xr = xu - (fxu * (xl - xu) / (fxl - fxu));
- fxr = ((sin(2.0 * M_PI * xr / 16) * (cos(2.0 * M_PI * 12 * 48 / 16))) + pow(M_E, -xr)) - 0.8;
- error = (xr - d) / xr;
- d = xr;
- if ((fxl * fxr) < 0){
- xu = xr;
- }
- else if ((fxl * fxr) > 0){
- xl = xr;
- }
- else if(fxr==0) {
- cout << "xr =" << xr << endl;
- cout << "error = " << error * 100 << "%" << endl;
- break;
- }
- }
- else {
- cout << "error!" << endl;
- break;
- }
- }
- if (i == 10) {
- cout << "xr = " << xr << endl;
- cout << "error = " << error * 100 << "%" << endl;
- }
- }
- void newtonraphson(){
- double fx, fpx, fdpx, x, xr,fxr;
- cout<< "Enter the guess value" <<endl;
- cin >> x;
- for (int i = 1; i < 1000; i++) {
- fx = pow((pow(M_E, x) + x - 20), 3);
- fpx = (pow((pow(M_E, x) + x - 20), 2)) * (3 * pow(M_E, x) + 3);
- fdpx = (pow(M_E, x) + x - 20) * (pow(M_E, x) + 1) * ((3 * pow(M_E, x) + 3)) + (pow((pow(M_E, x) + x - 20), 2)) * (3 * pow(M_E, x));
- xr = x - ((fx * fpx) / (fpx * fpx - fx * fdpx));
- x = xr;
- }
- cout << "xr = " << xr << endl;
- cout<< (fx > -0.000000001 && fx < 0.00000000001)?"converges":"diverges";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement