Numerical analysis c++, Essays (university) of Mathematical Methods for Numerical Analysis and Optimization

Codes for numerical analysis equations c++

Typology: Essays (university)

2018/2019

Uploaded on 05/08/2019

Sherifmagdy
Sherifmagdy 🇪🇬

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bisection :
#include<iostream>
#include <math.h>
using namespace std;
float func(float x)
{
return(pow(x, 3) + x - 1);
}
float bisection(float XL, float XU, float error) {
if (func(XL)*func(XU) < 0) {
float XR = (XL + XU) / 2;
if (func(XR)*func(XL) > 0)
XL = XR;
else if (func(XR)*func(XL) < 0)
XU = XR;
else
return XR;
if (abs(XL - XU) * 100 > error) {
return bisection(XL, XU, error);
}
XR = (XL + XU) / 2;
return XR;
}
else {
exit(0);
}
}
void main() {
cout << bisection(0, 1, .5) << endl;
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Numerical analysis c++ and more Essays (university) Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Bisection :

#include

#include <math.h>

using namespace std;

float func(float x)

return(pow(x, 3) + x - 1);

float bisection(float XL, float XU, float error) {

if (func(XL)*func(XU) < 0) {

float XR = (XL + XU) / 2;

if (func(XR)*func(XL) > 0)

XL = XR;

else if (func(XR)*func(XL) < 0)

XU = XR;

else

return XR;

if (abs(XL - XU) * 100 > error) {

return bisection(XL, XU, error);

XR = (XL + XU) / 2;

return XR;

else {

exit(0);

void main() {

cout << bisection(0, 1, .5) << endl;

False Position

#include

#include <math.h>

using namespace std;

float func(float x)

return(x*log10(x) - 1.2);

float falsepo(float XL, float XU, float error) {

if (func(XL)*func(XU) < 0) {

float XR = XU - ((func(XU)*(XL - XU)) / (func(XL) - func(XU)));

if (func(XR)*func(XL) > 0)

XL = XR;

else if (func(XR)*func(XL) < 0)

XU = XR;

else

return XR;

if (func(XR) > error) {

return falsepo(XL, XU, error);

XR = XU - ((func(XU)*(XL - XU)) / (func(XL) - func(XU)));

return XR;

else {

cout << "error" << endl;

void main() {

cout << falsepo(2, 3, 0.002) << endl;

Newton :

#include

#include <math.h>

using namespace std;

float cal(float x)

float nor = - pow(x, 2) + 1.8*x + 2.5;

float der = - 2 * x + 1.8;

return (x - (nor / der));

float newton(float XO, float error) {

if (abs(XO - cal(XO)) * 100 > error)

return newton(cal(XO), error);

return XO;

void main() {

cout << newton(5, 0.05) << endl;

Gauss Elimination, Lu Decomposition and Cramer

#include using namespace std; float m21, m31, m32; //very important (global variables) void printMatrix(float matrix[][4], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << matrix[i][j] << "\t"; } cout << endl; } } void GaussElim(float matrix[3][4]) { cout << "\nsolve by Gauss Elimination\n"; cout << "The Entered Matrix \n"; printMatrix(matrix, 3, 4); m21 = matrix[1][0] / matrix[0][0]; m31 = matrix[2][0] / matrix[0][0]; cout << "M21 = " << m21 << endl; cout << "M31 = " << m31 << endl; //R2-m21R1-->R //R3-m31R1-->R for (int i = 0; i < 4; i++) { matrix[1][i] = matrix[1][i] - m21matrix[0][i]; matrix[2][i] = matrix[2][i] - m31matrix[0][i]; } m32 = matrix[2][1] / matrix[1][1]; cout << "M32 = " << m32 << endl; cout << endl; //R3-m32R2-->R for (int i = 0; i < 4; i++) { matrix[2][i] = matrix[2][i] - m32matrix[1][i]; } float x3, x2, x1; x3 = matrix[2][3] / matrix[2][2];

float x1, x2, x3; x3 = c3 / matrix[2][2]; x2 = (c2 - (x3matrix[1][2])) / matrix[1][1]; x1 = (c1 - (x3matrix[0][2]) - (x2*matrix[0][1])) / matrix[0][0]; cout << "X1= " << x1 << endl; cout << "X2= " << x2 << endl; cout << "X3= " << x3 << endl; cout << endl; } float det(float matrix[3][4]) { //det of any matrix float result = ( matrix[0][0] * (matrix[1][1] * matrix[2][2] - (matrix[1][2] * matrix[2][1])) - matrix[0][1] * ((matrix[1][0] * matrix[2][2]) - (matrix[1][2] * matrix[2][0])) + matrix[0][2] * ((matrix[1][0] * matrix[2][1]) - (matrix[1][1] * matrix[2][0])) ); return result; } void swappy(float &a, float &b) { //important to swap columns in A1,A2,A float temp = a; a = b; b = temp; } void Cramer(float matrix[3][4]) { float A1, A2, A3; cout << "A Matrix \n"; printMatrix(matrix, 3, 3); float A = det(matrix); cout << "\nDetriment of A = " << A << endl; //to find matrix A for (int i = 0; i < 3; i++) {//swap First column with the Last Column (output column - >b) swappy(matrix[i][0], matrix[i][3]); } cout << "Matrix of A1 \n"; printMatrix(matrix, 3, 3); A1 = det(matrix);

cout << "\n\nDetriment of A1 = " << A1 << endl; //to find matrix A for (int i = 0; i < 3; i++) {//swap second column with the Last Column (output column - >b) swappy(matrix[i][0], matrix[i][3]); //revert the changes we made in the first swap swappy(matrix[i][1], matrix[i][3]); //then swap second column with the Last Column (output column - >b) } cout << "Matrix of A2 \n"; printMatrix(matrix, 3, 3); A2 = det(matrix); cout << "\n\nDetriment of A2 = " << A2 << endl; //to find matrix A for (int i = 0; i < 3; i++) { swappy(matrix[i][1], matrix[i][3]);//revert the changes we made in the second swap swappy(matrix[i][2], matrix[i][3]);//then swap second column with the Last Column (output column) } cout << "Matrix of A3 \n"; printMatrix(matrix, 3, 3); A3 = det(matrix); cout << "\n\nDetriment of A3 = " << A3 << endl; //to find x1,x2,x float x1 = A1 / A; float x2 = A2 / A; float x3 = A3 / A; cout << "X1= " << x1 << endl; cout << "X2= " << x2 << endl; cout << "X3= " << x3 << endl; cout << endl; //:) } void main() { float matrix[3][4]; char c, option; do { cout << "Please enter the Augmented Matrix\n"; for (int i = 0; i < 3; i++) { cout << "enter " << i + 1 << " Row\n"; for (int j = 0; j < 4; j++) { cin >> matrix[i][j];