필터 지우기
필터 지우기

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 CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by