필터 지우기
필터 지우기

Creating a driver routine for my function

조회 수: 4 (최근 30일)
Jason
Jason 2023년 10월 4일
이동: Torsten 2023년 10월 4일
Hello,
I am new to MATLAB and going through my textbook example, I have this function that calls two other functions(f and df). It works well when I call it but now I am trying to figure out how to create a driver routine to test this function that specifies the input arguments and calls newtfun.
Below is newtun:
function [x, f, conv] = newtfun(fh, dfh, x0)
steps = 0;
x = x0;
re = 1e-8;
myrel = 1;
while myrel > re & (steps<20)
xold=x;
x=x-feval(fh,x)/feval(dfh,x);
steps=steps+1;
disp([x feval(fh,x)])
myrel = abs(x-xold); %Changing myrel not to divide by x
end
if myrel <=re
conv=1;
else conv=0;
end
f= feval(fh,x);
For my driver function is this the correct start?
function test_newtfun()
x=[1 2 3];
y = newtfun(x);
I guess I am a little confused. Any help would be much appreciated

답변 (1개)

Torsten
Torsten 2023년 10월 4일
이동: Torsten 2023년 10월 4일
For my driver function is this the correct start?
No. The driver function calls newtfun with three input arguments: The function you search the root of, its derivative (both as function handles) and an initial guess for the root.
Example:
x = newtfun(@(x)x^2-2,@(x)2*x,1)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by