Creating a driver routine for my function

9 vues (au cours des 30 derniers jours)
Jason
Jason le 4 Oct 2023
Déplacé(e) : Torsten le 4 Oct 2023
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

Réponses (1)

Torsten
Torsten le 4 Oct 2023
Déplacé(e) : Torsten le 4 Oct 2023
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)

Catégories

En savoir plus sur Data Exploration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by