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!