0% found this document useful (0 votes)
12 views4 pages

False Position and Newton Raphson Method

The document contains C++ implementations of two numerical methods: the False Position Method and the Newton-Raphson Method for finding roots of equations. The first part implements the False Position Method for the equation x^2 - x - 2, while the second part implements the Newton-Raphson Method for the equation x^2 - 3x + 2. Each method includes functions to calculate the root and handle user input.

Uploaded by

imransarker.web
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

False Position and Newton Raphson Method

The document contains C++ implementations of two numerical methods: the False Position Method and the Newton-Raphson Method for finding roots of equations. The first part implements the False Position Method for the equation x^2 - x - 2, while the second part implements the Newton-Raphson Method for the equation x^2 - 3x + 2. Each method includes functions to calculate the root and handle user input.

Uploaded by

imransarker.web
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Code:

#include <iostream>
using namespace std;
const int P = 5; // Number of processes
const int R = 3; // Number of resources
int available[R];
int maxMatrix[P][R];
int allocation[P][R];
int need[P][R];

// Function to calculate Need matrix


void calculateNeed() {
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = maxMatrix[i][j] - allocation[i][j];
}
}
}

// Safety Algorithm
bool isSafeState() {
bool finish[P] = {false};
int work[R];
for (int i = 0; i < R; i++) work[i] = available[i];

int safeSequence[P];
int count = 0;

while (count < P) {


bool found = false;
for (int i = 0; i < P; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < R; j++) {
if (need[i][j] > work[j]) break;
}
if (j == R) {
for (int k = 0; k < R; k++) work[k] +=
allocation[i][k];
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) return false;
}

cout << "System is in a safe state. Safe sequence: ";


for (int i = 0; i < P; i++) cout << safeSequence[i] << " ";
cout << endl;
return true;
}

int main() {
cout << "Enter Allocation Matrix: \n";
for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
cin >> allocation[i][j];
}
}

cout << "Enter Max Matrix: \n";


for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
cin >> maxMatrix[i][j];
}
}

cout << "Enter Available Resources: \n";


for (int i = 0; i < R; i++) {
cin >> available[i];
}

calculateNeed();

if (!isSafeState()) {
cout << "System is in an unsafe state!" << endl;
}
return 0;
}Output:
False Position Method Implementation in C++ (Equation: x^2 - x - 2 = 0)

Code:
#include <iostream>
#include <cmath>
using namespace std;

#define EPSILON 1e-6 // Tolerance

// Function f(x) = x^2 - x - 2


double f(double x) {
return x * x - x - 2;
}

// False Position Method


void falsePosition(double x1, double x2) {
if (f(x1) * f(x2) >= 0) {
cout << "Incorrect initial values." << endl;
return;
}
double x_new;
while (fabs(x2 - x1) >= EPSILON) {
x_new = x1 - (f(x1) * (x2 - x1)) / (f(x2) - f(x1));
if (fabs(f(x_new)) < EPSILON)
break;
else if (f(x_new) * f(x1) < 0)
x2 = x_new;
else
x1 = x_new;
}
cout << "Root found by False Position Method: " << x_new << endl;
}

int main() {
double x1 = 1, x2 = 3;
cout << "Applying False Position Method...\n";
falsePosition(x1, x2);
return 0;
}
Output:
False Position Method Implementation in C++ (Equation: x^2 - x - 2 = 0)

#include <iostream>
#include <cmath>
using namespace std;

#define EPSILON 1e-6 // Tolerance

// Function f(x) = x^2 - 3x + 2


double f(double x) {
return x * x - 3 * x + 2;
}

// Derivative of f(x), f'(x) = 2x - 3


double df(double x) {
return 2 * x - 3;
}

// Newton-Raphson Method
void newtonRaphson(double x) {
double h = f(x) / df(x);
while (fabs(h) >= EPSILON) {
h = f(x) / df(x);
x = x - h;
}
cout << "Root found by Newton-Raphson Method: " << x << endl;
}

int main() {
double x0 = 2.5;
cout << "Applying Newton-Raphson Method...\n";
newtonRaphson(x0);
return 0;
}
Output:

You might also like