Troubles on differentiation inside a function
Mostrar comentarios más antiguos
Do you guys know how to differentiate a function inside a function?
I want to input a function, ask the code to differentiate it and use both function and its derivative in a loop.
function [zero] = newton_raphson (x0,precisao,funcao)
%Estimates the root of a function
syms x
f=funcao(x);
derivada=diff(f,x);
x(1)=x0;
erro=realmax;
i=1;
while erro > precisao
x(i+1)=x(i)-f(x(i))/derivada(x(i));
erro = abs (x(i+1)-x(i));
i=i+1;
end
disp(['The estimate root is ' num2str(x(end)) ' with ' num2str(i) ' iteractions.'])
return
The error message when i enter "newton_raphson(2,10^-3, @(x) x+1): ??? Error using ==> mupadmex Error in MuPAD command: Index exceeds matrix dimensions.
Error in ==> sym.sym>sym.subsref at 1366
B = mupadmex('mllib::subsref',A.s,inds{:});
Error in ==> newton_raphson at 16
x(i+1)=x(i)-f(x(i))/derivada(x(i));
Thanks!
3 comentarios
Sargondjani
el 21 de Jul. de 2012
you should give the error message
note that 'error' is a reserved matlab command, so you shouldnt use it.
Ryan
el 21 de Jul. de 2012
I'm guessing f isn't in a proper format for diff. What is "func"? What class is f? What does unique(f) return?
Arthur
el 22 de Jul. de 2012
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Arithmetic Operations en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!