# How to add iterations in Newton Raphson code of Kepler Equation

3 ビュー (過去 30 日間)
imran shaikh 2021 年 9 月 19 日

function [E] = Kepler(M,e,tol)
% File Kepler.m solves Kepler Equation
% Input: M = Mean anomoly in radians
% e = Eccentricity
% tol = tolerance
% Output: E = Eccentric anomoly in radians
En = M; % Using M as the first guess
Ens = En - (En-e*sin(En) - M)/(1 - e*cos(En)); % Newton Raphson iteration
while(abs(Ens-En)>tol) % Setting the tolerance as limit for iteration
En = Ens; % establishing forward step of iteration
Ens = En- (En - e*sin(En) - M)/(1 - e*cos(En)); % Re-calculation of new step
end
E = Ens; % update E
Matlab version used: Matlab R2020b
The above code works to provide a value of E, but how do i answer below question with only two input variables and add iterations to the code?

サインインしてコメントする。

### 採用された回答

Alan Stevens 2021 年 9 月 21 日
Like this
M = 0.908;
e = 0.725;
tol = 10^-6;
[E, its] = Kepler(M,e,tol);
E = 1.6579 after one iteration
disp(['E = ' num2str(E) ' after ' num2str(its), ' iterations'])
E = 1.6317 after 4 iterations
function [E,its] = Kepler(M,e,tol)
% File Kepler.m solves Kepler Equation
% Input: M = Mean anomoly in radians
% e = Eccentricity
% tol = tolerance
% Output: E = Eccentric anomoly in radians
En = M; % Using M as the first guess
Ens = En - (En-e*sin(En) - M)/(1 - e*cos(En)); % Newton Raphson iteration
its = 0;
while(abs(Ens-En)>tol) % Setting the tolerance as limit for iteration
En = Ens; % establishing forward step of iteration
Ens = En- (En - e*sin(En) - M)/(1 - e*cos(En)); % Re-calculation of new step
its = its+1;
if its==1
disp(['E = ' num2str(Ens) ' after one iteration'])
end
end
E = Ens;
end
##### 1 件のコメント表示非表示 なし
imran shaikh 2021 年 9 月 22 日
Thanks Alan, I will give it a try

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by