Question

In: Computer Science

Write the c++ program of Secant Method and Fixed point Method in a one program using...

Write the c++ program of Secant Method and Fixed point Method in a one program using switch condition .Like :

cout<<"1.Secant Method \n 2. Fixed point Method\n"<<endl; if press 1 then work Secant Method if press 2 then work Fixed point Method .so please writhe the code in c++ using switch case.and the equation given down consider the equation in the given.Note: Must showt the all the step of output all the iteration step shown in the program .in program must shown all the step of iteration result

for secant method equation :4x3-2x-6=0

for fixed point=x2-x-1=0

Please in every itaration must be shown the output then finally Got the root

Solutions

Expert Solution

Secant Method

#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define f(x) 4*x*x*x - 2*x - 6

using namespace std;

int main()
{
   float x0, x1, x2, f0, f1, f2, e;
   int step = 1, N;
cout<< setprecision(4)<< fixed;
   cout<<"Enter first guess: ";
   cin>>x0;
   cout<<"Enter second guess: ";
   cin>>x1;
   cout<<"Enter tolerable error: ";
   cin>>e;
   cout<<"Enter maximum iteration: ";
   cin>>N;  
   do
   {
       f0 = f(x0);
       f1 = f(x1);
       if(f0 == f1)
       {
           cout<<"Mathematical Error.";
           exit(0);
       }

       x2 = x1 - (x1 - x0) * f1/(f1-f0);
       f2 = f(x2);

       cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<< setw(10)<< f(x2)<< endl;

       x0 = x1;
       f0 = f1;
       x1 = x2;
       f1 = f2;

       step = step + 1;
   }while(fabs(f2)>e);

   cout<< endl<<"Root is: "<< x2;

   return 0;
}

Fixed point Method

#include<stdio.h>
#include<math.h>
#include<iostream>
#define g(x) (x+1)/(x)
using namespace std;
int main()
{
int k=0;
float x1,x0;
float eps = 1e-5;

cout<<"Enter the initial guess x0: ";
cin>>x0;
x1=x0;
do
{
k++;
x0=x1;
x1=g(x0);
cout<<"One root is obtained at:"<<k<<" is \t"<<x1;
cout<<"\n";
}while(fabs(x1-x0)>eps);
cout<<"Root is:" <<x0;
return 0;
}


Related Solutions

Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for...
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations.
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all...
Write one a MATLAB function that implements the Bisection method, Newton’s method and Secant Method (all in one function). Your function must have the following signature function output = solve(f,options) % your code here end where the input is • f: the function in f(x) =0. options: is a struct type with the following fields o method: bisection, newton or secant tol: the tolerance for stopping the iterations. maximum_iterations: the maximum number of iterations allowed. initial_guess: that is P_0; if...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make...
in Java, write a program for methods (Secant, False-Position and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations. (a) f(x) = 2x3 – 11.7x2 + 17.7x – 5 This function has 3 +ve roots, all of which lie between 0 and 4. Find the roots. Implement the...
Using C++ Write One one single program with two or more functioncallsWrite a C++...
Using C++ Write One one single program with two or more function callsWrite a C++ function, smallest Index, that takes as parameters an int array and its size and returns the index of the smallest element in the array. Also the program should test the function.Write another function that prompts the user to input a string and outputs the string in uppercase letters. You must use a character array to store the string.
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0,...
In MATLAB write a function secant.m to apply the secant method. function [x,i] = secant(f, x0, x1, tol, maxiters) [x,i] = secant(f, x0, x1, tol, maxiters) performs the secant method with f(x), starting at x_0 = x0 and x_1 = x1, and continuing until either |x_i+1 - x_i| <= tol, or maxiters iterations have been taken. The number of iterations, i, is also returned. An error is raised if the first input is not a function handle. A warning is...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Derive the Secant Method using a linear approximation and prove its convergence.
Derive the Secant Method using a linear approximation and prove its convergence.
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT