A
A
adchabolda2021-11-13 17:59:01
C++ / C#
adchabolda, 2021-11-13 17:59:01

As for each x changing from a to b with step h, find the values ​​of the function Y(x), the sum S(x) and |Y(x) - S(x)| and output as a table?

The values ​​a, b, h and n are entered from the keyboard. Since the value S(x) is a series of decomposition of the function Y(x), with the correct solution, the values ​​of S and Y for a given argument x (for test values ​​of the initial data) must match in the integer part and in the first two to four positions after the decimal point. Check the operation of the program for a = 0.1; b = 1.0; h = 0.1; parameter value n = 120.

Here is how I tried to do it:

#include "stdio.h"
#include <iostream>
#include "math.h"
#include <iomanip>

using namespace std;

double S, Y, a, b, h, k, x, r, n;

double sum(double a, double b, double h);
double Yx(double a, double b, double h);
double absDifference(double a, double b, double h);

int main()
{
cout « "Enter start point - a = ";
cin » a;
cout « "Enter final point - b = ";
cin » b;
cout « "Enter step size - h = ";
cin » h;
cout « "Enter number of evaquations - n = ";
cin » n;

cout « setw(20) « left « " S(x)" « setw(20) « " Y(x)" « setw(20) « " 
|Y-X|" « endl;
for (double x = a; x < b; x += h)
{
/*
sum(a, b, h);
Yx(a, b, h);
absDifference(a, b, h);
*/
k = 1;
S = 0;
r = -x;
r *= -(((x * x)) / ((2 * k - 1) * (2 * k + 1)));
S = r;
for (k = 2; k <= n; k++)
{
r *= -(((x * x) * (2 * (k - 1) - 1) * (2 * (k - 1) + 1)) / ((2 * k - 
1) * (2 * k + 1))); 
S += r;
}
Y = ((1 + (x * x)) / 2) * atan(x) - (x / 2);
double absDifference = abs(Y - S);

cout « setw(20) « S « setw(20) « Y « setw(20) « absDifference « endl;
}
system("pause");
return 0;
}

double sum(double a, double b, double h)
{
k = 1;
S = 0;
r = -x;
r *= -(((x * x)) / ((2 * k - 1) * (2 * k + 1))); 
S = r;
for (k = 2; k <= n; k++)
{
r *= -(((x * x) * (2 * (k - 1) - 1) * (2 * (k - 1) + 1)) / ((2 * k - 
1) * (2 * k + 1))); 
S += r;
}
return 1;
}

double Yx(double a, double b, double h)
{
Y = ((1 + (x * x)) / 2) * atan(x) - (x / 2);
return 1;
}

double absDifference(double a, double b, double h)
{
double absDifference = abs(Y - S);
return 1;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anzorik_228, 2022-01-14
@Anzorik_228

Hello, I'm very interested in where you got my code from, scarecrow. Think with your head, otherwise you will be begging for bread from your parents until middle age

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question