Creating a driver routine for my function

9 ビュー (過去 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 Center および File ExchangeTesting Frameworks についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by