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 Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by