Newton-Raphson method to solve equations.

조회 수: 1 (최근 30일)
Kokalz
Kokalz 2012년 10월 15일
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