Creating a driver routine for my function
9 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Testing Frameworks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!