MATLAB Answers

can anyone help me to find the error ?

1 view (last 30 days)
diadalina
diadalina on 28 Feb 2020
Commented: diadalina on 28 Feb 2020
syms x1
esp=10^-10;
x0=0.5;
nmax=100;
df=@(x1)(diff(f(x1)))
[ xapp,it] = newton(x0,esp,f,df,nmax)
where:
function [ xapp,it ] = newton(x0,esp,f,df,nmax)
it=0;
i=1;
x=[];
x(1)=x0;
x(2)=x(1)-f(x(1))/df(x(1));
while abs(x(i+1)-x(i))>esp && it>=nmax
if df(x(i))==0
disp('division par zero est impossible')
break
end
i=i+1;
it=it+1;
x(i)=x(i-1)-f(x(i-1))./df(x(i-1));
end
xapp=x(it );
and
function y=f(x)
y=(x+2).^(2/5)
end

  2 Comments

Guillaume
Guillaume on 28 Feb 2020
If you get an error, give us the full text of the error message you get, without any modification
If you don't get an error but the code doesn't behave as you expected, then please explain what it does and how it differs from what you expected.
diadalina
diadalina on 28 Feb 2020
df =
function_handle with value:
@(x1)(diff(f(x1)))
Not enough input arguments.
Error in f (line 2)
y=(x+2).^(2/5)
Error in tp4 (line 12)
[ xapp,it] = newton(x0,esp,f,df,nmax)

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 28 Feb 2020
[ xapp,it] = newton(x0,esp,@f,df,nmax)
should probably fix the problem.

  3 Comments

diadalina
diadalina on 28 Feb 2020
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in newton (line 6)
x(2)=x(1)-f(x(1))./df(x(1));
Error in tp4 (line 12)
[ xapp,it] = newton(x0,esp,@f,df,nmax)
>>
Guillaume
Guillaume on 28 Feb 2020
Oh yes, your df function does not work at all. Since it calls diff it's guaranteed to return one less element than the input so you're always going to get this error and since df is given a scalar, diff is always going to return [].
It's not clear what that df function is trying to do.
diadalina
diadalina on 28 Feb 2020
the derivative function of f

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!