Newton-Raphson method to solve equations.

2 次查看(过去 30 天)
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.

回答(1 个)

Gang-Gyoo
Gang-Gyoo 2012-10-16
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

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by