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
Torsten
2022년 3월 31일
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.
Ethan Cole
2022년 3월 31일
Ethan Cole
2022년 3월 31일
Torsten
2022년 3월 31일
Then show us the code together with the error message and maybe someone will be able to help.
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
2022년 3월 31일
편집: Cris LaPierre
2022년 3월 31일
채택된 답변
추가 답변 (2개)
Sam Chak
2022년 3월 31일
Hi @Ethan Cole
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;
John
2023년 7월 31일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


