The Newton Raphson Method

조회 수: 7 (최근 30일)
Bruce
Bruce 2022년 11월 21일
댓글: Torsten 2022년 11월 21일
% Author - Ambarish Prashant Chandurkar
%The Newton Raphson Method
clc;
close all;
clear all;
syms x;
f=x*exp(x)-1; %Enter the Function here
g=diff(f); %The Derivative of the Function
n=input('Enter the number of decimal places:');
epsilon = 5*10^-(n+1)
x0 = input('Enter the intial approximation:');
for i=1:100
f0=vpa(subs(f,x,x0)); %Calculating the value of function at x0
f0_der=vpa(subs(g,x,x0)); %Calculating the value of function derivative at x0
y=x0-f0/f0_der; % The Formula
err=abs(y-x0);
if err<epsilon %checking the amount of error at each iteration
break
end
x0=y;
end
y = y - rem(y,10^-n); %Displaying upto required decimal places
fprintf('The Root is : %f \n',y);
fprintf('No. of Iterations : %d\n',i);
  댓글 수: 1
Torsten
Torsten 2022년 11월 21일
Why did you change your former question ? Don't you think this is impolite against William Rose who tried to answer it ?

댓글을 달려면 로그인하십시오.

답변 (1개)

William Rose
William Rose 2022년 11월 21일
It appears that your x is a 2-coulmn array where column1 = theta, column 2=phi. I assume you have a function pendulum(), which returns a 1-by-2 vector whose values are .
Does the code you supplied run without error? The code you supplied appears to implement a forward Euler method. You will need to change what happens inside the loop for i=1:n,..., end, if you want to implement the midpoint method.
for i=1:n
thalf = t(i) + dt/2; t(i+1)=t(i)+dt;
xhalf = x(i,:) + (dt/2)*pendulum(thalf,xhalf);
%The equaiton above is an implicit equaiton, because xhalf appears on
%both sides. You will have to do some analysis of the derivative
%function inside pendulum() to solve this.
x(i+1,:) = 2*xhalf-x(i,:);
end
That gets you started, and you can finish the rest.

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by