0% found this document useful (0 votes)
5 views5 pages

Computer Programming Oel Abdullah

The document outlines two programming problems involving numerical methods in C++. The first problem implements Halley's method to find roots of a cubic polynomial, while the second problem calculates the sine of an angle using a series expansion. Both problems include pre-coding plans and complete code implementations.

Uploaded by

faisal4700445
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)
5 views5 pages

Computer Programming Oel Abdullah

The document outlines two programming problems involving numerical methods in C++. The first problem implements Halley's method to find roots of a cubic polynomial, while the second problem calculates the sine of an angle using a series expansion. Both problems include pre-coding plans and complete code implementations.

Uploaded by

faisal4700445
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/ 5

COMPUTER PROGRAMMING OEL

EE-163

PROBLEM 1
PRE CODING PLANS
Pre-Program Plan for Halley's Method Code:

• Define a function f(x) to evaluate the cubic polynomial .

• Define f_prime(x) for the first derivative of the polynomial.

• Define f_double_prime(x) for the second derivative.

• Implement halleys_method() which:

o Accepts coefficients and initial guess.

o Uses a loop to update the guess using Halley's formula until the desired
precision is reached.

o Returns the calculated root.

• In main(), take input from the user and put in halleys method.

CODE
#include <iostream>

#include <cmath>

using namespace std;

double f(double x, double a, double b, double c, double d) {

return a*x*x*x + b*x*x + c*x + d;

// First derivative f'(x)

Page | 1
double f_prime(double x, double a, double b, double c) {

return 3*a*x*x + 2*b*x + c;

// Second derivative f''(x)

double f_double_prime(double x, double a, double b) {

return 6*a*x + 2*b;

// Halley's Method implementation

double halleys_method(double a, double b, double c, double d, double x0, double


precision = 0.0001) {

double x1;

do {

double fx = f(x0, a, b, c, d);

double f1x = f_prime(x0, a, b, c);

double f2x = f_double_prime(x0, a, b);

x1 = x0 - (2 * fx * f1x) / (2 * f1x * f1x - fx * f2x);

if (fabs(x1 - x0) < precision)

break;

x0 = x1;

} while (true);

return x1;

int main() {

double a, b, c, d, x0;

cout << "Enter coefficients a, b, c, d: ";

Page | 2
cin >> a >> b >> c >> d;

cout << "Enter initial guess: ";

cin >> x0;

double root = halleys_method(a, b, c, d, x0);

cout << "Root of the equation: " << root << endl;

return 0;

IMAGES

Page | 3
PROBLEM 2
PRE CODING PLANS
• Input: Value of angle x in radians.
• Initialize: First term as x, sum as x, start n=1.
• Iterate: Compute next term using the previous term to avoid recomputation of
powers and factorials.
• Terminate: When |term| < 0.0001.

CODE
#include <iostream>

#include <cmath>

using namespace std;

int main() {

double x, term, sum;

int n = 1;

const double EPS = 0.0001;

cout << "Enter angle in radians: ";

cin >> x;

term = x; // First term of the series

sum = x; // Initialize sum with the first term

// Keep adding terms until the change is very small

while (fabs(term) > EPS) {

Page | 4
term *= -1 * x * x / ((2 * n) * (2 * n + 1));

sum += term;

n++;

cout << "sin(" << x << ") = " << sum << endl;

return 0;

IMAGES

Page | 5

You might also like