Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Ekvationslösningsalgoritm
- {
- class Program
- {
- static void Main(string[] args)
- {
- double x0 = 1;
- double x0Derrivata;
- double x0Funktion;
- double x1;
- double xny;
- double felmarginal;
- x0Funktion = Funktion(x0);
- x0Derrivata = Derrivata(x0);
- x1 = NästaGissning(x0Funktion, x0Derrivata, x0);
- while (true)
- {
- xny = x1;
- x0Funktion = Funktion(xny);
- x0Derrivata = Derrivata(xny);
- x1 = NästaGissning(x0Funktion, x0Derrivata, xny);
- Console.WriteLine(x1);
- felmarginal = Math.Abs(x1 - xny);
- if (felmarginal < 0.000001)
- {
- Console.WriteLine(x1);
- break;
- }
- }
- }
- static double Funktion(double x1)
- {
- double yvärde = Math.Pow(x1, 3) - Math.Pow(1.2, x1);
- return yvärde;
- }
- static double Derrivata(double x1)
- {
- double kvärde = 3 * Math.Pow(x1, 2) - Math.Pow(1.2, x1) * Math.Log(1.2);
- return kvärde;
- }
- static double NästaGissning(double funktion, double derrivata, double x0)
- {
- double x1 = x0 - (funktion / derrivata);
- return x1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement