#include \"StdAfx.h\" #include \"iostream\" #include \"iomanip\" using namespace
ID: 3569081 • Letter: #
Question
#include "StdAfx.h"
#include "iostream"
#include "iomanip"
using namespace std;
using std::cout;
using std::endl;
using std::cin;
using std::setw;
#define ErrorTolerance 0.0001
#define MaxSteps 40
void Secant(double a, double b);
double rungekutta3(double t);
double f(double t, double x);
double g(double x);
void main()
{
// Initialize guesses 0.7 and 1.0
double g1 = 0.7;
double g2 = 1.0;
Secant(g(g1), g(g2));
//Press key function to terminate the program.
cout << " Press Any Key to Exit..." << endl;
cin.get();
}
double rungekutta3 (double x0)
{
double h = 0.025; // Initialize step size = 0.025.
double x1, x2, x3; // Runge-Kutta orders.
double t0 = 0.0;
//alpha
double a1 = 1.0 / 2.0;
double a2 = 1.0;
//beta
double b10 = 1.0/2.0;
double b20 = -1.0;
double b21 = 2.0;
//weights
double c0 = 1.0 / 6.0;
double c1 = 4.0 / 6.0;
double c2 = 1.0 / 6.0;
// Define y values.
double y[200];
y[0] = 0;
int i = 0;
cout << " h f(t,x) x1 x2 x3 y" << endl;
for (t0 = 0.0; t0 <= 1.0; t0 = t0 + h)
{
i=i+1;
//Calculate to 3rd orders of Runge-Kutta.
x1 = x0 + h * (b10 * f(t0,x0));
x2 = x0 + h * (b20 * f(t0,x0) + b21 * f(t0 + a1 * h, x0));
x3 = x0 + h * (c0 * f(t0,x0) + c1 * f(t0 + a1 * h, x0) + c2 * f(t0 + a2 * h, x0));
//Calculate y.
y[i] = y[i-1] + (1.00/6.00)*(c0*x1 + c1*x2 + c2*x3)*h;
cout << std::setprecision(4) << t0 << " " << std::setprecision(8) << f(t0,x0) << " " << x1 << " " << x2 << " " << x3 << " " << y[i] << " " << endl;
//Set step forward.
x0 = x3;
}
return x3;
}
//Method to return f(t,x) values.
double f(double t, double x)
{
return (x + 0.09*pow(x,2) + cos(10*t));
}
// Method to return the guess values.
double g(double x)
{
return (x + rungekutta3(x) - 3.0);
}
//Secant method.
void Secant(double a, double b)
{
int i;
double fa, fb, d, m;
fa = g(a);
fb = g(b);
//Print out the values of initial guesses and values of the guessed function.
cout << "x(0) = " << std::setprecision(6) << fa << " x(1) = " << std::setprecision(6) << fb << " x(0) + x(1) - 3 = " << std::setprecision(6) << fa + fb << endl;
//Loop runs 40 times.
for (i = 2; i <= MaxSteps; i++)
{
//Calculate d and f(b).
d = (b - a)/(fb - fa);
b = a;
fb = fa;
//Calculate new d.
d = d * fa;
m = 3 - a;
//Function to check if error tolerance is smaller than 10^-4 or 0.001, then print out the value.
if (fabs(d) < ErrorTolerance)
{
cout << "x(0) = " << std::setw(4) << a << " x(1) = " << std::setw(4) << m << " x(0) + x(1) - 3.0 = " << std::setw(4)<< (a + m) - 3.0 << endl;
cout << " Value is found at: " << a << " and satisfied to within a tolerance of " << ErrorTolerance << endl;
return;
}
//Calculate new a and f(a).
a = a - d;
fa = g(a);
//Print out current values.
cout << "x(0) = " << std::setw(4) << a << " x(1) = " << std::setw(4) << b << " x(0) + x(1) - 3 = " << std::setw(4) << 2*(a + b) - 3.0 << endl;
}
}
Explanation / Answer
public class SentimentAnalyser
{
public static void main(String arg[])
{
double g1 = 0.7;
double g2 = 1.0;
RungeKutta ob = new RungeKutta();
ob.Secant(ob.g(g1), ob.g(g2));
}
}
class RungeKutta
{
double ErrorTolerance,MaxSteps;
public RungeKutta()
{
ErrorTolerance = 0.0001;
MaxSteps = 40;
}
double rungekutta3 (double x0)
{
double h = 0.025; // Initialize step size = 0.025.
double x1=0, x2=0, x3=0; // Runge-Kutta orders.
double t0 = 0.0;
//alpha
double a1 = 1.0 / 2.0;
double a2 = 1.0;
//beta
double b10 = 1.0/2.0;
double b20 = -1.0;
double b21 = 2.0;
//weights
double c0 = 1.0 / 6.0;
double c1 = 4.0 / 6.0;
double c2 = 1.0 / 6.0;
// Define y values.
double y[] = new double[200];
y[0] = 0;
int i = 0;
System.out.println(" h f(t,x) x1 x2 x3 y");
for (t0 = 0.0; t0 <= 1.0; t0 = t0 + h)
{
i=i+1;
//Calculate to 3rd orders of Runge-Kutta.
x1 = x0 + h * (b10 * f(t0,x0));
x2 = x0 + h * (b20 * f(t0,x0) + b21 * f(t0 + a1 * h, x0));
x3 = x0 + h * (c0 * f(t0,x0) + c1 * f(t0 + a1 * h, x0) + c2 * f(t0 + a2 * h, x0));
//Calculate y.
y[i] = y[i-1] + (1.00/6.00)*(c0*x1 + c1*x2 + c2*x3)*h;
System.out.printf(" %.4f",t0);
System.out.printf(" %.8f",f(t0,x0));
System.out.printf(" %f %f %f %f %f %f",t0,f(t0,x0),x1,x2,x3,y[i]);
x0 = x3;
}
return x3;
}
//Method to return f(t,x) values.
double f(double t, double x)
{
return (x + 0.09*Math.pow(x,2) + Math.cos(10*t));
}
double g(double x)
{
return (x + rungekutta3(x) - 3.0);
}
//Secant method.
void Secant(double a, double b)
{
int i;
double fa, fb, d, m;
fa = g(a);
fb = g(b);
//Print out the values of initial guesses and values of the guessed function.
System.out.printf( "x(0) = %.5f x(1) = %.5f x(0) + x(1) - 3 = %.5f ", fa, fb,(fa + fb));
//Loop runs 40 times.
for (i = 2; i <= MaxSteps; i++)
{
//Calculate d and f(b).
d = (b - a)/(fb - fa);
b = a;
fb = fa;
//Calculate new d.
d = d * fa;
m = 3 - a;
//Function to check if error tolerance is smaller than 10^-4 or 0.001, then print out the value.
if (Math.abs(d) < ErrorTolerance)
{
System.out.printf( " x(0) = %.4f x(1) = %.4f x(0) + x(1) - 3.0 = %.4f",a,m, (a + m) - 3.0);
System.out.println( "Value is found at: "+ a +" and satisfied to within a tolerance of " +ErrorTolerance);
return;
}
//Calculate new a and f(a).
a = a - d;
fa = g(a);
//Print out current values.
System.out.printf( " x(0) = %.4f x(1) = %.4f x(0) + x(1) - 3 = ",a,b,2*(a + b) - 3.0);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.