% 근을 포함하는 구간을 찾음
% 두 지점의 함숫값이 다르면, ( f(xl) * f(xu)
% 증분의 크기가 작을 수록, 계산 시간이 많이 걸린다.
% 증분의 크기가 클 수록, 근을 놓치게 된다.
% 증분의 크기와 관계 없이 중근을 찾는데는 어려움이 따른다.
function xb = incsearch(func,xmin,xmax,ns)
% incsearch: incremental search root locator
% xb = incsearch(func,xmin,xmax,ns):
% finds brackets of x that contain sign changes
% of a function on an interval
% input:
% func = name of function
% xmin, xmax = endpoints of interval
% ns = number of subintervals (default = 50)
% output:
% xb(k,1) is the lower bound of the kth sign change
% xb(k,2) is the upper bound of the kth sign change
% If no brackets found, xb = [].
if nargin
if nargin
% Incremental search
x = linspace(xmin,xmax,ns); %x의 구간을 잡아서 일정 등분을 한다.
f = func(x);
nb = 0; xb = []; %xb is null unless sign change detected
for k = 1:length(x)-1
if sign(f(k)) ~= sign(f(k+1))
%check for sign change
%부호가 다르면 두 두간 사이에 적어도 하나의 근을 가진다는 뜻이다.
nb = nb + 1;
xb(nb,1) = x(k);
xb(nb,2) = x(k+1);
end
end
if isempty(xb) %display that no brackets were found
disp('no brackets found')
disp('check interval or increase ns')
else
disp('number of brackets:') %display number of brackets
disp(nb)
end
증분 탐색법 관련해서 이런 코드를 발췌했는데, 교수가 무턱대고 매틀랩 배운적도없는데 등간격 탐색법의 매틀랩 코드를 짜오라고 했습니다.
해당 m파일을 열어 넣어도 어떤 명령어, 변수, 함수 정의는 어떻게하는지도 모르는데
0.02 간격으로 x(0)가 -1일때 수렴한 근을 구하는 코드를 해당 코드로 만들 수 있나요?
댓글 0