Help with newton-raphson

Construct a MATLAB function called newtonRaphson that implements n steps of the
Newton-Raphson method to find the root of some function f(z).
Your function should receive the following three inputs (in this order): an inline function
f; an initial guess cO; and the number of steps to be computed, n.
Your function should return a vector called c with n + 1 entries containing a vector of all
the approximations computed. The first entry in c should contain the initial guess; the
remaining n entries should contain the approximations that your function has computed
in order.
The definition of your function should look like this:
function c=newtonRaphson(f,c0,n)
Im unsure where to start as every type of code i try to use ends up coming back with an error. please help

6 Comments

There are so many MATLAB Newton codes in the net.
If you don't know where to start, you should choose one of them and try to understand it.
i have tried to follow codes from the web and also previous worksheets and this is what was recommended
function c=newtonRaphson(f,c0,n)
syms x;
df=diff(f,x);
c(1)=c0;
for i=1:n
c(i+1)=c(i)-(f(c(i))/df(c(i)))
end
display (c);
end
however i always get an error about Index exceeds the number of array elements. Index must not exceed 1. and am unsure why this is or how to fix this
Then show us the code together with the error message and maybe someone will be able to help.
Sam Chak
Sam Chak on 31 Mar 2022
Edited: Sam Chak on 31 Mar 2022
I see... @Ethan Cole, you have tried writing many codes for the Newton–Raphson Method in solving root-finding problem .
Most likely you are a little rigid (restricted) and overwhelmed by the technical terms in the instructions, such as to have an inline function f, the number of steps to be computed, n, and then the solver shall return a vector called c with n + 1 entries. If you look at the Newton–Raphson Iteration Formula in mathematics book:
,
it definitely does not tell you what an inline function is, what steps are, and what a vector c is. That's why you don't know where to begin.
Can you put up the latest version of your code here?
Ethan Cole
Ethan Cole on 31 Mar 2022
Edited: Cris LaPierre on 31 Mar 2022
i am using aprogramme called matlab grader so a screenshot of the code is alli can give

Sign in to comment.

 Accepted Answer

Torsten
Torsten on 31 Mar 2022
Edited: Torsten on 31 Mar 2022
f = inline('x.^2-3','x');
c0 = 2;
n = 10;
c = newtonRaphson(f,c0,n)
function c = newtonRaphson(f,c0,n)
df = @(x) (f(x+1.0e-6)-f(x))*1e6;
c = zeros(n+1,1);
c(1) = c0;
for i = 1:n
c(i+1) = c(i)-(f(c(i))/df(c(i)))
end
end

13 Comments

through matlab grader i cant use the inline function,which is where i am getting stuck.the code needs to be able to run a variety of functions
But you were told to define f as an inline function. What is MATLAB grader ? Does the code throw an error when executed with usual MATLAB ?
i willcheck if it throws an error now, if youlook aboveat the screenshot i posted the programme gives a function and variables for the newton-raphson
Torsten
Torsten on 31 Mar 2022
Edited: Torsten on 31 Mar 2022
yes, you did not initialize c:
c = zeros(n+1,1)
at what point in the code should that go. aologies im extremely new to all this
At the point where I did it in my code.
i have added it into my code but its still throwing an error.ive captured the question,my code and error if that will help
After the line
df = diff(f,x)
include the line
df = matlabFunction(df);
in your code.
thank you
Wait @Ethan Cole. You need to test various nonlinear functions. Don't submit first. The assignment doesn't say that you cannot do have hybrid approach to check the accuracy of the solution. Some nonlinear function may require more iterations to converge to the solution.

linking from the previous question, any chance you can help me convert it correctly to satisfy for all

Torsten
Torsten on 31 Mar 2022
Edited: Torsten on 31 Mar 2022
Isn't this exactly Sam Chak's code ?
function c = newtonRaphson2(f,c0,epsilon)
syms x
df = matlabFunction(diff(f,x));
error = 2*epsilon;
itermax = 30;
cold = c0;
iter = 0;
flag = 0;
while error > epsilon
iter = iter + 1;
cnew = cold - f(cold)/df(cold);
error = abs(cnew-cold)/max(1,abs(cold));
if iter > itermax
disp('Iteration limit exceeded.');
flag = 1;
break
end
cold = cnew;
end
if flag == 0
c = cnew;
else
c = c0;
end
end

Sign in to comment.

More Answers (2)

The method presented by @Torsten satisfies your requirements in your assignment. Here is an alternative. The main difference is the termination condition, where the program does not execute a fixed number of iterations, but the interation will stop once the condition is satisfied.
format long g
f = @(x) x^2 - 3; % to find the square root of 3
epsilon = 1e-6;
x0 = 1;
[c, Iterations] = NewtonRaphson(f, x0, epsilon)
function [x, Iter] = NewtonRaphson(f, x0, epsilon)
Iter = 0;
df = @(x) (f(x+1.0e-6)-f(x))*1e6;
x1 = x0 - f(x0)/df(x0);
while abs(f(x1)) > epsilon
x0 = x1;
x1 = x0 - f(x0)/df(x0);
Iter = Iter + 1;
end
x = x1;

2 Comments

Ive seen a similar codelikethis online,the only downside is that i have tostay within certain inputs that being f-function, c0- intitial and n-number of steps to be computed
No worries. I understand that the assignment requires you to input the number of iterations/steps. @Torsten will surely fix it. But I wonder if you put n = 1e9, will the MATLAB Grader going to execute the 1 billion iterations? 😅

Sign in to comment.

John
John on 31 Jul 2023
function [p, PN] = Newton_371(p0,N,tol,f,fp)
p=p0;
PN(1)=p0;
for n= 1:N
p=p-f(p)/fp(p);
PN(n+1) =p;
if abs(p-PN(n)) <=tol
break
end
end
end

Asked:

on 31 Mar 2022

Answered:

on 31 Jul 2023

Community Treasure Hunt

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

Start Hunting!