I need to write a program that a user inputs a function, the function's derivative, and their initial guess of the root, and then with the Newton Raphson method find the root (or a number with a very small error close to the root).

I need to write a program that a user inputs a function, the function's derivative, and their initial guess of the root, and then with the Newton Raphson method find the root (or a number with a very small error close to the root). We were told to use inline(), but I'm completely lost, any help would be great.
func = inline('f','x');
dfunc = inline('df','x');
f = input('Enter a function (MATLAB COMPATIBLE) in terms of x:\n', 's');
df = input('Enter the derivative of that function (MATLAB COMPATIBLE):\n', 's');
xr = input('Enter your initial guess of the root:\n');
iter=0;
while(1)
xold=xr;
xr= xr-func(xr)/dfunc(xr);
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
end

 Accepted Answer

m - use inline after you have requested the function expression from the user. For example,
fExpr = input('Enter a function (MATLAB COMPATIBLE) in terms of x:\n', 's');
f = inline(fExpr);
dfExpr = input('Enter the derivative of that function (MATLAB COMPATIBLE):\n', 's');
df = inline(dfExpr);
For example, the user could enter a function string as
x^2
and its corresponding derivative as
2*x
Then the inline objects/functions f and df could be used to evaluate any input as
>> f(3)
ans =
9
>> df(3)
ans =
6
Try incorporating the above into your algorithm.
Note also that your algorithm appears to be incomplete. How do you expect to break out once the root has been found? What is ea used for? You will need a maximum number of iterations check on your while loop so that you don't get stuck in an infinite loop. Also, since manipulating floating point numbers, you cannot compare them to zero but rather you will need to use a tolerance check to see if the value is "close" to zero. Since this is homework, I will leave this as an exercise for you to complete.

3 Comments

Yea, I figured I needed to add max iterations and an error check. However, this is my new code, but I keep getting these errors: Error using inlineeval (line 14) Error in inline expression ==> f Undefined function or variable 'f'.
Error in inline/subsref (line 23) INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
Error in HW5 (line 16) xr= xr-func(xr)/dfunc(xr);
In the first error, I don't understand how f isn't defined. The second error, I don't know what it's even trying to suggest, and the third error, I can not find anything wrong with that line.
f = input('Enter a function (MATLAB COMPATIBLE) in terms of x:\n', 's');
func=inline('f','x');
df = input('Enter the derivative of that function (MATLAB COMPATIBLE):\n', 's');
dfunc=inline('df','x');
xr = input('Enter your initial guess of the root:\n');
maxiters=50.00000;
es=.0010000;
iter=0;
while(1)
xold=xr;
xr= xr-func(xr)/dfunc(xr);
iter=iter+1;
if xr~=0, ea=abs((xr-xrold)/xr)*100;end
if ea <= es || iter >= maxiters, break, end
end
(BY THE WAY, THANK YOU SO MUCH FOR THE HELP)

Sign in to comment.

More Answers (0)

Asked:

m
m
on 27 Sep 2015

Commented:

m
m
on 28 Sep 2015

Community Treasure Hunt

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

Start Hunting!