Hasan Şener - Anasayfa

Hasan Şener

Personal Writings, PhD Adventure



watch this blog!

Newton - Raphson ve Secant Metodları

2 Ekim 2022 Pazar - # # # #

Soru 1

$x^{5} = 83$ sayısının çözümünü 6. basamağa kadar hassas olarak bütün metotlarla çözümleyiniz.

Newton - Raphson Metodu ile Çözüm

İlk olarak hata tanımlaması ϵ yapılır. Eğer ϵ 1E- 6 sayısından küçük ise döngü durdurulur. İlk değer olarak x=1 değerini verelim.

format shortG
clc
%% Başlangıç
f = @(x) x^5 - 83;
f_prime = @(x) 5.*(x^4);
f_prime_prime = @(x) 20*(x^3);

% İlk değer olarak x_current = 1 olsun.
x_current = 1;
counter = 1;
while (true)
x_next = x_current - (f(x_current)/f_prime(x_current));
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 17.4000000000 | Epsilon: 16.4000000
|Deneme: 2 | x_n: 17.4000000000 | x_(n+1): 13.9201810969 | Epsilon: 3.4798189
|Deneme: 3 | x_n: 13.9201810969 | x_(n+1): 11.1365869857 | Epsilon: 2.7835941
|Deneme: 4 | x_n: 11.1365869857 | x_(n+1): 8.9103487829 | Epsilon: 2.2262382
|Deneme: 5 | x_n: 8.9103487829 | x_(n+1): 7.1309125017 | Epsilon: 1.7794363
|Deneme: 6 | x_n: 7.1309125017 | x_(n+1): 5.7111498924 | Epsilon: 1.4197626
|Deneme: 7 | x_n: 5.7111498924 | x_(n+1): 4.5845231203 | Epsilon: 1.1266268
|Deneme: 8 | x_n: 4.5845231203 | x_(n+1): 3.7051963022 | Epsilon: 0.8793268
|Deneme: 9 | x_n: 3.7051963022 | x_(n+1): 3.0522341810 | Epsilon: 0.6529621
|Deneme: 10 | x_n: 3.0522341810 | x_(n+1): 2.6330528631 | Epsilon: 0.4191813
|Deneme: 11 | x_n: 2.6330528631 | x_(n+1): 2.4518004923 | Epsilon: 0.1812524
|Deneme: 12 | x_n: 2.4518004923 | x_(n+1): 2.4208156265 | Epsilon: 0.0309849
|Deneme: 13 | x_n: 2.4208156265 | x_(n+1): 2.4200019545 | Epsilon: 0.0008137
|Deneme: 14 | x_n: 2.4200019545 | x_(n+1): 2.4200014070 | Epsilon: 0.0000005

Newton’un Değiştirilmiş Metodu

clear epsilon counter x_current x_next

% İlk değer olarak x_current = 1 olsun.
x_current = 1;
counter = 1;

while (true)
u_x = f(x_current) / f_prime(x_current);
u_prime_x = 1 - (f(x_current) * f_prime_prime(x_current) / (f_prime(x_current))^2);
x_next = x_current - (u_x / u_prime_x);
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 1.2462462462 | Epsilon: 0.2462462
|Deneme: 2 | x_n: 1.2462462462 | x_(n+1): 1.5438286717 | Epsilon: 0.2975824
|Deneme: 3 | x_n: 1.5438286717 | x_(n+1): 1.8801216472 | Epsilon: 0.3362930
|Deneme: 4 | x_n: 1.8801216472 | x_(n+1): 2.1948435554 | Epsilon: 0.3147219
|Deneme: 5 | x_n: 2.1948435554 | x_(n+1): 2.3786273026 | Epsilon: 0.1837837
|Deneme: 6 | x_n: 2.3786273026 | x_(n+1): 2.4185873006 | Epsilon: 0.0399600
|Deneme: 7 | x_n: 2.4185873006 | x_(n+1): 2.4199997543 | Epsilon: 0.0014125
|Deneme: 8 | x_n: 2.4199997543 | x_(n+1): 2.4200014070 | Epsilon: 0.0000017
|Deneme: 9 | x_n: 2.4200014070 | x_(n+1): 2.4200014070 | Epsilon: 0.0000000

Secant Metodu

Bu metotta iki adet başlangıç değeri kullanacağız. İlk başlangıç değeri olarak $x_0$=1 ve  $x_{-1}$=2 değerlerini kullanacağız.
clear epsilon counter x_current x_next

% İlk değer olarak x_current = 1, x_previous = 2 olsun.
x_current = 1;
x_previous = 2;
counter = 1;


