Need help altering code to so it records all values, not just the final ones.

1 回表示 (過去 30 日間)
Grant Piephoff
Grant Piephoff 2017 年 9 月 15 日
回答済み: Image Analyst 2017 年 9 月 16 日
Hello, I need help putting all of the i, xi, and Err values in a table with the column headers "Iteration Number", "xi", and "Error".
% 4)Use Newton Raphson's method with intial guess of 4.5
% to find one of the roots. The stop condition is error<0.1%.
% part
%Err=relative approx error
%xi=initial guess
%y=function
%der=derivative
%i=iteration number
fprintf(' \n 4) Newton Raphsons Method \n')
fprintf(' \n a) Initial guess of 4.5 \n')
Err=1;
xi=4.5;
i=0;
while Err>.001
y=(0.5*xi.^3)-(4*xi.^2)+(6*xi)-1;
der=1.5*xi.^2+8*xi+6;
xii=xi-(y/der);
Err=abs((xii-xi)/(xii));
xi=xii;
i=i+1;
end
disp('Root is');
disp(xi);
disp('Relative Approximation Error');
disp(Err);

採用された回答

Image Analyst
Image Analyst 2017 年 9 月 16 日
To use a table, try this:
clc; % Clear the command window. close all; % Close all figures (except those of imtool.) clear; % Erase all existing variables. Or clearvars if you want. workspace; % Make sure the workspace panel is showing. format long g; format compact; fontSize = 20;
% 4)Use Newton Raphson's method with intial guess of 4.5
% to find one of the roots. The stop condition is error<0.1%.
% part
%Err=relative approx error
%xi=initial guess
%y=function
%der=derivative
%i=iteration number
fprintf(' \n 4) Newton Raphsons Method \n')
fprintf(' \n a) Initial guess of 4.5 \n')
Err=1;
xi=4.5;
i=0;
t = table(0, 0, 0, 'VariableNames', {'Iteration_Number', 'xi', 'Error'})
while Err>.001
y=(0.5*xi.^3)-(4*xi.^2)+(6*xi)-1;
der=1.5*xi.^2+8*xi+6;
xii=xi-(y/der);
Err=abs((xii-xi)/(xii));
xi=xii;
t{i+1, 'Iteration_Number'} = i;
t{i+1, 'xi'} = xi;
t{i+1, 'Error'} = Err;
i=i+1;
end
t % Display table.
disp('Root is');
disp(xi);
disp('Relative Approximation Error');
disp(Err);
You'll see:
t =
32×3 table
Iteration_Number xi Error
________________ ________________ ____________________
0 4.63039723661485 0.0281611339052593
1 4.75460242909775 0.0261231500078256
2 4.87206180740243 0.0241087619467844
3 4.98240338473024 0.0221462552923705
4 5.08542318931628 0.0202578626696143
5 5.18106638669633 0.0184601374006014
6 5.26940551151001 0.0167645334223594
7 5.35061769927527 0.0151780957507514
8 5.42496239431477 0.0137041862478913
9 5.4927605844669 0.0123431904794569
10 5.55437622584841 0.0110931702996232
11 5.61020019850971 0.00995044217426145
12 5.6606368855061 0.00891007284454638
13 5.70609329106083 0.00796629203836284
14 5.74697049878916 0.00711282713856731
15 5.78365720623546 0.00634316767714857
16 5.8165250448515 0.00565076886329729
17 5.84592539385265 0.00502920359402285
18 5.87218741288532 0.00447227194674285
19 5.89561704494568 0.0039740763149545
20 5.91649677224753 0.0035290693303166
21 5.93508594010807 0.0031320806552974
22 5.95162149505599 0.00277832771483392
23 5.96631901188352 0.00246341451039634
24 5.97937390957255 0.00218332184714708
25 5.99096277770451 0.00193439160982435
26 6.00124475319516 0.00171330714101836
27 6.0103629022366 0.00151707129665137
28 6.01844557452832 0.00134298336532802
29 6.02560770661793 0.00118861572779571
30 6.03195205882755 0.00105179088754994
31 6.03757037616482 0.000930559312310236
Root is
6.03757037616482
Relative Approximation Error
0.000930559312310236

その他の回答 (1 件)

カテゴリ

Help Center および File ExchangeNewton-Raphson Method についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by