Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parenthe
Show older comments
>> clc; clear all;
E=input('Epsilon değerini giriniz E: ');
max=20;
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) ;
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;
1 Comment
Aleyna Dolar
on 29 May 2022
Answers (1)
I don't see anything that would cause that error, but there are other errors.
clc; clf
fstr = 'sin(x)'; %input('Fonksiyon değerini giriniz f(x):', 's');
xguess = 2.8; % x0 is undefined
epsilon = 0.0001; %input('Epsilon değerini giriniz E: '); % E is unused (is this the same epsilon?)
maxsteps = 20; % you're overloading max(). it's not causing an error, but it's a bad idea
syms x
fsym = str2sym(fstr);
f = matlabFunction(fsym, 'vars', x);
f1sym = diff(fsym) ;
f1 = matlabFunction(f1sym, 'vars', x);
x0 = xguess; % just doing this so that the user's guess can be plotted later
for k=1:maxsteps
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
% you can use fprintf() to display dynamic text
fprintf('Root identified at %f\n',x1)
fprintf('Y-error %e\n',y)
fprintf('Iteration number %d\n',k)
% visualize the initial and final values
fplot(f); hold on; grid on
plot([xguess x1],f([xguess x1]),'*')
Categories
Find more on Newton-Raphson Method in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!