Adv Math Prog
Adv Math Prog
/* Inputs */
cout<<"Enter initial guess: ";
cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
x1 = x0 - f0/g0;
step = step+1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
f1 = f(x1);
}while(fabs(f1)>e);
cout<< endl<<"Root is: "<< x1;
return 0;}
OUTPUT
*********************
Newton Raphson Method
*********************
Iteration-1: x1 = 0.614547 and f(x1) = 0.026607
Iteration-2: x1 = 0.607108 and f(x1) = 0.000023
Iteration-3: x1 = 0.607102 and f(x1) = -0.000000
Newton-Raphson’s Method
(Python Language)
# Defining Functiondef f(x):
return x**3 - 5*x - 9
# Defining derivative of functiondef g(x):
return 3*x**2 - 5
# Implementing Newton Raphson Method
def newtonRaphson(x0,e,N):
print('\n\n*** NEWTON RAPHSON METHOD IMPLEMENTATION ***')
step = 1
flag = 1
condition = True
while condition:
if g(x0) == 0.0:
print('Divide by zero error!')
break
x1 = x0 - f(x0)/g(x0)
print('Iteration-%d, x1 = %0.6f and f(x1) = %0.6f' % (step, x1, f(x1)))
x0 = x1
step = step + 1
if step > N:
flag = 0
break
if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')
# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')
# Converting x0 and e to float
x0 = float(x0)
e = float(e)
# Converting N to integer
N = int(N)
#Note: You can combine above three section like this# x0 = float(input('Enter Guess: '))# e =
float(input('Tolerable Error: '))# N = int(input('Maximum Step: '))
# Starting Newton Raphson Method
newtonRaphson(x0,e,N)
OUTPUT
Enter Guess: 2
Tolerable Error: 0.000001
Maximum Step: 10
#include<iostream>#include<iomanip>#include<math.h>
/*
Defining equation to be solved.
Change this equation to solve another problem.
*/
#define f(x) cos(x) - x * exp(x)
using namespace std;
int main(){
/* Declaring required variables */
float x0, x1, x, f0, f1, f, e;
int step = 1;
/* Inputs */
up:
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
if( f0 * f < 0)
{
x1 = x;
}
else
{
x0 = x;
}
step = step + 1;
}while(fabs(f)>e);
cout<< endl<< Root is: "<< x<< endl;
return 0;}
OUTPUT
****************
Bisection Method
****************
Iteration-1: x = 0.500000 and f(x) = 0.053222
Iteration-2: x = 0.750000 and f(x) = -0.856061
Iteration-3: x = 0.625000 and f(x) = -0.356691
Iteration-4: x = 0.562500 and f(x) = -0.141294
Iteration-5: x = 0.531250 and f(x) = -0.041512
Iteration-6: x = 0.515625 and f(x) = 0.006475
Iteration-7: x = 0.523438 and f(x) = -0.017362
Iteration-8: x = 0.519531 and f(x) = -0.005404
Iteration-9: x = 0.517578 and f(x) = 0.000545
Iteration-10: x = 0.518555 and f(x) = -0.002427
Iteration-11: x = 0.518066 and f(x) = -0.000940
Iteration-12: x = 0.517822 and f(x) = -0.000197
Iteration-13: x = 0.517700 and f(x) = 0.000174
Iteration-14: x = 0.517761 and f(x) = -0.000012
Iteration-15: x = 0.517731 and f(x) = 0.000081
Iteration-16: x = 0.517746 and f(x) = 0.000035
Iteration-17: x = 0.517754 and f(x) = 0.000011
Iteration-18: x = 0.517757 and f(x) = -0.000000
step = step + 1
condition = abs(f(x2)) > e
# Input Section
x0 = input('First Guess: ')
x1 = input('Second Guess: ')
e = input('Tolerable Error: ')
# Converting input to float
x0 = float(x0)
x1 = float(x1)
e = float(e)
#Note: You can combine above two section like this# x0 = float(input('First Guess: '))# x1 =
float(input('Second Guess: '))# e = float(input('Tolerable Error: '))
# Checking Correctness of initial guess values and bisectingif f(x0) * f(x1) > 0.0:
print('Given guess values do not bracket the root.')
print('Try Again with different guess values.')else:
bisection(x0,x1,e)
OUTPUT
First Guess: 2
Second Guess: 3
Tolerable Error: 0.00001
#include<iostream>#include<conio.h>
using namespace std;
int main(){
float x[100], y[100], xp, yp=0, p;
int i,j,n;
/* Input Section */
cout<<"Enter number of data: ";
cin>>n;
cout<<"Enter data:"<< endl;
for(i=1;i<=n;i++)
{
cout<<"x["<< i<<"] = ";
cin>>x[i];
cout<<"y["<< i<<"] = ";
cin>>y[i];
}
cout<<"Enter interpolation point: ";
cin>>xp;
return 0;}
OUTPUT
# Lagrange Interpolation
# Importing NumPy Libraryimport numpy as np
# Reading number of unknowns
n = int(input('Enter number of data points: '))
# Making numpy array of n & n x n size and initializing # to zero for storing x and y value
along with differences of y
x = np.zeros((n))
y = np.zeros((n))
p=1
for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])
yp = yp + p * y[i]
# Displaying outputprint('Interpolated value at %.3f is %.3f.' % (xp, yp))
OUTPUT
#include <iostream>
#include <cmath>
double f(double x) {
return sin(x); // define the function to differentiate
}
int main() {
double x = 1.0; // point at which to evaluate the derivative
double h = 0.001; // step size
double dfdx = central_diff(x, h); // approximate the derivative
cout << "The derivative of f(x) at x = " << x << " is " << dfdx << endl;
return 0;
}
OUTPUT
The Derivative of f(x) at x = 1 is
0.540302
Numerical Differentiation
(Python Language)
import numpy as np
import matplotlib.pyplot as plt
def g(x):
return np.exp(-2*x)
x=
2*np.pi f
= np.cos
derivative = diff(f, x, dx)
print('The approximate derivative of cos(x) at x = 2*pi is:
{0:.5f}. The error is {1:.5f}.'
.format(derivative, abs(derivative - 0)))
x=1
f = np.log
derivative = diff(f, x, dx)
print('The approximate derivative of ln(x) at x = 0 is: {0:.5f}. The error is {1:.5f}.'
.format(derivative, abs(derivative - 1)))
OUTPUT
The approximate derivative of exp(x) at x = 0 is: 1.000017. The error is 0.000017.
The approximate derivative of exp(-2*x) at x = 0 is: -2.00013. The error is 0.00013.
The approximate derivative of cos(x) at x = 2*pi is: 0.00000. The error is 0.00000.
The approximate derivative of ln(x) at x = 0 is: 1.00003
Numerical Integration
(C++ Language)
#include<stream>
#include<math.h>
/* Input */
cout<<"Enter lower limit of integration: ";
cin>>lower;
cout<<"Enter upper limit of integration: ";
cin>>upper;
cout<<"Enter number of sub intervals: ";
cin>>subInterval;
/* Calculation */
return 0;
OUTPUT
Numerical Integration
(Python Language)
# Trapezoidal Method
# Finding sum
integration = f(x0) + f(xn)
for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)
return integration
# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))
OUTPUT