Matlab program that uses newtons method to output the roots

Matlab program that uses newtons method to output the roots of a second order polynomial and the number of iterations used. The user inputs the numbers as the coefficents

Solution

newtraph.m

function [root]=newtraph(func,dfunc,xr,es)

% input:
% func = name of function
% dfunc = name of derivative of function
% xr = initial guess
% es = desired relative error (default = 0.0001%)
% output:
% root = real root

if nargin < 3,
    error(\'at least 3 input arguments required\');
end
if nargin < 4|isempty(es),
    es = 0.0001;
end

x = xr; y = func(x); yp = dfunc(x);
while norm(y)>10e-7
    x = x - func(x)/dfunc(x);
    y = func(x);
end
root = x;

Output:-

>> y = @(m) sqrt(9.81*m/0.25)*tanh(sqrt(9.81*0.25/m)*4)-36;
>> dy = @(m) 1/2*sqrt(9.81/(m*0.25))*tanh((9.81*0.25/m)^(1/2)*4)-9.81/(2*m)*sech(sqrt(9.81*0.25/m)*4)^2;
>> newtraph(y,dy,140,0.00001)

ans =

142.7376

Matlab program that uses newtons method to output the roots of a second order polynomial and the number of iterations used. The user inputs the numbers as the c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site