Calculate and display on the screen in the form of a table the values of the function specified using the Taylor series in the interval from Xstart to Xend with a step of dx with an accuracy of E. Provide the table with a header and a header. Each row of the table must contain the value of the argument, the value of the function and the number of summed members of the series.Taylor series
I just can't understand what does lnx on the left side mean? This is not Taylor series for lnx..
using System;
namespace LabWorks1k2s
{
internal class Program
{
static void Main()
{
double y, xmin, xmax, dx, e;
int n, nmax;
string buf;
do {
Console.WriteLine("x start: ");
buf = Console.ReadLine();
xmin = Convert.ToDouble(buf);
}
while (xmin < 0.5);
do
{
Console.WriteLine("x end: ");
buf = Console.ReadLine();
xmax = Convert.ToDouble(buf);
}
while (xmax < 0.5);
Console.WriteLine("dх: ");
buf = Console.ReadLine();
dx = Convert.ToDouble(buf);
Console.WriteLine("Iterations: ");
buf = Console.ReadLine();
nmax = Convert.ToInt32(buf);
Console.WriteLine("accuracy e: ");
buf = Console.ReadLine();
e = Convert.ToDouble(buf);
n = 0;
double sum = 0;
Console.WriteLine("Arg\t Func\t N");
while (xmin<=xmax)
{
do
{
y = Math.Pow((xmin - 1), n + 1) / (n + 1) * Math.Pow(xmin, n + 1);
sum = sum + y;
n++;
xmin += dx;
Console.WriteLine("{0}\t{1,50}\t{2}", xmin, sum, n);
if (n>=nmax) {
Console.WriteLine("Too much iterations!");
break;
}
}
while (Math.Abs(sum) >= e);
}
Console.ReadKey();
}
}
}
Made this code, but I don't actually understand task :c
This is a Taylor series for ln(x) around 1. Just a bit simplified and limited to x > 1/2. If you have any doubts, you can see it on the graph. In other words, the Taylor series with infinite number of terms is exactly equal to ln(x), which is what the screen says. Taylor series with finite number of terms is an approximation of ln(x) around 1, which is what you need to calculate.
Regarding the code, the main problem is that the outer and inner loops are confused. To calculate the Taylor series approximation you have to calculate all necessary series terms for each point. Thus, the outer loop is responsible for iterating over the points. The inner loop is responsible for iterating over the terms of the series.
You have to do {
n = 0,sum = 0,xmin += dx, output to table} once for the point (in the outer loop).And you have to add formula once for the series term (in the inner loop).
The condition
Math.Abs(sum) >= eis incorrect. The value of sum is an approximation of ln(x). Therefore, the correct condition isMath.Abs(sum - Math.Log(xmin)) >= e.It is not necessary to control nmax because the answer always converges (in case of correct input).
Also, there is an error in the formula. You write
When the correct formula would be
Or like this