Newton-Raphson method to solve equations.

1 vue (au cours des 30 derniers jours)
Kokalz
Kokalz le 15 Oct 2012
Hi guys!
I'm trying to write a function that would allow me to use numerical methods (Newton-Raphson method) to solve equations. I don't have any additional toolboxes on my matlab. So the problem I have is that I cannot declare 'x' as a variable when I input it in to my function. As a result I can't use any functions that are not polynomials in my input.
Is there any way to bypass that ?
Example:
I need to find the solution of sin(x) = x. But I cannot imput that into my function because variable x is not defined.

Réponses (1)

Gang-Gyoo
Gang-Gyoo le 16 Oct 2012
Please refer to this code.
function main x0= 1; % initial value eps= 1e-6; % accuracy nmax= 50; % maximum iteration x= newton(@(x) func(x), @(x) dfunc(x), x0, eps, nmax) end
function x= newton(fun, dfun, x0, eps, nmax) for i= 1: nmax x1= x0-fun(x0)/dfun(x0); if abs(x1-x0) <= eps x= x1; return; end x0= x1; end disp('Completed unsuccessfully'); x= x1; end
function f= func(x) f= sin(x)-x; end function f= dfunc(x) f= cos(x)-1; end

Community Treasure Hunt

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

Start Hunting!

Translated by