예제 #1
0
 public Vector computeExactSolution(Vector nodes, double time)
 {
     Vector exactSolution = new Vector(nodes.Length);
     for (int i = 0; i < nodes.Length; i++)
         exactSolution[i] = ExactSolution(nodes[i], time);
     return exactSolution;
 }
예제 #2
0
        public static double evaluateLagrangePolynome(double x, Vector nodes, int j)
        {
            int idx;
            //Gibt die Position zurück, wenn x einer Stützerstelle entspricht.
            if ((idx = nodes.ContainsValue(x)) != -1)
            {
                if (idx == j)
                    return 1.0;
                else
                    return 0.0;
            }

            Vector barycentricWeights = computeBarycentricWeights(nodes);
            double tempProd = 1.0;

            for(int i = 0; i < nodes.Length; i++)
            {
                if (i != j)
                    tempProd *= (x - nodes[i]);
            }

            tempProd *= barycentricWeights[j];

            return tempProd;
        }
예제 #3
0
        public Vector computeSolutionVectorWithMultipleSteps(Vector initial, OrdinaryDifferentialEquation ode, double startTime, double endTime, double step)
        {
            double tempTime = startTime;
            int N = Convert.ToInt32((endTime - startTime) / step) + 1;
            Vector tempSolution = initial;

            Vector solution = new Vector(N);
            solution[0] = initial[0];

            Vector temp = new Vector(1);

            for (int i = 1; i < N; i++)
            {

                temp[0] = solution[i - 1];
                solution[i] = computeSolutionForNextStep(temp, ode, tempTime, tempTime + step)[0];
                tempTime += step;
            }

            if (!GeneralHelper.isXAlmostEqualToY(startTime + (N-1) * step, endTime))
            {
                tempTime = startTime + N * step;
                tempSolution = computeSolutionForNextStep(tempSolution, ode, tempTime, endTime);
            }

            return solution;
        }
예제 #4
0
 private static double computeIntegralSummation(Func<double, double> myFunction, Vector nodes, Vector weights)
 {
     double evaluation = 0.0;
     for (int i = 0; i <nodes.Length; i++)
     {
         evaluation += myFunction(nodes[i]) * weights[i];
     }
     return evaluation;
 }
예제 #5
0
        /// <summary>
        /// Creates a diagonal matrix using values from input vector for diagonal entries.
        /// </summary>
        /// <param name="_diag">Vector, which should be mapped as a diagonal matrix.</param>
        public Matrix(Vector _diag)
        {
            noRows = _diag.Length;
            noColumns = noRows;
            values = new double[noRows, noColumns];

            for (int i = 0; i < noRows; i++)
                values[i, i] = _diag[i];
        }
예제 #6
0
 public static String ConvertToMatLabPlotString(Vector nodes, Vector evaluation)
 {
     StringBuilder sb = new StringBuilder();
     sb.Append(ConvertVectorToMatLabVector(nodes, "X")).AppendLine();
     sb.Append(ConvertVectorToMatLabVector(evaluation, "Y")).AppendLine();
     sb.Append("figure").AppendLine();
     sb.Append("plot(X,Y)");
     return sb.ToString();
 }
예제 #7
0
        private Matrix computeSBPMatrix(Vector nodes, Vector weights)
        {
            Matrix massMatrix = IntegrationToolbox.generateMassMatrix(weights);
            Matrix diffMatrix = InterpolationToolbox.computeLagrangePolynomeDerivativeMatrix(nodes);

            Matrix left = massMatrix * diffMatrix;
            Matrix B = left + !left;
            return B;
        }
예제 #8
0
 public Vector ComputeConstant()
 {
     Vector constant = new Vector((polynomOrder + 1) * elements.Length);
     for(int i = 0; i < elements.Length; i++)
     {
         constant.InjectMatrixAtPosition(elements[i].GetConstantSolution(Ground), i * (polynomOrder+1), 0);
     }
     return constant;
 }
        public Vector evaluateOrdinaryDifferentialEquation(Vector startSolution, double time)
        {
            Vector evaluatedODE = new Vector(myOrdinaryFunctionList.Count);

            for(int i = 0; i < myOrdinaryFunctionList.Count; i++)
            {
                evaluatedODE[i] = myOrdinaryFunctionList.ElementAt(i)(startSolution[i], time);
            }
            return evaluatedODE;
        }
