Creating a driver routine for my function
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
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)
0 commentaires
Voir également
Catégories
En savoir plus sur Whos 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!