Skip to content

Instantly share code, notes, and snippets.

/lab2

Created June 3, 2012 09:29
Show Gist options
  • Save anonymous/2862746 to your computer and use it in GitHub Desktop.
Save anonymous/2862746 to your computer and use it in GitHub Desktop.
Newton's method & simple iterations method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace NumericalAnalysis
{
class NonlinearEquations
{
public NonlinearEquations()
{
}
// lab2.1
public double NewtonsMethod(double eps, StreamWriter sw)
{
double x0 = 0.7, x1 = 0.8; // [0.7, 0.8]
int iterCount = 0;
sw.WriteLine("Newton's method. Miscalculation = {0}", eps);
while (true)
{
sw.WriteLine("x{0} = {1}", iterCount, x1);
iterCount++;
x1 = x0 - f(x0) / f_(x0);
if (Math.Abs(x1 - x0) < eps)
break;
else
x0 = x1;
}
return x1;
}
public double SimpleIterationsMethod(double eps, StreamWriter sw)
{
double a = 3.5, b = 4, x0 = (a + b)/2, x1 = x0, q = 0.2006165278, eFactor = q/(1 - q);
int iterCount = 0;
sw.WriteLine("Simple iterations method. Miscalculation = {0}", eps);
while (true)
{
// sw.WriteLine("x{0} = {1}", iterCount, x1);
iterCount++;
x1 = g(x0);
if ((eFactor * Math.Abs(x1 - x0)) < eps)
return x1;
x0 = x1;
}
}
private double f(double x)
{
return Math.Sin(x) - 2 * x * x + 0.5;
}
private double f_(double x)
{
return Math.Cos(x) - 4 * x;
}
private double g(double x)
{
return (Math.Sin(x) + 0.5) / (2 * x);
}
// lab2.2
public void NewtonsMethodSystem(double eps, StreamWriter sw)
{
double x0 = 0.75, y0 = 1.75, x1, y1; // equation root is located on 0.5 < x < 1, 1.5 < y < 2
int iterCount = 0;
Matrix A1, A2, J;
sw.WriteLine("Newton's method of solving system. Miscalculation = {0}\nStart values: x0 = {1}, y0 = {2}\n", eps, x0, y0);
while (true)
{
iterCount++;
A1 = CalculateMatrixA1(x0, y0);
A2 = CalculateMatrixA2(x0, y0);
J = CalculateMatrixJ(x0, y0);
x1 = x0 - A1.det / J.det;
y1 = y0 - A2.det / J.det;
if (GetMaxDifference(x0, x1, y0, y1) <= eps)
{
sw.WriteLine("{0} < {1} => stopping criterion completed", GetMaxDifference(x0, x1, y0, y1), eps);
break;
}
sw.WriteLine("Values on {0} iteration: x = {1}, y = {2}", iterCount, x1, y1);
x0 = x1;
y0 = y1;
}
sw.WriteLine("Roots x = {0}, y = {1} calculated by Newton's method with miscalculation = {2} for the {3} iterations", x1, y1, eps, iterCount);
}
public void SimpleIterationsMethodSystem(double eps, StreamWriter sw)
{
double x0 = 0.75, y0 = 1.75, x1, y1, q = 0.877582, eFactor = q / (1 - q);
int iterCount = 0;
eps *= 10;
sw.WriteLine("\n\nSimple iterations method of solving system. Miscalculation = {0}\nStart values: x0 = {1}, y0 = {2}\n", eps, x0, y0);
while (true)
{
iterCount++;
x1 = f1(x0, y0);
y1 = g1(x0, y0);
if (StoppingCriterion(x0, y0, x1, y1, eFactor, eps))
{
sw.WriteLine("q/(1 - 1) * ||X{0} - X{1}|| < {2} => stopping criterion completed", iterCount, iterCount - 1, eps);
break;
}
sw.WriteLine("Values on {0} iteration: x = {1}, y = {2}", iterCount, x1, y1);
x0 = x1;
y0 = y1;
}
sw.WriteLine("Roots x = {0}, y = {1} calculated by simple iterations method with miscalculation = {2} for the {3} iterations", x1, y1, eps, iterCount);
}
private bool StoppingCriterion(double x0, double y0, double x1, double y1, double eFactor, double eps)
{
if (GetMaxDifference(x0, x1, y0, y1) * eFactor < eps)
return true;
return false;
}
private double f1(double x, double y)
{
return Math.Cos(y) + 1;
}
private double g1(double x, double y)
{
return Math.Sin(x) + 1;
}
private double GetMaxDifference(double x1, double x2, double y1, double y2)
{
double num1 = Math.Abs(x2 - x1), num2 = Math.Abs(y2 - y1);
return (num1 > num2 ? num1 : num2);
}
private double f(double x, double y)
{
return x - Math.Cos(y) - 1;
}
private double g(double x, double y)
{
return Math.Sin(x) - y + 1;
}
public Matrix CalculateMatrixJ(double x, double y)
{
Matrix J = new Matrix(new double[,] { { Dfdx(x, y), Dfdy(x, y) }, { Dgdx(x, y), Dgdy(x, y) } });
J.CalculateDeterminantSquare();
return J;
}
public Matrix CalculateMatrixA1(double x, double y)
{
Matrix A1 = new Matrix(new double[,] { { f(x, y), Dfdy(x, y) }, { g(x, y), Dgdy(x, y) } });
A1.CalculateDeterminantSquare();
return A1;
}
public Matrix CalculateMatrixA2(double x, double y)
{
Matrix A2 = new Matrix(new double[,] { { Dfdx(x, y), f(x, y) }, { Dgdx(x, y), g(x, y) } });
A2.CalculateDeterminantSquare();
return A2;
}
private double Dfdx(double x, double y)
{
return 1;
}
private double Dfdy(double x, double y)
{
return Math.Sin(y);
}
private double Dgdx(double x, double y)
{
return Math.Cos(x);
}
private double Dgdy(double x, double y)
{
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment