Plot data from while loop
Afficher commentaires plus anciens
I've written an algorithm that can approximate the root of a multivariable function using the Newton-Raphon method. The algorithm, given 2 multivariable functions and 2 guess values, runs through an equation multiple times, returning the approximate root and error each iteration. I am trying to plot the error against the iteration. However, when i do so, i get a blank plot. How would I approach plotting this error data (eax and eay [found in part 3]) from the while loop against the iteration number?
Thank you
Below is the code:
disp(['<strong>Method: </strong>' 'Newton-Raphson for Simultaneous Equations'])
n = input('Desired Accuracy: ');
e = 0.5*(10^(2-n));
syms x y
Data_Vector = [f1 f2 diff(f1,x) diff(f1,y) diff(f2,x) diff(f2,y)];
Iteration = 0;
x=input('x_guess: ');
y=input('y_guess: ');
while (1)
disp('------------')
Iteration = Iteration + 1;
disp(['<strong>Iteration: </strong>' num2str(Iteration) ])
%Part 1: Initializing
x;
y;
i = Iteration - 1;
disp([ '(x_' num2str(i) ',' 'y_' num2str(i) ')' ' = ' '(' num2str(x) ',' num2str(y) ')'])
Data_Vector_Num1 = subs(Data_Vector);
Data_Vector_Num2 = double(Data_Vector_Num1);
%Part 2: Evaluation
%Part 2A: X - Root
xr = x - (Data_Vector_Num2(1)*Data_Vector_Num2(6)-Data_Vector_Num2(2)*Data_Vector_Num2(4))/(Data_Vector_Num2(3)*Data_Vector_Num2(6)-Data_Vector_Num2(4)*Data_Vector_Num2(5));
%Part 2B: Y - Root
yr = y - (Data_Vector_Num2(2)*Data_Vector_Num2(3)-Data_Vector_Num2(1)*Data_Vector_Num2(5))/(Data_Vector_Num2(3)*Data_Vector_Num2(6)-Data_Vector_Num2(4)*Data_Vector_Num2(5));
%Part 3: Approximate Error
eax = abs((xr-x)/xr)*100;
eay = abs((yr-y)/yr)*100;
%Part 4: Resetting X & Y
x=xr;
y=yr;
%Part 5: Root Check
f1_rc_a = subs(f1);
f1_rc_b = double(f1_rc_a);
f2_rc_a = subs(f2);
f2_rc_b = double(f2_rc_a);
disp([ '(x_' num2str(Iteration) ',' 'y_' num2str(Iteration) ')' ' = ' '(' num2str(x) ',' num2str(y) ')'])
disp(['Value of Root: ' 'f1(' num2str(x) ',' num2str(y) ')' ' = ' num2str(f1_rc_b) ' || ' 'f2(' num2str(x) ',' num2str(y) ')' ' = ' num2str(f2_rc_b)])
disp(['Approximate Percent Error: ' 'x:' num2str(eax) '%' ' y:' num2str(eay) '%'])
if eax < e && eay < e
break,end
end
%Part 6: Summary
disp('=======================')
disp('<strong>Summary of Results: </strong>')
disp(['<strong>Method: </strong>' 'Newton-Raphson for Simultaneous Equations'])
disp(['<strong>Number of Iterations:</strong> ' num2str(Iteration)])
disp('<strong>Approximate Root: </strong>')
[round(x,n,'significant') round(y,n,'significant')]
disp(['<strong>Value of Root: </strong>' 'f1(' num2str(x) ',' num2str(y) ')' ' = ' num2str(f1_rc_b) ' ' 'f2(' num2str(x) ',' num2str(y) ')' ' = ' num2str(f2_rc_b)])
disp([ '<strong>Accuracy: </strong>' num2str((n)) ' Significant Figures'])
Here, I'll attatch a sample output:
Method: Newton-Raphson for Simultaneous Equations
Desired Accuracy: 4
x_guess: 1.5
y_guess: 3.5
------------
Iteration: 1
(x_0,y_0) = (1.5,3.5)
(x_1,y_1) = (2.036,2.8439)
Value of Root: f1(2.036,2.8439) = -0.064375 || f2(2.036,2.8439) = -4.7562
Approximate Percent Error: x:26.3272% y:23.0715%
------------
Iteration: 2
(x_1,y_1) = (2.036,2.8439)
(x_2,y_2) = (1.9987,3.0023)
Value of Root: f1(1.9987,3.0023) = -0.0045199 || f2(1.9987,3.0023) = 0.049571
Approximate Percent Error: x:1.8676% y:5.2764%
------------
Iteration: 3
(x_2,y_2) = (1.9987,3.0023)
(x_3,y_3) = (2,3)
Value of Root: f1(2,3) = -1.2861e-06 || f2(2,3) = -2.214e-05
Approximate Percent Error: x:0.064969% y:0.076305%
------------
Iteration: 4
(x_3,y_3) = (2,3)
(x_4,y_4) = (2,3)
Value of Root: f1(2,3) = 1.501e-13 || f2(2,3) = 2.7769e-12
Approximate Percent Error: x:8.0619e-07% y:1.9554e-05%
=======================
Summary of Results:
Method: Newton-Raphson for Simultaneous Equations
Number of Iterations: 4
Approximate Root:
ans =
2 3
Value of Root: f1(2,3) = 1.501e-13 f2(2,3) = 2.7769e-12
Accuracy: 4 Significant Figures
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Calculus dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!