Strange Problem when i write same code in function
조회 수: 1 (최근 30일)
이전 댓글 표시
This is my original code for Bisection Methods of Numerical Analysis
% A program in matlab
% bisection method to find the roots of x^3+x^3+5=0
xleft = -2.5 ; xright = -1.5 ; n = 20
format longe
% Input: xleft,xright = left and right brackets of the root
% n = (optional) number of iterations; default: n =15
% Output: x = estimate of the root
if nargin<3, n=15; end % Default number of iterations
a = xleft; b =xright; % Copy orig bracket to local variables
fa = a^3 + a^2 + 5; % Initial values
fb = b^3 + b^2 +5; % Initial values
fprintf(' k a xmid b f(xmid)\n');
for k=1:n
xm = a + 0.5*(b-a); % Minimize roundoff in the midpoint
fm = xm^3 + xm^2 +5; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n',k,a,xm,b,fm);
if sign(fm) == sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end
**************************************** Now i am writing the same function by manually giving input
my main file is this one
input('Type value of a \n');
input('Type value of b\n');
bisec(a,b)
and my function file is this one
function bisec(a,b)
format longe
n=20;
% Output: x = estimate of the root
*fa = a^3 + a^2 + 5; % Initial values
fb = b^3 + b^2 +5; % Initial values* _ITALIC TEXT_
fprintf(' k a xmid b f(xmid)\n');
for k=1:n
xm = a + 0.5*(b-a); % Minimize roundoff in the midpoint
fm = xm^3 + xm^2 +5; % f(x) at midpoint
fprintf('%3d %12.8f %12.8f %12.8f %12.3e\n',k,a,xm,b,fm);
if sign(fm) == sign(fa)
a = xm; fa = fm;
else
b = xm; fb = fm;
end
end
end
But my results are comming different plz guide me
댓글 수: 0
채택된 답변
추가 답변 (2개)
참고 항목
카테고리
Help Center 및 File Exchange에서 MuPAD에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!