예제 #10
0
        public static String ConvertToMatLabPlotStringWithAxisLabelAndTitle(Vector nodes, Vector evaluation, String xAxisLabel, String yAxisName, String title)
        {
            StringBuilder sb = new StringBuilder(ConvertToMatLabPlotString(nodes, evaluation));
            sb.AppendLine();
            sb.Append("title('").Append(title).Append("');").AppendLine();
            sb.Append("xlabel('").Append(xAxisLabel).Append("');").AppendLine();
            sb.Append("ylabel('").Append(yAxisName).Append("');").AppendLine();

            return sb.ToString();
        }
예제 #11
0
        /// <summary>
        /// Aquidistante Stützstellen berechnen
        /// </summary>
        /// <param name="leftBorder">Linker Rand</param>
        /// <param name="rightBorder">Rechter Rand</param>
        /// <param name="N">Zerlegungsparameter</param>
        /// <returns>Vektor mit aquidistanten Stützstellen</returns>
        private Vector computeAquiDistantNodes(double leftBorder, double rightBorder, int N)
        {
            Vector aquiDistantNodes = new Vector(N + 1);

            for(int i = 0; i <= N; i++)
            {
                aquiDistantNodes[i] = leftBorder + ((rightBorder - leftBorder) / (double)N) * (double)i;
            }
            return aquiDistantNodes;
        }
예제 #12
0
        /// <summary>
        /// Berechnet die Gauß Lobatto Stützstellen und deren zugehörige Gewichte.
        /// </summary>
        /// <param name="N">Grad N des Legendre Stützpolynoms</param>
        /// <param name="nodes">Ausgabe der GL-Stützstellen</param>
        /// <param name="weights">Ausgabe der GL-Gewichte</param>
        public static void computeGaussLobattoNodesAndWeights(int N, out Vector nodes, out Vector weights)
        {
            if( N == 0)
            {
                throw new Exception("Gauß Lobatto Nodes and Weights not defined for N = 0.");
            }
            Vector gaussNodes = new Vector(N + 1);
            Vector gaussWeights = new Vector(N + 1);
            if (N == 1)
            {
                gaussNodes[0] = -1.0;
                gaussWeights[0] = 1.0;
                gaussNodes[1] = 1.0;
                gaussWeights[1] = 1.0;
            }
            else
            {
                int upperBound = (N + 1) / 2 - 1;
                double q, qDerivative, evaPolynomial, delta;

                gaussNodes[0] = -1;
                gaussWeights[0] = 2.0 / (N * (N + 1));
                gaussNodes[N] = 1;
                gaussWeights[N] = gaussWeights[0];

                for(int j=1;j<= upperBound; j++)
                {
                    gaussNodes[j] = -Math.Cos( ((j + 0.25) * Math.PI / (double)N) - ((3.0 / (8.0 * N * Math.PI)) * (1.0 / (j + 0.25))) );

                    for(int k = 0; k <= NEWTON_ITERATOR; k++)
                    {
                        evaluateQandL(N, gaussNodes[j], out q,out qDerivative,out evaPolynomial);
                        delta = -q / qDerivative;
                        gaussNodes[j] += delta;
                        if (Math.Abs(delta) <= 2 * GeneralHelper.EPSILON * Math.Abs(gaussNodes[j]))
                            k = NEWTON_ITERATOR + 1;
                    }
                    evaluateQandL(N, gaussNodes[j], out q, out qDerivative, out evaPolynomial);
                    gaussNodes[N - j] = -gaussNodes[j];
                    gaussWeights[j] = 2.0 / (N * (N + 1) * evaPolynomial * evaPolynomial);
                    gaussWeights[N - j] = gaussWeights[j];
                }

                if(N%2 == 0)
                {
                    evaluateQandL(N, 0.0, out q, out qDerivative, out evaPolynomial);
                    gaussNodes[N / 2] = 0.0;
                    gaussWeights[N / 2] = 2.0 / (N * (N + 1) * evaPolynomial * evaPolynomial);
                }
            }

            nodes = gaussNodes;
            weights = gaussWeights;
        }
예제 #13
0
        private Vector GetZValues(int x, int y)
        {
            Vector zValues = new Vector(DimZ);

            for(int i = 0; i < DimZ; i++)
            {
                zValues[i] = matrices[i][x, y];
            }

            return zValues;
        }
예제 #14
0
        private static Vector computeSpaceDiscretationVector(double leftSpaceBoundary, double rightSpaceBoundary, int N)
        {
            Vector spaceDiscretization = new Vector(N - 1);
            double spaceStep = (Math.Abs(leftSpaceBoundary) + Math.Abs(leftSpaceBoundary)) / (double)N;
            for (int i = 0; i < N-1; i++)
            {
                double spaceLocation = leftSpaceBoundary + (i + 1) * spaceStep;
                spaceDiscretization[i] = spaceLocation;
            }

            return spaceDiscretization;
        }
예제 #15
0
        /// <summary>
        /// Berechnet die Visualisierungsmatrix
        /// </summary>
        /// <param name="lagrangeEvaluation">Auswertung der Funktion an den erzeugenden Stützstellen</param>
        /// <param name="lagrangeNodes">Lagrange Polynome erzeugende Stützstellen </param>
        /// <param name="nodes">Auswertungsstellen der Interpolation</param>
        /// <returns></returns>
        private Matrix computeVisualizationMatrix(Vector lagrangeEvaluation, Vector lagrangeNodes, Vector nodes)
        {
            Matrix visualMatrix = new Matrix(nodes.Length, lagrangeNodes.Length);

            for(int i = 0; i < nodes.Length; i++)
            {
                for(int k = 0; k < lagrangeNodes.Length; k++)
                {
                    visualMatrix[i, k] = InterpolationToolbox.evaluateLagrangePolynome(nodes[i], lagrangeNodes, k);
                }
            }
            return visualMatrix;
        }
예제 #16
0
        private static Vector evaluateExactSolution(double time, double leftSpaceBoundary, double rightSpaceBoundary, int N)
        {
            Vector evaluation = new Vector(N - 1);
            double spaceStep = (Math.Abs(leftSpaceBoundary) + Math.Abs(leftSpaceBoundary)) / (double)N;

            for(int i = 0; i< N-1; i++)
            {
                double spaceLocation = leftSpaceBoundary + (i + 1) * spaceStep;
                evaluation[i] = exactSolution(time, spaceLocation);
            }

            return evaluation;
        }
예제 #17
0
        private static String ConvertVectorToMatLabVector(Vector array, String arrayName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(arrayName).Append(" = ");
            sb.Append("[");
            for(int i = 0; i < array.Length - 1; i++)
            {
                sb.Append(array[i] + ";");
            }
            sb.Append(array[array.Length-1] + "];");

            return sb.ToString().Replace(",",".") ;
        }
예제 #18
0
        public Vector computeSolutionForNextStep(Vector initial,OrdinaryDifferentialEquation ode, double startTime, double endTime)
        {
            Vector tempSolution = initial;
            Vector evaluatedODE = ode.evaluateOrdinaryDifferentialEquation(tempSolution, startTime);
            double deltaTime = endTime - startTime;

            for(int i = 0; i < 5; i++)
            {
                double nextTimeStep = startTime + B[i] * deltaTime;
                evaluatedODE = (Vector) (A[i] * evaluatedODE) + ode.evaluateOrdinaryDifferentialEquation(tempSolution, nextTimeStep);
                tempSolution = tempSolution + (Vector)(C[i] * deltaTime * evaluatedODE);
            }

            return tempSolution;
        }
예제 #19
0
        /// <summary>
        /// Solves the equation Ax = b by using Gauß Elimination Algorithm for a square matrix A
        /// </summary>
        /// <param name="A">Matrix A</param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static Vector Solve(Matrix A, Vector b)
        {
            int N = A.NoRows;

            if (A.NoRows != A.NoColumns)
                throw new SolverException("Matrix A is not square.");

            //Transform to lower triangular matrix
            for (int i = 0; i < N; i++)
            {
                int pivotIdx = A.GetPivotRow(i, i);
                if (A[pivotIdx,i] == 0.00)
                    throw new SolverException("Matrix A is not singular");

                A.SwapRow(i, pivotIdx);
                b.SwapRow(i, pivotIdx);

                for (int row = i + 1; row < N; row++)
                {
                    for (int col = i + 1; col < N; col++)
                    {
                        A[row, col] = A[row, col] - A[i, col] * ((A[row, i] / A[i, i]));

                    }

                    //Update vecctor b
                    b[row, 0] = b[row, 0] - b[i, 0] * ((A[row, i] / A[i, i]));

                    //Set lower Triangular Matrix with zeros
                    A[row, i] = 0.0;
                }
            }

            //Transform lower triangular matrix to diagnol matrix
            for (int i = N-1; i >= 0; i--)
            {
                //Normalize
                b[i,0] = b[i,0]/A[i,i];
                A[i, i] = 1.0;
                for (int row = i - 1; row >= 0; row--)
                {
                    b[row, 0] = b[row, 0] - b[i, 0] * (A[row, i]);
                    A[row, i] = 0.0;
                }
            }

            return b;
        }
