フィルターのクリア

Issue regarding output after running a script

1 回表示 (過去 30 日間)
David Hagan
David Hagan 2012 年 2 月 6 日
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

採用された回答

Walter Roberson
Walter Roberson 2012 年 2 月 6 日
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 件)

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by