Write a code in C/MATLAB/Python to implement Regula Falsi Method and use it to find a real
root of 3x-cos(x)-1.0=0 correct upto 4 decimal Places.
Answers
Answer:
Follow me
Mark me Brainliest
Thank me
Explanation:
Regula Falsi method, also known as the false position method, is the oldest approach to find the real root of a function. It is a closed bracket method and closely resembles the bisection method.
The C Program for regula falsi method requires two initial guesses of opposite nature. Like the secant method, interpolation is done to find the new values for successive iterations, but in this method one interval always remains constant.
The programming effort for Regula Falsi or False Position Method in C language is simple and easy. The convergence is of first order and it is guaranteed. In manual approach, the method of false position may be slow, but it is found superior to the bisection method.
Features of Regula Falsi Method:
Type – closed bracket
No. of initial guesses – 2
Convergence – linear
Rate of convergence – slow
Accuracy – good
Programming effort – easy
Approach – interpolation
Below is a short and simple source code in C program for regula falsi method to find the root of cos(x) – x*e^x. Here, x0 and x1 are the initial guesses taken.
Variables:
itr – a counter which keeps track of the no. of iterations performed
maxmitr – maximum number of iterations to be performed
x0, x1 – the limits within which the root lies
x2 – the value of root at nth iteration
x3 – the value of root at (n+1)th iteration
allerr – allowed error
x – value of root at nth iteration in the regula function
f(x0), f(x1) – the values of f(x) at x0 and x1 respectively
f(x) = cos(x) – x*e^x
Source Code for Regula Falsi Method in C:
C Program for Regula Falsi Method Source Code
C
#include<stdio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<stdio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}
Explanation:
The Regula-Falsi Method" uses two initial approximations {x0 , x1} to solve a given equation y = f(x).In this method the function f(x) , is approximated by a secant line, whose equation is from the two initial approximations supplied.The secant line then intersects the X - Axis at third point {x2} .
Upto this point both "The Secant Method" and the "The Regula-Falsi Method" are exactly same. But in the next step, in "The Regula-Falsi Method" we use the "Intermediate Value Theorem" to check whether the "Zero" of the equation y = f(x) lies in the interval {x0,x1} or {x1,x2}.
After the Interval has been found, "The Regula-Falsi Method " is again applied on those two points.
The script proceeds in the same way and performs upto 100 iterations. The Accuracy required (required no. of decimal places) is taken as input from the user. The error between solutions of each iteration is checked every time and if found less than required accuracy, the iterations are stopped.
mark me the brainliest