Issue regarding output after running a script
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 6 Feb. 2012
That isn't a script, it is a function.
To get rid of the "ans=", invoke your function as
NR3;
with the semi-colon.
Please also re-read the documentation for fprintf(), and in particular pay attention to what the output from it is.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!