The Newton Raphson Method

1 次查看(过去 30 天)
评论: Torsten ,2022-11-21
% Author - Ambarish Prashant Chandurkar
%The Newton Raphson Method
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
if err<epsilon %checking the amount of error at each iteration
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 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,:);
That gets you started, and you can finish the rest.

Community Treasure Hunt

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

Start Hunting!

Translated by