Clear Filters
Clear Filters

How can I replace my values in an iteration?

2 views (last 30 days)
This is a code I am using to run the modified secant open method. However, I don't understand why my values are not replacing the old ones each time I run the iteration.
function find_newX = modifiedsecant(initialGuess, pertubation, tolerance, func)
%%variables used as input input arguments:
%%initialGuess = initial guess for x root user inputs
%%pertubation = small pertubation fraction
%%tolerance = amount of error allowed to stop iterations
%%func = name of the function
%%the first step is to find the value of the func at the initial guess
%%func_initialGuess = func(initialGuess);
%%the second step is to find the value of the func at the (intial guess + perturbation*initial guess)
%%xp = initialGuess * perturbation
%%xp = initialGuess*pertubation;
%%func_initialGuess_and_xp = func(initialGuess + xp);
%%interative formula for modified secant method
%%this equation will be reiterated until the required tolerance is met
%%find_newX = (xp*func_initialGuess)/(func_initialGuess_and_xp - func_initialGuess);
%%fourth step is to calculate the relative approximate error to compare to the tolerance
%%assume first that ea = 100%
n = 0;
%%creating a while loop such it runs until the tolerance is reached
while n < 10
func_initialGuess = func(initialGuess); %%these are the formulas made in the previous comments put in the while loop
xp = initialGuess*pertubation;
func_initialGuess_and_xp = func(initialGuess + xp);
find_newX = (initialGuess - (xp*func_initialGuess))/(func_initialGuess_and_xp - func_initialGuess);
ea = abs((find_newX - initialGuess)/find_newX); %%calculates the new ea
find_newX = initialGuess; %%this is setting the find_newX value to the intialGuess variable
n = n+1;
display(initialGuess);
display(find_newX);
end
When I display the values for initialGuess, it will display the original value I assigned rather than replacing it with the find_newX value. I don't understand why.

Accepted Answer

Geoff Hayes
Geoff Hayes on 15 Feb 2016
Maryam - perhaps the problem is with the first line of the while loop
func_initialGuess = func(initialGuess);
Every iteration of the loop uses the same initialGuess that was passed as a parameter into this function. And the line of code
find_newX = initialGuess;
will always ensure that the two are identical and overwrites the previous initialization of find_newX
find_newX = (initialGuess - (xp*func_initialGuess))/(func_initialGuess_and_xp - func_initialGuess);
I think that you need to reset the initialGuess on each iteration to the "new" estimate. So instead of doing
find_newX = initialGuess;
do
initialGuess = find_newX;
This way, the initialGuess or new estimate is used on subsequent iterations.
As an aside, you may want to consider renaming this variable to something else as it is no longer an initial guess after the first iteration has completed.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!