Issue regarding output after running a script
Afficher commentaires plus anciens
I wrote a function that uses the Newton-Raphson method to calculate the value of y for the following equations: x^2 + y^2 - 1 , x*y - 0.5 .
The output gives me the Results followed by ans = 11 (which im assuming is the number of iterations it took to converge.) How do I get rid of the "ans = " part?
The last y value converges to: 0.7071
ans =
11
function [result1,result2] = NR3()
syms x y
f1 = input('\nPlease Enter equation 1: ');
f2 = input('\nPlease Enter equation 2: ');
y0 = input('\nPlease Enter an initial y value: ');
tol = input('\nPlease Enter a tolerance: ');
f_x = solve(f2,x);
f_y = subs(f1,x,f_x);
dfdy = diff(f_y,y);
d_w = [];
yy_w = [];
yy_w(1) = y0;
tol1 = 100;
i = 1;
while tol1 > tol
i = i + 1;
d_w(i-1) = subs(f_y,y,yy_w(i-1))/subs(dfdy,y,yy_w(i-1));
yy_w(i) = yy_w(i-1) - d_w(i-1);
tol1 = abs(yy_w(i) - yy_w(i-1));
end
format long
if isnan(yy_w(end))
result1 = fprintf('\nError: Divergence! ');
result2 = fprintf('\nPlease try a different initial value. \n');
else
result1 = fprintf('\n\nResults\n\n');
result2 = fprintf('The y value converges to: %7.4f\n',yy_w(end));
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Symbolic Math Toolbox dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!