while (true)
x_next = x_current - ((f(x_current)*(x_current - x_previous)) / (f(x_current) - f(x_previous)));
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 3.6451612903 | Epsilon: 2.6451613
|Deneme: 2 | x_n: 3.6451612903 | x_(n+1): 2.1371971716 | Epsilon: 1.5079641
|Deneme: 3 | x_n: 2.1371971716 | x_(n+1): 2.5558287757 | Epsilon: 0.4186316
|Deneme: 4 | x_n: 2.5558287757 | x_(n+1): 2.3678677390 | Epsilon: 0.1879610
|Deneme: 5 | x_n: 2.3678677390 | x_(n+1): 2.4420995924 | Epsilon: 0.0742319
|Deneme: 6 | x_n: 2.4420995924 | x_(n+1): 2.4109975081 | Epsilon: 0.0311021
|Deneme: 7 | x_n: 2.4109975081 | x_(n+1): 2.4237311195 | Epsilon: 0.0127336
|Deneme: 8 | x_n: 2.4237311195 | x_(n+1): 2.4184668586 | Epsilon: 0.0052643
|Deneme: 9 | x_n: 2.4184668586 | x_(n+1): 2.4206345479 | Epsilon: 0.0021677
|Deneme: 10 | x_n: 2.4206345479 | x_(n+1): 2.4197404794 | Epsilon: 0.0008941
|Deneme: 11 | x_n: 2.4197404794 | x_(n+1): 2.4201089905 | Epsilon: 0.0003685
|Deneme: 12 | x_n: 2.4201089905 | x_(n+1): 2.4199570577 | Epsilon: 0.0001519
|Deneme: 13 | x_n: 2.4199570577 | x_(n+1): 2.4200196906 | Epsilon: 0.0000626
|Deneme: 14 | x_n: 2.4200196906 | x_(n+1): 2.4199938695 | Epsilon: 0.0000258
|Deneme: 15 | x_n: 2.4199938695 | x_(n+1): 2.4200045143 | Epsilon: 0.0000106
|Deneme: 16 | x_n: 2.4200045143 | x_(n+1): 2.4200001259 | Epsilon: 0.0000044
|Deneme: 17 | x_n: 2.4200001259 | x_(n+1): 2.4200019351 | Epsilon: 0.0000018
|Deneme: 18 | x_n: 2.4200019351 | x_(n+1): 2.4200011893 | Epsilon: 0.0000007

Soru 2

$f(x) = x^2 - 2e^{-x} \times x^3 - e^{-3x}$denkleminin köklerini $x_0$=1 ve  $x_{-1}$=2 başlangıç değerlerinde bütün metotlarla hesaplayınız.

Newton - Raphson Metodu ile Çözüm

İlk olarak hata tanımlaması ϵ yapılır. Eğer $\epsilon$ 1E- 6 sayısından küçük ise döngü durdurulur. İlk değer olarak $x_0$=1 değerini verelim.

format shortG
clear
clc

%% Başlangıç
f = @(x) x^2 - (2*exp(-1*x) * x^3) - exp(-3*x);
f_prime = @(x) 2*x + 3*exp(-3*x) - 6*x^2*exp(-x) + 2*x^3*exp(-x);
f_prime_prime = @(x) 12*x^2*exp(-x) - 12*x*exp(-x) - 9*exp(-3*x) - 2*x^3*exp(-x) + 2;

% İlk değer olarak x_current = 1 olsun.
x_current = 1;
counter = 1;
while (true)
x_next = x_current - (f(x_current)/f_prime(x_current));
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 0.6836230367 | Epsilon: 0.3163770
|Deneme: 2 | x_n: 0.6836230367 | x_(n+1): 0.6591230911 | Epsilon: 0.0244999
|Deneme: 3 | x_n: 0.6591230911 | x_(n+1): 0.6594925813 | Epsilon: 0.0003695
|Deneme: 4 | x_n: 0.6594925813 | x_(n+1): 0.6594926752 | Epsilon: 0.0000001

Newton’un Değiştirilmiş Metodu

clear epsilon counter x_current x_next

% İlk değer olarak x_current = 1 olsun.
x_current = 1;
counter = 1;

while (true)
u_x = f(x_current) / f_prime(x_current);
u_prime_x = 1 - (f(x_current) * f_prime_prime(x_current) / (f_prime(x_current))^2);
x_next = x_current - (u_x / u_prime_x);
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 0.4889449156 | Epsilon: 0.5110551
|Deneme: 2 | x_n: 0.4889449156 | x_(n+1): 0.7011445708 | Epsilon: 0.2121997
|Deneme: 3 | x_n: 0.7011445708 | x_(n+1): 0.6603131216 | Epsilon: 0.0408314
|Deneme: 4 | x_n: 0.6603131216 | x_(n+1): 0.6594931343 | Epsilon: 0.0008200
|Deneme: 5 | x_n: 0.6594931343 | x_(n+1): 0.6594926752 | Epsilon: 0.0000005

Secant Metodu

Bu metotta iki adet başlangıç değeri kullanacağız. İlk başlangıç değeri olarak $x_0 = 1$ ve $x_{-1} = 2$ değerlerini kullanacağız.
clear epsilon counter x_current x_next