예제 #20
0
        public static Vector computeBarycentricWeights(Vector nodes)
        {
            double tempProd = 0.0;
            int N = nodes.Length;
            Vector baryWeights = new Vector(N);

            for(int i = 0; i < N; i++)
            {
                tempProd = 1.0;
                for(int j = 0; j < N; j++)
                {
                    if(i != j)
                        tempProd *= (nodes[i] - nodes[j]);
                }
                baryWeights[i] = 1.0/tempProd;
            }

            return baryWeights;
        }
예제 #21
0
        public double computeSolution(double endTime, double timeStep)
        {
            double recentTime = 0.0;
            Vector[] tempTimeDerivaties = new Vector[elements.Length];

            while (recentTime < endTime)
            {
                if (recentTime + timeStep > endTime)
                    timeStep = endTime - recentTime;
                //Vektoren zurüksetzen
                for (int i = 0; i < elements.Length; i++)
                    tempTimeDerivaties[i] = new Vector(polynomOrder+1);

               //Runga Kutta
                for (int k = 0; k < 5; k++)
                {
                    //Runga Kutta für jedes Element, damit Zeitsynchron berechnet wird
                    for (int i = 0; i < elements.Length; i++)
                    {
                        Vector solution = elements[i].GetSolution();
                        double nextTimeStep = recentTime + B[k] * timeStep;
                        tempTimeDerivaties[i] = (Vector)(A[k] * tempTimeDerivaties[i]) + elements[i].EvaluateTimeDerivative(nextTimeStep);
                        solution = solution + (Vector)(C[k] * timeStep * tempTimeDerivaties[i]);
                        elements[i].UpdateSolution(solution);
                    }
                    //Rand updaten
                    for (int i = 0; i < elements.Length; i++)
                    {
                        elements[i].UpdateBorderValues();
                    }
                }

                recentTime += timeStep;
            }

            //for (int k = 0; k < elements.Length; k++)
            //{
            //    for (int i = 0; i < elements[k].GetSolution().Length; i++)
            //        Console.WriteLine(elements[k].GetSolution()[i]);
            //}
            return computeError(endTime);
        }
예제 #22
0
        public void evaluate()
        {
            //Anfangsbedingung
            Vector initial = new Vector(1);
            initial[0] = -1.0;
            List<Func<double, double, double>> mySystem = new List<Func<double, double, double>>();
            mySystem.Add(derivativeFunction);
            OrdinaryDifferentialEquation testEquation = new OrdinaryDifferentialEquation(mySystem);
            IODESolver odeSolver = new RungeKuttaSolver();

            double[] errorList = new double[10];

            for (int i = 1; i <= 10; i++)
            {
                double timeStep = Math.Pow(2.0, -i);
                Vector res = odeSolver.computeSolutionVectorWithMultipleSteps(initial, testEquation, 1.0, 2.0, timeStep);
                //Vector exact = null; //hier die exacte auswertung
                //double error = (res - exact) * (res - exact);
            }
        }
예제 #23
0
        public static Matrix computeLagrangePolynomeDerivativeMatrix(Vector nodes)
        {
            Matrix D = new Matrix(nodes.Length, nodes.Length);
            Vector baryWeights = computeBarycentricWeights(nodes);

            for (int i = 0; i < nodes.Length; i++)
            {
                D[i, i] = 0.0;
                for(int j = 0; j < nodes.Length; j++)
                {
                    if(i != j)
                    {
                        D[i, j] = baryWeights[j] / baryWeights[i] * (1.0/(nodes[i] - nodes[j])) ;
                        D[i, i] -= D[i, j];
                    }
                }
            }

            return D;
        }
