MATLAB BASIC : Storing values in array through indexing in a loop function

3 views (last 30 days)
Hi all. This is my first MATLAB code and I am still very new, so please forgive me for the very naive question. I have created a custom function "newt" which evaluates the root of an input function using the newton-raphson scheme. In the script, one of the output is an array 'z' which is supposed to store the value of root approximation after each iteration, including the first guess. However when i run the code to find the root for function "5^t - t^2 - 5", not all the values of the root estimation is stored accordingly in the array 'z'. I have provided below: the command prompts I typed at the command window to call the function, the result I get in command window, .m script for the custom function "newt", and also my queries in detail. There is no error message displayed and the function "newt" itself is seem to be working but not in the way I intended it. I checked and checked and I can't find anything wrong with my codes, which led me to think that either my PC is screwed up or some bugs in the version of MATLAB i'm using. I'm going crazy over this!!! Can anyone give suggestions/troubleshoot? THANNKKSS!!! :-D
*1. Prompt at Command Window*
>> func = @(t) 5^t - t^2 - 5;
[i, err, z] = newt(func, 1, 0.001, 1e-9, 10)
2. Result of the prompt i =
10
err =
0
z =
Columns 1 through 6
1.1446 0 0 0 0 0
Columns 7 through 10
0 0 0 1.1446
3. The newt.m script file (im sorry about the long-winded comments, it is mainly for my own reference :-P)
function [i, err, z] = newt(func,x,h,tol,n);
%Before using this function, the input function must be defned in a
%function handle. The function handle is then input into the "func"
%argument. This function works by finding numerical approximation for the
%derivate of the input function, using the central difference approximation.
%Therefore this function is more robust than "newt23" function for applying
%Newton-Raphson scheme for finding the roots of a function.
%Input arguments = [func, x, h, tol, n]
%Output arguments = [i, err, z]
%func : enter the handle of the input function
%x : intial estimation value of the root
%h : enter the step size to be used for the central difference
%approximation. Normally used is = 0.001
%tol : tolerance value, normally used is = 1e-9
%n : number of iterations
%i : the i-th iteration when root found within tol level
%err : relative error between at i-th iteration
%z : array which stores the value of root at each iteration
err = 10*tol;
z(1,n) = 0; %array to store answers of each iteration.
while (err > tol);
for i = [1,n];
z(1,i) = x;
fv = feval(func , x);%evaluates the value of function "func" at 'x'
fpv = fcentraldiff(func , x , h); %evaluates the value of derivative of
%funciton "func" at 'x'.
dx = -fv./fpv;
err = abs(dx);
x = x + dx;
end
end
function [fpv] = fcentraldiff(func, x, h)
fpv = (feval(func, x+h) - feval(func, x-h))/(2.*h); %using central difference
%approximation to
%find the derivate of
%"func" at value 'x'
4. Now, Here are my Queries
a) The conditions for the "while" doesn't seem to be stopping the loop, even though i think around the 5th or 6th iterations the value of error 'err' goes below the tolerance level 'tol'. The programs carries on with the loop until the number of iterations allowed in the "for" loop is exhausted. I have checked this by letting huge values of tol = 1 and n = 30. And guess what, the program runs 30 iterations!!
b) the array 'z' only stores the final value of 'x' at the end of n-th iteration instead of storing each value of 'x' at the end of each iteration to the corresponding column. what seems to be the problem here? is my indexing wrong?
c) related to the above question, the array 'z' does not even pick up the initial value of 'x' inputted in the argument when calling the function "newt" when by right it should.
THANKS AGAIN!!
  2 Comments
Kanakarajh Raman
Kanakarajh Raman on 16 Feb 2014
the meaningful code itself is short but there are long comments which serve as reference point in the future since i am just beginning to use MATLAB and I dont have prior experience in any other programming language. So yes, a complete noob. :-)
Kanakarajh Raman
Kanakarajh Raman on 16 Feb 2014
OH!! I discovered the problem. I should have used for i =[1:n] instead of for i=[1,n] which led the script to run only at i=1 and i=n and not throughout. Also discovered that the while loop is redundant as the values evaluated inside the for loop is not being considered at the while loop. Improved my code and now it works!!
err = 10*tol;
z(1,n) = 0; %array to store answers of each iteration. See also line 24.
for i = [1:n];
z(1,i) = x;
fv = feval(func , x);%evaluates the value of function "func" at value 'x'
fpv = fcentraldiff(func , x , h); %evaluates the value of derivative of funciton "func" at value 'x'. See also line 33
dx = -fv./fpv;
err = abs(dx);
x = x + dx;
if (err < tol); i=i+1; z(1,i) = x; z(i+1:n) = []; return; end
end

Sign in to comment.

Accepted Answer

Nitin
Nitin on 16 Feb 2014
Just a quick comment on your code: Please make it more concise. It will be easier to understand and assist you.
1. Your for loop is inside your while, the for loop will therefore terminate and move back to your while loop and check if the condition is true and continue.
2. you need to initialize z, for example, z = zeros(1,20) is an array of 20. You can then save the elements of by writing z(i).
  1 Comment
Kanakarajh Raman
Kanakarajh Raman on 18 Feb 2014
yes. thanks for taking the time to go through and for the valuable comment... i have rectified the problem as per my above comment... actually the real code lines are only a few, there are more comments on it than actual working matlab commands. and yes i did initialize z with the z(1,n) = 0; line.... :-D

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!