% İlk değer olarak x_current = 1, x_previous = 2 olsun.
x_current = 1;
x_previous = 2;
counter = 1;


while (true)
x_next = x_current - ((f(x_current)*(x_current - x_previous)) / (f(x_current) - f(x_previous)));
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 0.8674329630 | Epsilon: 0.1325670
|Deneme: 2 | x_n: 0.8674329630 | x_(n+1): 0.7809065325 | Epsilon: 0.0865264
|Deneme: 3 | x_n: 0.7809065325 | x_(n+1): 0.7270244648 | Epsilon: 0.0538821
|Deneme: 4 | x_n: 0.7270244648 | x_(n+1): 0.6955806536 | Epsilon: 0.0314438
|Deneme: 5 | x_n: 0.6955806536 | x_(n+1): 0.6782557696 | Epsilon: 0.0173249
|Deneme: 6 | x_n: 0.6782557696 | x_(n+1): 0.6690882100 | Epsilon: 0.0091676
|Deneme: 7 | x_n: 0.6690882100 | x_(n+1): 0.6643550597 | Epsilon: 0.0047332
|Deneme: 8 | x_n: 0.6643550597 | x_(n+1): 0.6619446725 | Epsilon: 0.0024104
|Deneme: 9 | x_n: 0.6619446725 | x_(n+1): 0.6607260712 | Epsilon: 0.0012186
|Deneme: 10 | x_n: 0.6607260712 | x_(n+1): 0.6601123037 | Epsilon: 0.0006138
|Deneme: 11 | x_n: 0.6601123037 | x_(n+1): 0.6598037612 | Epsilon: 0.0003085
|Deneme: 12 | x_n: 0.6598037612 | x_(n+1): 0.6596488060 | Epsilon: 0.0001550
|Deneme: 13 | x_n: 0.6596488060 | x_(n+1): 0.6595710228 | Epsilon: 0.0000778
|Deneme: 14 | x_n: 0.6595710228 | x_(n+1): 0.6595319874 | Epsilon: 0.0000390
|Deneme: 15 | x_n: 0.6595319874 | x_(n+1): 0.6595123999 | Epsilon: 0.0000196
|Deneme: 16 | x_n: 0.6595123999 | x_(n+1): 0.6595025718 | Epsilon: 0.0000098
|Deneme: 17 | x_n: 0.6595025718 | x_(n+1): 0.6594976406 | Epsilon: 0.0000049
|Deneme: 18 | x_n: 0.6594976406 | x_(n+1): 0.6594951665 | Epsilon: 0.0000025
|Deneme: 19 | x_n: 0.6594951665 | x_(n+1): 0.6594939251 | Epsilon: 0.0000012
|Deneme: 20 | x_n: 0.6594939251 | x_(n+1): 0.6594933023 | Epsilon: 0.0000006

Soru 3

$sin{x^3+2} - 1/x$ denkleminin köklerini $x_0$=1 ve $x_{-1}$=2 başlangıç değerlerinde secant metodu ile hesaplayınız. 
Denklemin köklerini secant metodu ile bulacağız.
Aşağıdaki kod bloğunda sinüs fonksiyonu (sind) derece cinsinden kullanılmıştır.
clear epsilon counter x_current x_next f

f = @(x) sind(x^3 + 2) - (1/x);

% İlk değer olarak x_current = 1, x_previous = 2 olsun.
x_current = 1;
x_previous = 2;
counter = 1;


while (true)
x_next = x_current - ((f(x_current)*(x_current - x_previous)) / (f(x_current) - f(x_previous)));
epsilon = abs(x_next - x_current);
fprintf("|Deneme: %.0f \t | x_n: %4.10f | x_(n+1): %4.10f \t | Epsilon: %4.7f \n", ...
counter, x_current, x_next,epsilon);
if (epsilon <= 1E-6)
break;
end
counter = counter + 1;
x_current = x_next;
end
|Deneme: 1 | x_n: 1.0000000000 | x_(n+1): 2.5252621968 | Epsilon: 1.5252622
|Deneme: 2 | x_n: 2.5252621968 | x_(n+1): 2.7110298503 | Epsilon: 0.1857677
|Deneme: 3 | x_n: 2.7110298503 | x_(n+1): 2.7012901260 | Epsilon: 0.0097397
|Deneme: 4 | x_n: 2.7012901260 | x_(n+1): 2.7018592793 | Epsilon: 0.0005692
|Deneme: 5 | x_n: 2.7018592793 | x_(n+1): 2.7018261629 | Epsilon: 0.0000331
|Deneme: 6 | x_n: 2.7018261629 | x_(n+1): 2.7018280903 | Epsilon: 0.0000019
|Deneme: 7 | x_n: 2.7018280903 | x_(n+1): 2.7018279782 | Epsilon: 0.0000001



Yorumlar

  1. Donanımlı bir çalışma olmuş.Tebrik ederim.

    YanıtlaSil

Last Comments