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. :-)
MATLAB BASIC : Storing values in array through indexing in a loop function
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Nitin
2014 年 2 月 16 日
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).
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!