예제 #24
0
        public Vector computeSolutionWithMultipleSteps(Vector initial, OrdinaryDifferentialEquation ode, double startTime, double endTime, double step)
        {
            double tempTime = startTime;
            int N =  Convert.ToInt32((endTime - startTime)/ step);
            Vector tempSolution = initial;

            for(int i = 1; i <= N; i++)
            {
                tempSolution = computeSolutionForNextStep(tempSolution, ode, tempTime, tempTime + step);
                tempTime += step;
            }

            if (!GeneralHelper.isXAlmostEqualToY(startTime + N * step, endTime))
            {
                tempTime = startTime + N * step;
                tempSolution = computeSolutionForNextStep(tempSolution, ode, tempTime, endTime);
            }

            return tempSolution;
        }
예제 #25
0
        public static double evaluateLagrangeRepresentation(double x, Vector nodes, Vector functionValues)
        {
            int idx;
            //Gibt die Position zurück, wenn x einer Stützerstelle entspricht.
            if( (idx = nodes.ContainsValue(x)) != -1)
                return functionValues[idx];

            Vector barycentricWeights = computeBarycentricWeights(nodes);
            double tempQuot = 0.0;
            double denominator = 0.0;
            double numerator = 0.0;

            for(int i = 0; i < nodes.Length; i++)
            {
                tempQuot = barycentricWeights[i] / (x - nodes[i]);
                numerator += functionValues[i] * tempQuot;
                denominator += tempQuot;
            }

            return numerator / denominator;
        }
예제 #26
0
        public void TaskOneEnergy()
        {
            DGSystemController controller = new DGSystemController();
            controller.createDGElements(101, 3, 0.0, 20.0, 2);

            double test = controller.ComputeEnergy();
            Console.WriteLine(test);
            Dictionary<double, double> Energy = controller.ComputeSolution(1.0);

            Vector time = new Vector(Energy.Count);
            Vector energyVector = new Vector(Energy.Count);
            int counter = 0;
            foreach (KeyValuePair<double, double> entry in Energy)
            {
                time[counter] = entry.Key;
                energyVector[counter] = entry.Value;
                counter++;
                Console.WriteLine("Time:" + entry.Key + " Energy:" + entry.Value);
            }

            string matlabString = MatLabConverter.ConvertToMatLabPlotStringWithAxisLabelAndTitle(time, energyVector, "Zeit t", "Energie e", "Energieverlauf");
        }
예제 #27
0
 private Vector visualizeFunctionDerivative(Matrix visualizationMatrix, Vector evaluation, Vector nodes)
 {
     Matrix diffMatrix = InterpolationToolbox.computeLagrangePolynomeDerivativeMatrix(nodes);
     Vector differentialFunctionEvaluation = diffMatrix * evaluation;
     Vector visualizedDerivativeEvaluation = visualizationMatrix * differentialFunctionEvaluation;
     return visualizedDerivativeEvaluation;
 }
예제 #28
0
 private Vector visualizeFunction(Matrix visualizationMatrix, Vector evaluation)
 {
     Vector visualizedEvaluation = visualizationMatrix * evaluation;
     return visualizedEvaluation;
 }
예제 #29
0
        /// <summary>
        /// Evaluiert eine Funktion an gegebenen Stützstellen.
        /// </summary>
        /// <param name="function">Delegate an die Funktion</param>
        /// <param name="nodes">Die Stützstellen</param>
        /// <returns>Funktionsauswertung an den Stützstellen</returns>
        private Vector evaluateFunctionAtNodes(Func<double, double> function, Vector nodes)
        {
            Vector evaluation = new Vector(nodes.Length);
            for (int i = 0; i < nodes.Length; i++)
                evaluation[i] = function(nodes[i]);

            return evaluation;
        }
예제 #30
0
        private Vector ComputeV(Matrix Solution)
        {
            Vector v = new Vector(Solution.NoRows);
            Vector h = Solution.GetColumn(0);
            Vector hv = Solution.GetColumn(1);

            for(int i = 0; i < v.Length; i++){
                v[i] = hv[i] / h[i];
            }
            return v;
        }