필터 지우기
필터 지우기

Newtons Method, help calling anon function inside of function file

조회 수: 2 (최근 30일)
Peter M Hogan
Peter M Hogan 2019년 2월 17일
댓글: Walter Roberson 2019년 2월 18일
Ok so I have been tasked with writing a file myNewton which finds the root of any function given the listed inputs. I am maybe misunderstanding how to implement the method in general, but my matlab related question pertains more to the nature of calling anony functions inside of function files. I do not do this here but I anticipate that the best way to solve this problem is to do that. I am basically confused! Any help is appreciated, here is what I have so far.
function p = myNewton(f,fprime,x0,tol)
%input f, anonymous function for root finding problem
%input fprime, anonlymous function, the derivative of f
%input x0, an initial guess
%input tol, a tolerance (method will stop when successive iterates are within tol of each other)
%output p, a root f(p) = 0
x(1) = x0 - (f(x0)/fprime(x0));
k = 2;
ex(1) = abs(x(1)-x0);%error
while (ex(k-1) >= tol) %while my error is greater than tolerance
x(k) = x(k-1) - (f(x(k-1))/fprime(x(k-1)));
k = k+1;
end
end
code to call function;
%As a test, the root of f(x) = x+2^x is -0.64118574
% define f, fprime, an initial guess (you can use 1), and use a tolderance of 1e-6
% send these into the function and store the result as the variable p
format long % to display more digits
x0=1;
tol=1e-6;
myNewton('x+2^x','diff(x+2^x)',x0,tol)

답변 (1개)

Walter Roberson
Walter Roberson 2019년 2월 17일
You are calling the anonymous function properly in your code.
Your problem is that you are not passing in an anonymous function. You are passing in character vectors.
myNewton(@(x) x+2^x, @(x) 2^x * log(2) + 1, x0, tol)
You cannot ask to diff(x+2^x) unless you have the symbolic toolbox.
  댓글 수: 4
Peter M Hogan
Peter M Hogan 2019년 2월 18일
편집: Peter M Hogan 2019년 2월 18일
function p = myNewton(f,fprime,x0,tol)
%input f, anonymous function for root finding problem
%input fprime, anonlymous function, the derivative of f
%input x0, an initial guess
%input tol, a tolerance (method will stop when successive iterates are within tol of each other)
%output p, a root f(p) = 0
x(1) = x0 - (f(x0)/fprime(x0));
k = 2;
ex(1) = abs(x(1)-x0);%error
while (ex(k-1) >= tol) %while my error is greater than tolerance
x(k) = x(k-1) - (f(x(k-1))/fprime(x(k-1)));
k = k+1;
ex(k)=abs(x(k)-x0);
end
p=x(k);
end
this is my new code. if you have any insights, i want p to express the root of the function. i am not sure where i am going wrong.
Walter Roberson
Walter Roberson 2019년 2월 18일
Where do you assign ex(2) ?
When k = 2 you use ex(k-1) which is ex(1), which you have assigned to specifically, so that part is good. You then increment k from 2 to 3, and assign to ex(3). Your while loop then tests ex(3-1) which is ex(2) but you did not assign to ex(2)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by