I want it to find the solution with the Newton Raphson method with the function entered, but it gives an error and when I copy and paste the codes on another page, it pastes d

2 views (last 30 days)
clc; clear all;
syms x
E=input('Epsilon değerini giriniz E: ');
max=20;
x0=input('X0 değerini giriniz -> ');
f(x)=input('Fonksiyon değerini giriniz f(x):');
f1=diff(f);
for k=1:max
a=f1(x0);
if abs(a)<0.00001
disp('turevin değeri sıfıra çok yakın, algoritma durur')
break
end
x1=x0-f(x0)/f1(x0);
hata=abs(x1-x0);
x0=x1;
y=f(x0);
if(abs(y)<epsilon)
break
end
end
disp('root')
x1;
disp('error')
y;
disp('iteration')
k;
  3 Comments
Jan
Jan on 27 May 2022
The value of epsilon depends on what you want. You can choose this freely as upper limit, which you accept as "almost zero".
By the way, avoid redefining built-in functions as max() as a variable. This is no error, but if you try to use the function max() later the level of confusion is high.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 May 2022
fstr = input('Fonksiyon değerini giriniz f(x):', 's');
syms x
fsym = str2sym(fstr);
f = matlabFunction(fsym, 'vars', x);
f1sym = diff(fsym) ;
f1 = matlabFunction(f1sym, 'vars', x) ;

Community Treasure Hunt

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

Start Hunting!