Newton Raphson!but with multiple initial values :/
이전 댓글 표시
This works for one initial value. Now I want to do the exact thing but for 2 more initial values. Where they all are x0= [0.5,1.5,2.5]; and the outputs will be r = [r1 , r2 , r3]. Any help would be appreciated, thanks.
r1 = newtonr()
function[r]= newtonr
i = 0;
x0=0.5;
n = 5;
while i < 5
f = x0^4 - 9*x0^3+29*x0^2-39*x0+18;
df= 4*x0^3 - 27*x0^2 +58*x0 -39;
xnew= x0 - f/df;
i= i+1
x0=xnew;
disp(x0);
if i== 5
r= xnew;
disp('root is')
disp(x0)
break
end
end
end
채택된 답변
추가 답변 (1개)
Michael Soskind
2020년 5월 5일
편집: Michael Soskind
2020년 5월 5일
0 개 추천
Hi Alex,
Edited: Please note, David Hill's answer is the one I recommend.
As a note:
To make calculations work on multiple values within an array, the operator should be preceded with a '.'
This is the case for multiplication, division, and raising to a power.
This is the main modification that David makes in his code, as well as implementing functions for the function and its derivative. This is in general better practice.
카테고리
도움말 센터 및 File Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!