Untitled

 avatar
unknown
plain_text
a year ago
774 B
3
Indexable
% To calculate the root of given equation using N-R method.

close all;
clear variables;
clc;
syms x;
func = input('Enter function f(x):');
f=inline(func);
df=diff(f(x));
g=inline(df);
disp(f);
disp(g);
E=0.0005;

% User input section

x0=input('Enter the initial value of root x0=');
f0=f(x0);
g0=g(0);

%Calculation section

x1=x0-f0/g0;
f1=f(x1);
result=[x0,f0,g0,x1,f1];
disp('_______________________________________________________');
disp('   x0        f(x0)        df(x0)        x1        f(x1)');
disp('________________________________________________________');
disp(result);

while(abs(x1-x0)>E)
    x0=x1;
    f0=f(x0);
    g0=g(x0);
    x1=x0-f0/g0;
    result=[x0,f0,g0,x1,f1];
    disp(result);
end

% Output Section.

Editor is loading...
Leave a Comment