Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parenthe

>> 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

I want to show the approach to the result by using the newton raphson method with the function value entered by the user, but it gives an error, how do I solve it?

Sign in to comment.

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)
Root identified at 3.141592
fprintf('Y-error %e\n',y)
Y-error 9.024789e-07
fprintf('Iteration number %d\n',k)
Iteration number 2
% visualize the initial and final values
fplot(f); hold on; grid on
plot([xguess x1],f([xguess x1]),'*')

Asked:

on 29 May 2022

Answered:

DGM
on 29 May 2022

Community Treasure Hunt

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

Start Hunting!