Application of the Newton-Raphson method of finding roots, program does not work

4 views (last 30 days)
i've been given a task to use the newton-raphson method to find roots for an equation, getting all kinds of errors. This is the first time i'm using matlab, and would appreciate if any of you could help me debug this thing and go 1 step ahead in learning proper syntax by it...
the program is divided to main+3 functions, and here they are:
function main()
global h0 m k g
h0=300;
m=0.25;
k=0.1;
g=32.174;
t0=3;
while abs(6.0032-t0)>0.005
t0=nraphson(t0, fun(t0), der(t0));
end
function [ t0 ] = nraphson( t0, fun, der )
%This is the Newton-Raphson iterative method
tplus= t0-(fun(to)/der(t0);
t0=tplus;
end
function [ a ] = fun(t0)
%This is the mathematical function we are asked to work with
global h0 g k m
a=h0-(m*t0*g/k)+((g*m^2)/k^2)*(1-exp(-k*t0/m));
end
function [ b ] = der( t0)
%Derivative of the function we were given to find a root for
global m g k
b= (-m*g/k)+g*m*exp(-k*t0/m)/k;
end
It's fairly simple, and i probably botched it in feeding the arguments/output values properly into functions and whatnot. -the program has to be in 4 parts like this, this is what iv'e been asked. Any help would be tremendously appreciated, been cracking my head for a full day now with no progress....
  7 Comments
Walter Roberson
Walter Roberson on 16 May 2015
You are passing fun(t0) into nraphson, where t0 has a definite value. That means that fun(t0) is to be evaluated and the numeric result is to be passed in. You then try to index that numeric result at t0, since variable(t0) means indexing unless variable is a function handle.
Try
t0=nraphson(t0, @fun, @der)
Rob Dunham
Rob Dunham on 16 May 2015
This did the trick. You have my thanks Walter, both for helping solve this particular problem and for explaining so i won't repeat a similar mistake in the future.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!