Filter löschen
Filter löschen

I am new to matab. I tried to solve a non linear equation with newton raphson and modified newton raphson method but got different solution. could you please help me find the error? please note I tried to run the code in two separate files.

5 Ansichten (letzte 30 Tage)
%%Newton raphson method
clc
clear all
close all
%%Initial condition
x0 =0.92;
maxiteration = 50;
tolX = 1e-04;
f = @(x) x^2-2*x+1; %function to be solved
%%Computation
x = x0;
xold = x0;
for i = 1:maxiteration;
df = 2*x-2;
x = x-f(x)/df;
err(i) = norm(x-xold);
xold = x;
if (err(i)<tolX)
break;
end
end
msg = ['The solution converged in ', num2str(i),'th iterations.'];
disp(msg)
msg1 = ['converged numerical solution(critical values):'];
disp(msg1)
fprintf('x = %.4f\n',x);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Modified newton Raphson method
clc
clear all
close all
%%Initial condition
x0 =0.92;
maxiteration = 50;
tolX = 1e-04;
f = @(x) x^2-2*x+1;
%%Computation
x = x0;
xold = x0;
for i = 1:maxiteration;
df = 2*x0-2;
x = x-f(x)/df;
err(i) = norm(x-xold);
xold = x;
if (err(i)<tolX)
break;
end
end
msg = ['The solution converged in ', num2str(i),'th iterations.'];
disp(msg)
msg1 = ['converged numerical solution:'];
disp(msg1)
fprintf('x = %.4f\n',x)

Antworten (1)

darova
darova am 23 Dez. 2019
Plot each iteration to see what it going on
fplot(f,[-5 5])
hold on
for i = 1:maxiteration;
% computations ...
yy = df*[-1 1]+f(x);
h(1) = plot([-1 1]+x,yy);
h(2) = plot(x,f(x),'.r');
pause(1)
delete(h)
end
hold off
  1 Kommentar
Hrishikesh Das
Hrishikesh Das am 23 Dez. 2019
thank you for your response , I am very new to matlab, I created two matlab files, one for newton raphson and another for modified newton raphson, I copied and pasted your code in each files and run them, but I really could not understand what is going on, Could you plz elabolate on that? I appreciate you help. Thank you.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by