Advertisement
Guest User

Algoritm_Narmevärde

a guest
Dec 10th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Ekvationslösningsalgoritm
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             double x0 = 1;
  14.             double x0Derrivata;
  15.             double x0Funktion;
  16.             double x1;
  17.             double xny;
  18.             double felmarginal;
  19.  
  20.             x0Funktion = Funktion(x0);
  21.             x0Derrivata = Derrivata(x0);
  22.             x1 = NästaGissning(x0Funktion, x0Derrivata, x0);
  23.             while (true)
  24.             {
  25.                 xny = x1;
  26.  
  27.                 x0Funktion = Funktion(xny);
  28.                 x0Derrivata = Derrivata(xny);
  29.                 x1 = NästaGissning(x0Funktion, x0Derrivata, xny);
  30.                 Console.WriteLine(x1);
  31.                 felmarginal = Math.Abs(x1 - xny);
  32.                 if (felmarginal < 0.000001)
  33.                 {
  34.                     Console.WriteLine(x1);
  35.                     break;
  36.                 }
  37.  
  38.             }
  39.  
  40.            
  41.         }
  42.  
  43.         static double Funktion(double x1)
  44.         {
  45.             double yvärde = Math.Pow(x1, 3) - Math.Pow(1.2, x1);
  46.             return yvärde;
  47.         }
  48.  
  49.         static double Derrivata(double x1)
  50.         {
  51.             double kvärde =  3 * Math.Pow(x1, 2) - Math.Pow(1.2, x1) * Math.Log(1.2);
  52.             return kvärde;
  53.         }
  54.  
  55.         static double NästaGissning(double funktion, double derrivata, double x0)
  56.         {
  57.             double x1 = x0 - (funktion / derrivata);
  58.             return x1;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement